Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Color is not changing  RSS feed

 
Ma Mi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi there,

color only changes, when resizng frame with mouse, not when clicking. any ideas?
thanks and have nice day

 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
color only changes, when resizng frame with mouse, not when clicking.


I assume you mean the color doesn't change when you click the button. This is because you didn't add the ActionListener to the button. You need:



The code in your paintComponent() method is wrong. You should never change the state of a component in a painting method. That is resizing the frame should NOT affect the color.

To fix this problem all the random color code should be moved to the ActionListener:



Then the painting code should be:


 
Ma Mi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks it works!

re:frame - frame is changing its size, so jvm is repainting and repainting?
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
re:frame - frame is changing its size, so jvm is repainting and repainting?


Correct. Swing will determine when the paintComponent() method needs to be called, which is why you should not change a property of your class in the painting method.

The changing of the oval color should only be controlled by the user clicking the button.
 
Ma Mi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now, only click changes the color



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!