We are going to create 1000 circles in random positions on the screen. Circles closer to the bottom-left corner will be blue and circles closer to the top-right corner will be green. This is an example of return values.
Each circle is 20 pixels by 20 pixels.
public class CircleRandom2 extends GraphicsProgram {
private RandomGenerator rd = new RandomGenerator();
public static final int APPLICATION_WIDTH = 500;
public static final int APPLICATION_HEIGHT = 500;
private static final int SIZE = 20;
public void run() {
for(int i = 0; i < 1000; i++) {
int x = rd.nextInt(getWidth()-SIZE);
int y = rd.nextInt(getHeight()-SIZE);
GOval o = new GOval(x, y, SIZE, SIZE);
o.setFilled(true);
o.setColor(getColor(x, y));
add(o);
pause(2);
}
}
private Color getColor(int x, int y) {
if(isGreen(x, y)) {
return Color.green;
}
return Color.blue;
}
private boolean isGreen(int x, int y) {
return x > y;
}
}