Java et le mac

Flash Gordon

Membre actif
29 Août 2008
428
65
Paumé dans un paquetage.
Salut à tous. J'ai un petit problème avec mon code. Je suis actuellement les cours de cysboy, et lorsque je tape ce code dans ma classe panneau :


Bloc de code:
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.io.*;



public class Panneau extends JPanel {
    
    public void paintComponent (Graphics g) {
        
        int x1 = this.getWidth()/4;
        int y1 = this.getHeight()/4;
        int x[] = {20,10,20,30,40,50,60,10};
        int y[] = {40,50,60,95,101,300,20,10};
        
        System.out.println("largeur = "+this.getWidth()+", longueur = "+this.getHeight());
        
        g.drawRoundRect(x1, y1, this.getWidth()/2, this.getHeight()/2,15,15);
        g.drawRect(x1, y1, this.getWidth()/2, this.getHeight()/2);
        g.drawOval(x1, y1, this.getWidth()/2, this.getHeight()/2);
        g.drawLine(0,0, this.getWidth(), this.getHeight());
        g.drawLine(0, this.getHeight(), this.getWidth(), 0);
        g.drawPolyline(x,y,8);
        g.drawString("blabla test !! ", 11, 11);
        Image img = null;
        try {
            img = ImageIO.read(new File("images.jpg"));
            g.drawImage(img, 0,0,this);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      
    }
}
Voici l'erreur affichée :

javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1279)
at Panneau.paintComponent(Panneau.java:28)
at javax.swing.JComponent.paint(JComponent.java:1006)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:559)
at javax.swing.JComponent.paintChildren(JComponent.java:843)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:34)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)


Donc, à mon avis, c'est du à un probleme de hiérarichisation des fichiers. Comment lire un fichier qui est sur mon burreau sur mac os x avec java ? J'ai essayé ça : Users/monnometprenomquevousneconnaitrezpas/Desktop

(ce qui est le chemin du fichier) mais ça ne fonctionne pas nomplus... Merci de m'éclairer à ce sujet au plus vite ! Ciao !
 
Place ton fichier dans ton classpath (dans le même répertoire que ta classe) et fais : getClass().getResourceAsStream("images.jpg") au lieu de new File("images.jpg").
 
Dans tous les cas, il vaut mieux se passer de java.io.File si on peut, à cause des problèmes de portabilités.
Tout comme il est déconseillé d'utiliser java.net.URL si on peut utiliser java.net.URI (cf. "Effective Java" de Joshua Bloch).