• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Simple Gui Question (Head First Java)

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all, I am going through the Head first java book and doing the section on Gui's.
The following code is suppose to make a window with two buttons, a label and a colored circle. The bottom button is supposed to change the color of the circle, and it does.
The East button is suppose to change the west text, and it does. My problem is the east button also changes the color of the circle as well.
I can't figure out whats causing this to happen, though I suspect it is something to do with the calls to the inner classes, my LabelListener is also calling the ColorListener.

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The circle color will change any time the JFrame is repainted. You can see this for yourself by minimizing and then restoring the JFrame. If you do this without pressing any buttons, the circle's color will change.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like something which would sit better on our Swing forum. Moving.
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete stein wrote:The circle color will change any time the JFrame is repainted. You can see this for yourself by minimizing and then restoring the JFrame. If you do this without pressing any buttons, the circle's color will change.



I did notice if I stretched the window the circle would change colors as I moved it. I guess my question, then, is why?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you stretch, resize, move the window, the JVM repaints it. And guess where your to paint the circle sits? In the paint method!

Though the paint is the correct place to have the circle code, if you want a consistent color, irrespective of what you do, just move your color generation code outside paint. Every time the paint is called, your code comes up with a new color.
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maneesh Godbole wrote:Whenever you stretch, resize, move the window, the JVM repaints it. And guess where your to paint the circle sits? In the paint method!

Though the paint is the correct place to have the circle code, if you want a consistent color, irrespective of what you do, just move your color generation code outside paint. Every time the paint is called, your code comes up with a new color.



Ok if I understand corectly , my problem is in :


Before I go into the errors I have found trying to minipulate that class, could we confirm this is the source of my "trouble"?

Thanks

I am assuming by when you say paint, you are referring to lines 16 and 17?
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. The problem is that the random colors are set in this method, the paintComponent method. This means that every time the window is repainted, new random colors are created and the circle's appearance thus changes.

To solve this, I recommend that you make the startColor and endColor class fields, that you set these random colors in the change color button's action listener (the ColorListener) and then call repaint.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry. When I said "paint" I meant it in the literal sense.

You are correct in isolating the paintComponent method as the "problematic area".
Check out lines 6,7,8 and 11,12,13.
Everytime the paintComponent() is called you are creating and assigning new values to the red, green and blue (twice; startColor and endColor), new because of the Math.random().

You got two choices.
1) Make red,green and blue as instance variables of the MyDrawPanel and initialize them only once.
2) Take out Math.random and put in some hard coded values yourself.

This will fix your problem of getting a new colored circle everytime.
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn!
Pete beat me to it. Curse this slow connection.
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I appreciate the patience, thanks to both of you. I now understand what exactly is going on.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic