File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with simple swing GUI program

 
Fritz Guerilus
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?


 
Rob Camick
Ranch Hand
Posts: 2473
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2473
8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ralph, changing the panel to the CENTER made it work.

Rob, Darryl thanks for the further explaination and tips.

-Fritz
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic