2

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

  1. Provide for starting the drawings at random location (i.e., the x-y cooridinate must be calculated randomly).
  2. Provide a random color for each circle
  3. 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

// 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
}
user845405
  • 1,461
  • 5
  • 23
  • 43
  • Start by googling "java random number" – dnault Sep 15 '13 at 05:46
  • I was able to get random color for each circle but not clear o how to approach for radius and x,y coordinates. – user845405 Sep 15 '13 at 06:02
  • Think about which variables determine the x,y coordinates and radius. Assign random values to these variables. – dnault Sep 15 '13 at 06:26
  • hmm!.When i put random values for x,y and radius the output seems to be scattered circles. My understanding is concentric circles should have common center not sure how i could reflect this in the program. – user845405 Sep 15 '13 at 06:48
  • So which variable controls where the center of the circle is? Initialize it to a random value, then use that same value for all circles. It will help to pay close attention to the documentation of drawArc. http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Graphics.html#drawArc(int,%20int,%20int,%20int,%20int,%20int) – dnault Sep 15 '13 at 07:12
  • 2
    You only set random x,y coords for a single circle. All others would use the same. – Extreme Coders Sep 15 '13 at 07:12
  • Could you show up in the program where the correction needs to be done?. – user845405 Sep 15 '13 at 07:19
  • Try `drawOval()`, for [example](http://stackoverflow.com/a/2510048/230513), instead of `drawArc()`. – trashgod Sep 15 '13 at 15:20
  • I need to use only drawArc as part of the project. Any help on this ? – user845405 Sep 15 '13 at 15:48
  • @user845405: Use `@` to address individuals; I saw this by accident. – trashgod Sep 16 '13 at 12:34

1 Answers1

4

Several points are key to such an exercise:

  • Start with constants for the number of circles and step size; the random number generator in particular only needs to be created once.

    private static final int N = 8;
    private static final int S = 32;
    private static Random random = new Random();
    
  • Choose a random point whose coordinates fall within the drawing area.

    // a random point inset by S
    int x = random.nextInt(getWidth() - S * 2) + S;
    int y = random.nextInt(getHeight() - S * 2) + S;
    
  • For each circle, find the diameter as a function of S, adding a random fraction of a step, and render the arc at the chosen point offset by the radius.

    for (int i = 0; i < N; i++) {
        g2d.setColor(…);
        int d = (i + 1) * S + random.nextInt(S / 2);
        int r = d / 2;
        g2d.drawArc(x - r, y - r, d, d, 0, 360);
    }
    
  • Resize the enclosing frame, which forces a repaint(), to see the effect.

  • As random colors are not always appealing, consider Collections.shuffle() on a List<Color>.

    private final List<Color> clut = new ArrayList<Color>();
    …
    for (int i = 0; i < N; i++) {
        clut.add(Color.getHSBColor((float) i / N, 1, 1));
    }
    …
    Collections.shuffle(clut);
    …
    g2d.setColor(clut.get(i));
    
  • Override getPreferredSize() to establsh the drawing panel's size.

    private static final int W = S * 12;
    private static final int H = W;
    …
    @Override
    public Dimension getPreferredSize() {
        return new Dimension(W, H);
    
  • See also Initial Threads.

concentric arcs

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • [please to see here :-)](http://www.coderanch.com/t/619963/GUI/java/Draw-series-concentric-circles) – mKorbel Sep 16 '13 at 17:17
  • I'll surely try my hands on this, as I wanted to set a colour to all the `JPanel`s randomly at the start, though till now I am using `new Color(random.nextInt(256), ...)`, though this new thingy looks more promising (hoping that it takes care of the alpha part too, since I always use to neglect that) – nIcE cOw Oct 04 '13 at 18:25