-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
  • 166,747
  • 40
  • 210
  • 420

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
  • 35,321
  • 10
  • 55
  • 81