A friend of mine asked me to help him with Swing, and I reached a point where I can't solve an issue. So, first I have an ImagePanel
public class ImagePanel extends JPanel {
private Image img;
public void setImage(String img) {
setImage(new ImageIcon(img).getImage());
}
public void setImage(Image img) {
int width = this.getWidth();
int height = (int) (((double) img.getHeight(null) / img.getWidth(null)) * width);
this.img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
}
@Override
public void paintComponent(Graphics g) {
g.drawImage(img, 0, 0, null);
}
}
And a FileChooser
, which should open the selected image into the ImagePanel. It simply calls setImage
of the ImagePanel
. But the image is not painted. No combinations of repatint, validate and invalidate help.
However, the image is painted when the JFrame
is resized (using the mouse).
So, the question is - what method is called on resizing (using the mouse), which makes the ImagePanel
to be repainted successfully.