Creo un JFrame
JFrame frame=new JFrame();
String[] coord={"200","150"};
MyPane p=new MyPane(coord,new File("mappa.jpg"),new File("freccia2.gif"));
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOS E);
frame.setLocation(670,0);
frame.setSize(620,400);
frame.setContentPane(p);
frame.setTitle("Cartina");
frame.setVisible(true);
in MyPane ho il seguente costruttore:
public MyPane(String[] coord,File f,File f2){
super(true); //crea un JPanel con doubleBuffered true
this.coord=coord;
try{
setImage(ImageIO.read(f));
setImage(ImageIO.read(f2));
} catch(Exception e) {}
}
public void setImage(BufferedImage img){
if(i==0){
this.img = img;
width = img.getWidth();
height = img.getHeight();
i++;}
else{
this.img2 = img;
width2 = img.getWidth();
height2 = img.getHeight();
}
}
l'override di paintComponent:
public void paintComponent(Graphics g){
super.paintComponent(g);
disegnaMio(g);
}
e il mio metodo:
private void disegnaMio(Graphics g) {
g.setColor(Color.RED);
g.drawImage(img, 0, 0, null);
g.drawString("ciao", 12, 12);
g.setFont(new Font("Arial",Font.BOLD,13));
g.drawRoundRect(50, 50, 50, 50, 50, 50);
g.drawImage(img2,Integer.parseInt(coord[0]) ,Integer.parseInt(coord[1]) , this);
}
e in questo modo ho come sfondo img, sopra alcune cose tra cui img2; io vorrei fare apparire e scomparire img2 per poi rifarlo apparire in un altro punto del pannello