GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Simple Gui Question (Head First Java) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Simple Gui Question (Head First Java)" Watch "Simple Gui Question (Head First Java)" New topic
Author

Simple Gui Question (Head First Java)

Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
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.

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Sounds like something which would sit better on our Swing forum. Moving.
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
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?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

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.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
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

Joined: Feb 23, 2007
Posts: 1561
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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

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
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Damn!
Pete beat me to it. Curse this slow connection.
Brian Pianczk
Ranch Hand

Joined: Jan 26, 2009
Posts: 45
I appreciate the patience, thanks to both of you. I now understand what exactly is going on.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Simple Gui Question (Head First Java)