Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Swing / AWT / SWT and the fly likes Concentric circle using JPanel Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Concentric circle using JPanel" Watch "Concentric circle using JPanel" New topic
Author

Concentric circle using JPanel

Dj Lee
Greenhorn

Joined: Apr 14, 2011
Posts: 3
Hello, I'm a beginner in Java and I'm trying to create a jpanel that
creates 10 concentric circles with random radius. But I'm having a bit trouble with it.

Here are my codes:




When I run this, I get only one circle (sometimes none...)
if i set radius = generator.nextInt(MAX_DIAMETER) + 1;

But if i replace MAX_DIAMETER which is 600, to something small maybe 100.
radius = generator.nextInt(100)+1;
than i get 5-10 circles...

What am i doing wrong??
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Sounds to me like using a big number causes most of the 10 circles to be located outside of the box displayed in your panel.
Dj Lee
Greenhorn

Joined: Apr 14, 2011
Posts: 3
Paul Clapham wrote:Sounds to me like using a big number causes most of the 10 circles to be located outside of the box displayed in your panel.


the panel is running in (600, 600) so isnt it suppose to show all the circles since I set the diameter of the circle to not exceed 600?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

That would be true if the centres of the circles were all at (0, 0). But it isn't true if the centre of the circles is way outside your box.
Debashis jain
Greenhorn

Joined: Mar 24, 2011
Posts: 3
When I run this, I get only one circle (sometimes none...)
if i set radius = generator.nextInt(MAX_DIAMETER) + 1;

But if i replace MAX_DIAMETER which is 600, to something small maybe 100.
radius = generator.nextInt(100)+1;
than i get 5-10 circles...

What am i doing wrong??


The radius of the circle is (diameter/2)
"generator.nextInt(MAX_DIAMETER) "when you are using this function that's mean you are expecting a random number from 0-600
so when you write the code diameter=2*radius....actually the diameter at most of the time becomes as big as 1200,and your panel size is (600,600) so it won't suit the created circles.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
I think this thread would sit better on our GUIs forum: moving.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
. . . and welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Concentric circle using JPanel
 
Similar Threads
Graphics Graphics Graphics - Where are my circles?
Event Handlers:Mouse over "circles"
Program is not doing what it is suppose to
Window (Game) not registering mouse clicks occasionally
Graphics Graphics Graphics - Where are my circles?