aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem with simple swing GUI program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with simple swing GUI program" Watch "problem with simple swing GUI program" New topic
Author

problem with simple swing GUI program

Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Hello,
I'm having problems with getting the desired results when executing this program.

The code is from HeadFirst Java chapter 12, the first chapter on swing.

When I click on the button, it's supposed to change the colors on the oval randomly, but nothing happens.

Can anyone help and explain why nothing happens, and what I must do get it to work the way I want.

I wrote it in eclipse. Any help would be greatly appreciated.

I'm very rusty with Java, and I'm trying to re-learn/practice, so I'm starting almost at SQAURE-ONE.

Thanks in advance.
BTW--
The eclipse program added this line of code to my MyDrawPanel inner class, but I don't understand why since the book didin't include it.

"private static final long serialVersionUID = 1L;"

Can anyone explain why?



SCJP 6.0
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
1) Have the component paint the background itself:



2) When you do custom painting the component doesn't have a preferred size. So the preferred size is actually (10, 10). The width gets stretched out to be the size of the window because you added the component to the NORTH. But the height is 10, to when you try to paint the component at (70, 70) the oval is out of bounds of the component so there is nothing to paint. Give you component a preferred size or add the component to the CENTER so is will take up all the extra space of the frame.

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I've forgotten -- or maybe I never knew -- what happens when you add things to south and north like you have, but if you change the drawPanel so it is added to CENTER instead of NORTH, it works.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
Ralph Cook wrote:I've forgotten -- or maybe I never knew -- what happens when you add things to south and north like you have, but if you change the drawPanel so it is added to CENTER instead of NORTH, it works.


Then you should read my answer.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Rob Camick wrote:
Ralph Cook wrote:I've forgotten -- or maybe I never knew -- what happens when you add things to south and north like you have, but if you change the drawPanel so it is added to CENTER instead of NORTH, it works.


Then you should read my answer.


You posted that while I was working things out -- didn't know it was there until after I had replied.

rc
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Also, you have absolutely no control over when and how often paintComponent(...) is called, so that' not the right place to compute a new random Color.

Declare randomColor as an instance field and assign a new value in actionPerformed(...). If you don't assign it at declaration, don't forget to perform a null check in paintComponent(...).

Unless of course you actually want the color to change randomly at times when you haven't clicked the button.


luck, db
There are no new questions, but there may be new answers.
Fritz Guerilus
Ranch Hand

Joined: Jun 20, 2009
Posts: 65
Thanks Ralph, changing the panel to the CENTER made it work.

Rob, Darryl thanks for the further explaination and tips.

-Fritz
 
 
subject: problem with simple swing GUI program