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
| public class ImagePanel extends JPanel
{
private Image image;
private int x,y;// coordonnées du coin sup gauche
public ImagePanel(URL url,int x,int y)
{
super();
try {
image = new SvgImage(url).getImage(500,300);
} catch (IOException e) {
image = null;
System.err.println("Fichier invalide");
}
System.out.println("image: " + image);
this.x = x;
this.y = y;
setVisible(true);
}
/**
* Gere l'affichage graphique du JPanel, ainsi que le refraichissement.
*/
public void paintComponent(Graphics g)
{
System.out.println("Dans paintComponent. x = " + x + " y = " + y);
super.paintComponent(g);
g.drawImage(image,x,y,null);//w,h,null);
}
/**
* Teste la classe.
*/
public static void main(String[] args) throws Exception{
Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
JFrame w = new JFrame("Essai");
w.pack();
w.setLocation(screenSize.width/2, screenSize.height/2);
URL fichierSvg = new URL("http://.../butterfly.svg");
ImagePanel p = new ImagePanel(fichierSvg,0,5);
p.setVisible(true);
w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
w.getContentPane().add(p);
w.setSize(550,375);
w.setVisible(true);
}
} |
Partager