Posted on Leave a comment

Circles

I am so proud of myself that I figured it out how to fit the circles on the window. I was stuck for a while but I finally figured it out today.

[code lang=”java”]/**
* Circle.java – Make random circles
* @author rafiks
*/

import java.awt.Color;

import acm.graphics.*;
import acm.program.*;
import acm.util.*;

public class Circle extends GraphicsProgram {
// Constants
// number of circles
private static final int NUM_CIRCLES=15;

//minimum radius
private static final int MIN_RAD=20;

//max radius
private static final int MAX_RAD=200;

//seed for troubleshooting
//private static final int SEED=1;

public void run(){

//rgen.setSeed(SEED);

//for loop to generate the circles base on NUM_CIRCLES
for(int i=0;i<NUM_CIRCLES;i++){
//random radius set wit MIN_RAD and MAX_RAD
int radius = rgen.nextInt(MIN_RAD, MAX_RAD);
// set x so that it fits the window
int x = rgen.nextInt(radius,getWidth()-radius);
// set y so that it fits the window
int y = rgen.nextInt(radius,getHeight()-radius);
// set a random color
Color c = rgen.nextColor();
//draw the circle
drawCircle(x,y,radius,c);
}
}

/* Method to Draw a circle base on the parameters */
private void drawCircle(int x,int y,int radius,Color c){
GOval circle = new GOval(x,y,radius,radius);
circle.setFilled(true);
circle.setColor(c);
circle.setFillColor(c);
add(circle);
}

/** private instance variable */
private RandomGenerator rgen = RandomGenerator.getInstance();
}
[/code]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.