Click to See Complete Forum and Search --> : Random Circles


PeteTheNotSoGr8
01-12-2005, 09:42 AM
My teacher gave me an assignment which she cannot complete. I have it working but not quite right. We were supposed make an applet that would generate n circles with random positions and random radii (n was input by the user).

However when I run my code I don't get the right number of circles. If anyone could help me out, since my teacher's code failed it would be much appreciated


import java.util.Random;
import java.awt.geom.Ellipse2D;
import java.awt.Graphics2D;

public class RandomCircle
{
public RandomCircle()
{}

public void maker(Graphics2D g2)
{
Random generator= new Random();
int x,y,radius;
x = generator.nextInt(250)+125;
y = generator.nextInt(250)+125;
radius = generator.nextInt(100)+20;
Ellipse2D.Double circle = new Ellipse2D.Double(x-radius, y-radius, radius*2, radius*2);
g2.draw(circle);
}
}


and


import javax.swing.JOptionPane;
import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Graphics2D;

public class RandCircleTest extends Applet
{

public void init()
{
String input = JOptionPane.showInputDialog("Please input the number of circlees you wish to be displayed.");
n = Integer.parseInt(input);
}

public void paint(Graphics g)
{
Graphics2D g2 = (Graphics2D)g;
RandomCircle circle = new RandomCircle();

for(int a=0;a<=n;++a)
{
circle.maker(g2);
}
System.exit(0);
}
int n;
}

fatTrav
01-12-2005, 11:02 AM
Ok, I just woke up so bare with me..

I'd use a for-loop like this "for (int i = 0; i < n; i++)" isntead of the one you have. Mostly because I prefer the i++ and not ++i in most situations. (The difference between doing the loop action and then incrementing the counter and incrementing the counter and then doing the loop action).

Have you put a debugging output statement in your code to see how many times it is being run? System.out.println("Here I am x = " + x + " y = " + y) inside the maker method would be a good location. Maybe try that and see what happens?

bwkaz
01-12-2005, 08:48 PM
This is an applet, so there is no System.out stream... ;)

And AFAIK, ++i and i++ are exactly equivalent when used as the last item in a for loop. The reason is that the last item is never evaluated, only executed, and the only difference between ++i and i++ is what gets returned when they're evaluated. They both "do" the same thing.

However, the problem is the a<=n test. Change that to a<n, and it'll work (fatTrav, you did have that in your code, but you didn't comment on it so I'm not sure if that was just reflex or what). This is a fairly-classic off-by-one bug. :)

fatTrav
01-12-2005, 09:01 PM
sorry, my mistake. it is in a while-loop where i++ and ++i make a difference. I figured the subtle difference would carry over in a for-loop as well.

PeteTheNotSoGr8
01-13-2005, 09:45 AM
but I am not off by one

when I put in 5 I get 2 or 1 circles

this leads me to believe that the counter isn't off by one

the only thing I can think is that the variable is somehow changed by the program (maybe the draw)

bwkaz
01-13-2005, 08:03 PM
Well... the counter is still off by one, but there may be some other issues also. :p

Are you sure you only get 1 or 2 circles? Are you sure the later circles aren't just painting overtop of earlier ones?

Doesn't Java have some kind of function you can call to throw up a message? Maybe you could call that each time through the maker() method, to see what's going on in a little more detail.

MadNewbie
01-14-2005, 04:17 AM
I think your problem is that the circles draw on top of each other.

Try declaring the random object outside the maker method and in the constructor instead, so you dont make a new random object for each new circle you are drawing.

bwkaz
01-14-2005, 08:46 PM
D'oh! I saw that problem in the last java.util.Random post that PeteTheNotSoGr8 made, but not this one... I must be losing it. ;)

The random numbers are exactly the same each time, so the circles draw on top of each other.