Results 1 to 4 of 4
  1. #1
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Trouble with using Random method

    I am having some trouble with randomizing the diameter for these circles. I have to create 100 circles with random colors and random diameters. And I am a bit stuck on trying to randomize the diameter. Can someone please help me I am new to programming and need some help. Thank you!
    Everything in my program works except they dont randomize in size they just stay the same size. Please help!

    This is my Class:
    Java Code:
    import javax.swing.JApplet;
    import java.awt.*;
    
    public class Circle {
    
       // instance variables
       private int diameter;
       private Color circleColor;
       
       // default constructor
       public Circle() {
       diameter = 50;
       circleColor = Color.black;
       }
       
       // constructor
       public Circle(int d, Color c) {
       diameter = d;
       circleColor = c;
       }
       
       // accessors
       public int getDiameter() {
          return diameter;
       }
       public Color getCircleColor() {
          return circleColor;
       }
       
       // mutators
       public void setDiameter( int a ) {
          diameter = a;
       }
       public void setCircleColor( Color b) {
          circleColor = b;
       }
       
       // draw method
       public void draw(int x, int y, Graphics g) {
       int width = diameter;
       int height = diameter;
       g.setColor(circleColor);
       g.drawOval(x, y, width, height);
       }
       
    }// end class

    This is my Main:
    Java Code:
    import java.awt.*;
    import javax.swing.JApplet;
    import java.util.Random;
    
    public class Lab7A extends JApplet {
       public void paint (Graphics g) {
    
    //create a default Circle object
    Circle circle = new Circle();
          
          Random random = new Random();
    		for (int i=0; i<100; i++) {
    			Color color = Color.black;
    			int diameter = 50;
    			int x = random.nextInt(getWidth());
    			int y = random.nextInt(getHeight());
    			circle.setCircleColor(color);
    			circle.setDiameter(diameter);
    			circle.draw(x, y, g);
    		}
    
    
          Random random2 = new Random();
    		for (int i=0; i<100; i++) {
    			int diameter = 50;
    			int x = random2.nextInt(getWidth());
    			int y = random2.nextInt(getHeight());
    			int red = random2.nextInt(256);
    			int green = random2.nextInt(256);
    			int blue = random2.nextInt(256);
    			Color color = new Color(red, green, blue);
    			circle.setCircleColor(color);
    			circle.setDiameter(diameter);
    			circle.draw(x, y, g);
    		}
    
       }
    
    }//end main

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Trouble with using Random method

    You are passing in random for your x and y, but always setting the diameter equal to 50.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Valerie10b is offline Member
    Join Date
    Mar 2013
    Posts
    18
    Rep Power
    0

    Default Re: Trouble with using Random method

    can you give me an idea on what i can possibly do to fix that please. I have no idea

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Trouble with using Random method

    Quote Originally Posted by Valerie10b View Post
    can you give me an idea on what i can possibly do to fix that please. I have no idea
    Step through your code (either with a debugger or with a piece of paper and a pencil) to understand what's going on. Because right now your logic doesn't really make sense: what is diameter each time? Why are you setting it in the paint method? Why are you looping at all?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 5
    Last Post: 10-24-2012, 09:17 PM
  2. Trouble with a Map Method
    By javadum in forum New To Java
    Replies: 3
    Last Post: 07-16-2012, 02:51 AM
  3. Trouble ranging random numbers
    By SteroidalPsycho in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 12:00 AM
  4. Trouble with Random number generation
    By SteroidalPsycho in forum New To Java
    Replies: 9
    Last Post: 11-16-2009, 11:15 PM
  5. Trouble with method
    By BlueJ2008 in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 09:05 PM

Posting Permissions

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