This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Color is not changing

 
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

 
Rancher
Posts: 3191
29
  • 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
Rancher
Posts: 3191
29
  • 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



 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic