import java.awt.*; public class Bouncer extends SimpleAnimationApplet { double redCenterX, redCenterY; double blueCenterX, blueCenterY; double greenCenterX, greenCenterY; double redVelocityX, redVelocityY; double blueVelocityX, blueVelocityY; double greenVelocityX, greenVelocityY; boolean starting = true; protected void drawFrame(Graphics g, int width, int height) { g.setColor(Color.Light yellow); g.fillRect(0,0,width,height); if (starting) { initializeRedCircle(width,height); initializeBlueCircle(width,height); initializeGreenCircle(width,height); starting = false; } g.setColor(Color.red); g.fillOval((int)redCenterX - 25, (int)redCenterY - 25, 50, 50); g.setColor(Color.blue); g.fillOval((int)blueCenterX - 25, (int)blueCenterY - 25, 50, 50); g.setColor(Color.green); g.fillOval((int)greenCenterX - 25, (int)greenCenterY - 25, 50, 50); updateRedCircle(width,height); updateBlueCircle(width,height); updateGreenCircle(width,height); g.setColor(Color.black); g.drawRect(0,0,width-1,height-1); } void initializeRedCircle(int width, int height) { redCenterX = width / 2; redCenterY = height / 2; do { redVelocityX = 12 * (Math.random() - 0.5); } while (Math.abs(redVelocityX) < 3); do { redVelocityY = 12 * (Math.random() - 0.5); } while (Math.abs(redVelocityY) < 3); } void updateRedCircle(int width, int height) { redCenterX += redVelocityX; redCenterY += redVelocityY; if (redCenterX - 25 <= 0) redVelocityX = Math.abs(redVelocityX); if (redCenterX + 25 >= width) redVelocityX = - Math.abs(redVelocityX); if (redCenterY - 25 <= 0) redVelocityY = Math.abs(redVelocityY); if (redCenterY + 25 >= width) redVelocityY = - Math.abs(redVelocityY); } void initializeBlueCircle(int width, int height) { blueCenterX = width /9 ; blueCenterY = height /9 ; do { blueVelocityX = 10 * (Math.random() - 0.5); } while (Math.abs(blueVelocityX) < 3); do { blueVelocityY = 10 * (Math.random() - 0.5); } while (Math.abs(blueVelocityY) < 3); } void updateBlueCircle(int width, int height) { blueCenterX += blueVelocityX; blueCenterY += blueVelocityY; if (blueCenterX - 25 <= 0) blueVelocityX = Math.abs(blueVelocityX); if (blueCenterX + 35 >= width) blueVelocityX = - Math.abs(blueVelocityX); if (blueCenterY - 25 <= 0) blueVelocityY = Math.abs(blueVelocityY); if (blueCenterY + 18 >= width) blueVelocityY = - Math.abs(blueVelocityY); } void initializeGreenCircle(int width, int height) { greenCenterX = width / 6; greenCenterY = height / 6; do { greenVelocityX = 16 * (Math.random() - 0.5); } while (Math.abs(greenVelocityX) < 3); do { greenVelocityY = 16 * (Math.random() - 0.5); } while (Math.abs(greenVelocityY) < 3); } void updateGreenCircle(int width, int height) { greenCenterX += greenVelocityX; greenCenterY += greenVelocityY; if (greenCenterX - 25 <= 0) greenVelocityX = Math.abs(greenVelocityX); if (greenCenterX + 25 >= width) greenVelocityX = - Math.abs(greenVelocityX); if (greenCenterY - 25 <= 0) greenVelocityY = Math.abs(greenVelocityY); if (greenCenterY + 25 >= width) greenVelocityY = - Math.abs(greenVelocityY); } }