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 61 62 63 64 65 66 67 68 69 70 71 72 73
|
public class DrawHotSpot extends JPanel {
class MyRectangle {
int x;
int y;
int width;
int height;
Color color;
}
ArrayList<MyRectangle> rectangles = new ArrayList<MyRectangle>();
private Image _imageSelected;
public DrawHotSpot(String pathImage){
super();
_imageSelected = Toolkit.getDefaultToolkit().getImage(pathImage);
try{
MediaTracker mt = new MediaTracker(this);
mt.addImage(_imageSelected,0);
mt.waitForAll();
}catch(Exception e){
e.printStackTrace();
}
}
public void setImageSelected(String pathImage) {
_imageSelected = Toolkit.getDefaultToolkit().getImage(pathImage);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
try{
MediaTracker mt = new MediaTracker(this);
mt.addImage(_imageSelected,0);
mt.waitForAll();
}catch(Exception e){
e.printStackTrace();
}
g.drawImage(_imageSelected,0,0,this.getWidth(),this.getHeight(),null);
System.out.print("on raffraichit !!\n");
for (int i=0;i<rectangles.size();++i)
traceRectangle(g,rectangles.get(i));
}
public void addRect( int ox, int oy, int width, int height, Color color) {
MyRectangle rectangle = new MyRectangle();
rectangle.x = ox;
rectangle.y = oy;
rectangle.width = width;
rectangle.height = height;
rectangle.color = color;
rectangles.add(rectangle);
System.out.print("rectangle ajoute !!\n");
repaint();
}
public void supprRect() {
int i = rectangles.size()-1;
rectangles.remove(i);
repaint();
}
private void traceRectangle(Graphics g,MyRectangle rect) {
g.setColor(rect.color);
System.out.print("on affiche !!\n");
g.drawRect(rect.x,rect.y,rect.width,rect.height);
} |
Partager