2

Hiya I'm doing a screensaver in BlueJ .. and i have made few circles but need to make a loop so that new circles get created at random points although i m not sure how to do it.. this is how much i have done.

public class ScreenSaver
     {
    // instance variables - replace the example below with your own
    private Circle a;
    private Circle b;
    private Circle c;
    private Circle d;

    /**
     * Constructor for objects of class ScreenSaver
     */
    public ScreenSaver()
       {
        // initialise instance variables
        //x = 0;
       }
    public void draw()
       {
        a = new Circle();
        a.moveVertical(70);
        a.changeSize(70);
        a.slowMoveVertical(-100);
        a.makeVisible();

        b= new Circle();
        b.changeColor("red");
        b.moveHorizontal(30);
        b.makeVisible();
        b.slowMoveVertical(-100);
        b.slowMoveVertical(100);

       } 

How do I get to make loops so that new circles get created at random points?

doelleri
  • 19,232
  • 5
  • 61
  • 65
  • First of all, sorry that you have to use bluej. Why don't you try a few things, then if you are still stuck go to the CS lab. – aglassman Dec 10 '12 at 23:40
  • *"I'm doing a screensaver"* You're about 7 years late for SaverBeans (the Java screensaver API). – Andrew Thompson Dec 11 '12 at 03:46
  • See [this answer](http://stackoverflow.com/questions/13795236/get-mouse-detection-with-a-dynamic-shape/13796268#13796268) for creating multiple ellipses. – Andrew Thompson Dec 11 '12 at 03:48

3 Answers3

0

I'll try to answer based solely on "what's closest to what you have right now" and that would be to enclose the code in draw() in a while loop that goes on and on until something happens.

To make the circles appear in random places, you could create a new java.util.Random() just before the while loop and call its nextInt(int) method in every iteration to get a pseudo random integer and force it to be in your desired range.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
0

Use class Random:

Random random = new Random();
int x = random.nextInt(screenWith);
int y = random.nextInt(screenyheight);
int radius = minradius + random.nextInt(50); 
AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

I am an advocate for loops like this:

while(true)
{
    //code goes here
    if (<something happens>)
    {
        break;
    }
}

What this does is it keeps going forever and then breaks the loop when the if statement comes true. You have have this as a waitForClick() method or something along those lines. As well, it has been stated to use the Random class for the random points. Just my suggestion!

I hope this helps :)

Nick
  • 35
  • 6