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 55 56 57 58 59 60
|
public class ImagePanel extends JPanel {
private Image image;
private File imagePath;
private Dimension dim;
private Collection shapes;
/**
* Crée un nouveau ImagePanel avec l'image renseignée.
* @param imagePath <code>File</code> : chemin vers l'image à afficher
*/
public ImagePanel(File imagePath) {
this.imagePath = imagePath;
this.shapes = new ArrayList();
try {
image = ImageIO.read(imagePath);
dim = new Dimension( image.getWidth(null), image.getHeight(null));
setPreferredSize( dim );
} catch (IOException e) {
e.printStackTrace();
}
}
public void drawShape(Drawable shape) {
shapes.add(shape);
shape.draw();
}
protected void paintComponent(Graphics g) {
//public void paint(Graphics g) {
super.paintComponent(g);
Graphics2D surface = (Graphics2D) g;
int x = getSize().width - image.getWidth(null);
int y = getSize().height - image.getHeight(null);
surface.drawImage(image, x, y, null);
for (Iterator it = shapes.iterator(); it.hasNext();) {
Drawable shape = (Drawable) it.next();
shape.draw();
}
}
public Collection getShapes() {
return shapes;
}
public File getImagePath() {
return imagePath;
}
public Image getImage() {
return image;
}
} |
Partager