[débutant] Probleme d'affichage
Bonjour,
J'ai ici une classe mypanel recupérée en feuilletant le forum, j'ai compris comment elle marchait :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
public class MyPanel extends JPanel {
protected BufferedImage image = null;
public MyPanel(Color background, String imagePath)
{
setBackground(background);
try
{
image = ImageIO.read(getClass().getResource(imagePath));
}
catch (IOException ioe)
{
System.out.println("Exception 1");
}
catch (IllegalArgumentException iae)
{
System.out.println("Exception 2");
}
}
protected void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
g2.setColor(getBackground());
g2.fill(g.getClip());
if (image != null)
{
int x = (getWidth() - image.getWidth()) / 2;
int y = (getHeight() - image.getHeight()) / 2;
g2.drawImage(image, x, y, null);
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
JFrame f = new JFrame("Image Panel");
MyPanel panel = new MyPanel(Color.blue, "Death Valley 002.jpg");
f.setContentPane(panel);
f.pack();
f.setVisible(true);
}
});
}
} |
Le créateur de cette classe affirmait qu'elle fonctionnait a tous les coups.
je l'ai mise en application ici :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| import java.awt.*;
import javax.swing.*;
public class YayaTest extends JFrame
{
private final int hauteur=1000;
private final int largeur=1000;
public YayaTest ()
{
this.setVisible(true);
this.setSize(largeur,hauteur);
MyPanel monPanel=new MyPanel(Color.white,new String("42k.png"));
this.setContentPane(monPanel);
this.repaint();
}
public static void main (String []args)
{
YayaTest y = new YayaTest();
}
} |
Lorsque j'execute ma classe, l'exception 2 (IllegalArgumentException) est levée...
La question est : pourquoi ? Nous avons essayé avec 3 types, sans résultat : PNG, JPG et BMP.
De l'aide ?
merci d'avance
mavina