Results 1 to 13 of 13
  1. #1
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Unhappy 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

    Java 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
    }
    Last edited by rciri; 09-15-2013 at 07:20 PM.

  2. #2
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Default Re: Draw series of concentric circles using java.util.Random

    Any one?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Default Re: Draw series of concentric circles using java.util.Random

    Sorry for that. It is requirement part of the project to use drawArc.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Draw series of concentric circles using java.util.Random

    Where is your ConcentricCircles class? See line 41.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default Re: Draw series of concentric circles using java.util.Random

    Hey, your program works fine! Just change ConcentricCircles to CirclesJPanel.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Default 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.

  8. #8
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Default 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

  9. #9
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Default 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

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,614
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Default Re: Draw series of concentric circles using java.util.Random

    Could you show up in the code where i will need to change?.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,240
    Rep Power
    19

    Default Re: Draw series of concentric circles using java.util.Random

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    rciri is offline Member
    Join Date
    Sep 2013
    Posts
    8
    Rep Power
    0

    Default Re: Draw series of concentric circles using java.util.Random

    Yes it is the same post created by me.

Similar Threads

  1. Replies: 12
    Last Post: 07-13-2013, 09:12 AM
  2. Drawing concentric circles
    By jazzo in forum New To Java
    Replies: 7
    Last Post: 02-07-2013, 03:01 PM
  3. Replies: 16
    Last Post: 01-30-2012, 05:36 AM
  4. Draw circles, select circles
    By cselic in forum Java 2D
    Replies: 2
    Last Post: 05-17-2010, 02:02 PM
  5. Replies: 4
    Last Post: 09-13-2008, 11:07 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •