-2

As the title, I have this code:

    protected void paintComponent(Graphics g) {
        Image image = Toolkit.getDefaultToolkit().getImage("image.png");
        g.drawImage(image, getWidth(), getHeight(), this);
    }

Now I want to change the image whenever I click a JButton.

Is there anyway to change the image in paintComponent when JButton is clicked?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Raku Ichijo
  • 1
  • 1
  • 3

1 Answers1

0

Add a click listener to for the button click event. When the listener onClick() method is called, change the button image.

DwB
  • 37,124
  • 11
  • 56
  • 82