I have a class (not public, so you probably do not know the class) which is drawing a picture with picture.draw(g2). With picture.changeToDry() I can change the picture. It works fine, when I call the picture and change it in implemented paintComponent() method. But when I call changeToDry() and after it repaint(), it does not work. It just shows the default picture but does not update it. What do I have to do, so that I can change the picture and update the JPanel in another method? It has to be something with the repaint() because the methods work in paintComponent() but not elsewhere.
EDIT: update() will be called in another class. Plus, as I mentioned in the comments, I cannot give more information for the class, as it is a private, teaching-only class. And obviously it should work. Otherwise my teacher would not give it to us. EDIT 2: I have another class ClimateFrame which is drawing a frame. There I am calling update()
public class ClimatePanel extends JPanel {
ClimatePicture picture = new ClimatePicture(100, 100);
public void update() {
picture.changeToDry();
repaint(); // does not work
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// picture.changeToDry() would work here
picture.draw(g2);
}
}
public class ClimateFrame extends JFrame {
public ClimateFrame() {
setTitle("A task");
setLayout(new BorderLayout());
add(new ClimatePanel(), BorderLayout.CENTER);
}
public static void main(String [] args) {
ClimateFrame frame = new ClimateFrame ();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);
frame.setVisible(true);
ClimatePanel climatePanel = new ClimatePanel();
climatePanel.update();
}
}