# Draw series of concentric circles using java.util.Random

• 09-15-2013, 05:21 PM
rciri
Draw series of concentric circles using java.util.Random
vote
favorite
I'm really stuck on how to go about programming this. Need to draw a series of 8 concentric circles using Java drawArc method with following conditions

using import java.util.Random library

Provide for starting the drawings at random location (i.e., the x-y cooridinate must be calculated randomly).
Provide a random color for each circle
Provide a random diameter for each circle
My current code is able to get random random color for each circle but not clear how to meet other random conditions

Code:

```// Exercise 12.6 Solution: CirclesJPanel.java // This program draws concentric circles import java.awt.Graphics; import javax.swing.JPanel; public class ConcentricCircles extends JPanel {   // draw eight Circles separated by 10 pixels   public void paintComponent( Graphics g )   {       Random random = new Random();       super.paintComponent( g );       // create 8 concentric circles       for ( int topLeft = 0; topLeft < 80; topLeft += 10 )       {         int radius = 160 - ( topLeft * 2 );         int r = random.nextInt(255);         int gr = random.nextInt(255);         int b = random.nextInt(255);         Color c = new Color(r,gr,b);         g.setColor(c);         g.drawArc( topLeft + 10, topLeft + 25, radius, radius, 0, 360 );       } // end for   }  } // This program draws concentric circles import javax.swing.JFrame; public class ConcentricCirclesTest extends JFrame {     /**     * @param args     */     public static void main(String[] args) {         // TODO Auto-generated method stub         JFrame frame=new JFrame("Concentric Circles");         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         ConcentricCircles cCirclesJPanel = new ConcentricCircles();         frame.add(cCirclesJPanel);         frame.setSize(200,250);         frame.setVisible(true);     }//end main }```
• 09-15-2013, 06:45 PM
rciri
Re: Draw series of concentric circles using java.util.Random
Any one?
• 09-15-2013, 06:49 PM
jim829
Re: Draw series of concentric circles using java.util.Random
It's Sunday and you only posted two hours ago. Be patient. Also, why are you using drawArc instead of drawOval?

Regards,
Jim
• 09-15-2013, 06:53 PM
rciri
Re: Draw series of concentric circles using java.util.Random
Sorry for that. It is requirement part of the project to use drawArc.
• 09-15-2013, 07:03 PM
jim829
Re: Draw series of concentric circles using java.util.Random
Where is your ConcentricCircles class? See line 41.

Regards,
Jim
• 09-15-2013, 07:05 PM
jim829
Re: Draw series of concentric circles using java.util.Random
Hey, your program works fine! Just change ConcentricCircles to CirclesJPanel.

Regards,
Jim
• 09-15-2013, 07:15 PM
rciri
Re: Draw series of concentric circles using java.util.Random
I dint get you "Just change ConcentricCircles to CirclesJPane" . Could you show in the code what needs to change?. Remember concentric should have random x,y coordinates and random diameter.
• 09-15-2013, 07:21 PM
rciri
Re: Draw series of concentric circles using java.util.Random
Sorry i changed the class but dint copy that. The one you pointed is already corrected . The programs works except no sure on how implement random conditions
• 09-15-2013, 07:34 PM
rciri
Re: Draw series of concentric circles using java.util.Random
Here is what i worked on the Concentric class for random conditions not sure if that is correct though-

import java.awt.Graphics;
import javax.swing.JPanel;
import java.util.Random;
import java.awt.Color;

public class ConcentricCircles extends JPanel {

public void paintComponent(Graphics g)
{

super.paintComponent(g); // to paint the window
Random random = new Random();

int d = random.nextInt(200);

int y = random.nextInt(20);
int z = random.nextInt(20);

for ( int topLeft = 0; topLeft < 80; topLeft += 10 )
{
int radius = d - ( topLeft * 2 );
int r = random.nextInt(255);
int gr = random.nextInt(255);
int b = random.nextInt(255);
Color c = new Color(r,gr,b);
g.setColor(c);
g.drawArc( topLeft + y, topLeft + z, radius, radius, 0, 360 );

}//end for

}//end method paint

}//end class Concentric Circles
• 09-15-2013, 08:29 PM
jim829
Re: Draw series of concentric circles using java.util.Random
Instead of calculating topleft in a for loop, simply choose the value randomly.

Regards,
Jim
• 09-15-2013, 08:59 PM
rciri
Re: Draw series of concentric circles using java.util.Random
Could you show up in the code where i will need to change?.
• 09-15-2013, 09:04 PM
DarrylBurke
Re: Draw series of concentric circles using java.util.Random
• 09-15-2013, 09:09 PM
rciri
Re: Draw series of concentric circles using java.util.Random
Yes it is the same post created by me.