Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple Gui Question (Head First Java)

 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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.

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48454
56
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I appreciate the patience, thanks to both of you. I now understand what exactly is going on.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic