I make a ImagePanel class that get a ImageIcon object and draw it show panel.
import javax.swing.JPanel;
import javax.swing.ImageIcon;
import java.awt.Graphics;
class ImagePanel extends JPanel {
private ImageIcon img;
public ImagePanel(ImageIcon img) {
this.setImage(img);
}
public void setImage(ImageIcon img){
this.img = img;
}
@Override
public void paintComponent(Graphics g) {
if(img instanceof ImageIcon)
g.drawImage(img.getImage(), 0, 0, this.getWidth(), this.getHeight(), null);
}
}
but the problem is when I change the img, it don't show it on panel till I change frame size. how can I update it?
EDIT: repaint() does not clean last img on panel.