Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

problem with simple swing GUI program

 
Ranch Hand
Posts: 65
  • 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?


 
Rancher
Posts: 3205
29
  • 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.

 
Ranch Hand
Posts: 479
  • 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
Rancher
Posts: 3205
29
  • 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
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • 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
  • 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
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic