I am currently taking my an AP Java Coding class and I am running into an intersting problem.
I am trying to create 100 random circles with various radii, and random colors in a graphics window using GObjects/GOval. I have tried to isolate the problem and I am certain that something with the communication between the for-loop and the GOval (circle) creation. I also have tried to redo this code several times from scratch but I keep running into the same problem. Specifically my problem is that my graphics windows only displaying one random circle and not 100. Please, please help. My code is below:
Please note that I chose the variable c to dictate the color randomly. There is no rhyme or reason, I just needed a random value to be used.
import java.awt.Color;
import acm.graphics.GOval;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;
public class _100_Random_Circles extends GraphicsProgram
{
public _100_Random_Circles()
{
// Random Number Generator
RandomGenerator rgen = new RandomGenerator();
// Random X-coordinate.
int x = rgen.nextInt(1, 500);
// Random Y-coordinate.
int y = rgen.nextInt(1, 500);
// Random Circle width
int c = rgen.nextInt(1, 100);
// Random Circle height
int d = rgen.nextInt(1, 100);
for(int i = 0; i < 100; i++)
{
GOval circle = new GOval (x, y, c, d);
add(circle);
//Color the circles randomly
if(c <= 10)
{
circle.setFilled(true);
circle.setColor(Color.BLUE);
}
else if(c <= 20)
{
circle.setFilled(true);
circle.setColor(Color.RED);
}
else if(c <= 30)
{
circle.setFilled(true);
circle.setColor(Color.YELLOW);
}
else if(c <= 40)
{
circle.setFilled(true);
circle.setColor(Color.GREEN);
}
else if(c <= 50)
{
circle.setFilled(true);
circle.setColor(Color.ORANGE);
}
else if(c <= 60)
{
circle.setFilled(true);
circle.setColor(Color.BLACK);
}
else if(c <= 70)
{
circle.setFilled(true);
circle.setColor(Color.GRAY);
}
else if(c <= 80)
{
circle.setFilled(true);
circle.setColor(Color.PINK);
}
else if(c <= 90)
{
circle.setFilled(true);
circle.setColor(Color.MAGENTA);
}
else
{
circle.setFilled(true);
circle.setColor(Color.WHITE);
}
}
}
}
As I pointed out first in the comments.
Thats because your xycd definition is outside the loop. Then you have just one value for all circles