import java.awt.*; import java.util.Random; import java.text.Format; public class hw1 extends BufferedApplet { private final int NUM_CIRCLES = 20; private final int CIRCLE_RADIUS = 20; int w = 0, h = 0; Circle circles[] = new Circle[NUM_CIRCLES]; //going to be triangle covering bottom half of screen Polygon triangle; int trianglePointsX[] = {0,0,0}; int trianglePointsY[] = {0,0,0}; long time0; int lastCount = 0; //first time initialization private void init(Graphics g) { time0 = System.currentTimeMillis(); //start the clock w = bounds().width; h = bounds().height; trianglePointsX[2] = w; trianglePointsY[1] = h; trianglePointsY[2] = h; triangle = new Polygon(trianglePointsX, trianglePointsY, 3); //initialize the circles for (int i=0; i= h) //off the screen? c.setStartingPlace(w, h); //System.err.printf("Circle #%d position: (%d, %d)\n", i, c.x, c.y); } } private void setBackground(Graphics g, Color c) { g.setColor(c); g.fillRect(0, 0, w, h); } //helps create and render a circle class Circle { Graphics g; Random random = new Random(); int r; int speed; public int x, y; Circle(Graphics g, int r, int x, int y) { this.g = g; this.r = r; this.x = x; this.y = y; this.speed = random.nextInt(3)+1; } //randomize the position based on screen height/width public void setStartingPlace(int screenWidth, int screenHeight) { this.x = screenWidth + random.nextInt(150); //lower y value is higher on screen this.y = random.nextInt(screenHeight) - screenHeight/3; } public void move(int dx, int dy) { this.x += dx * this.speed; this.y += dy * this.speed; } public void render() { g.drawOval(x, y, r*2, r*2); } } }