/* * Header comments go here */ import acm.graphics.*; import acm.program.*; import acm.util.*; import java.awt.*; import myLibrary.graphics.*; public class PlayPool extends GraphicsProgram { public void run() { // Add 3 circles for (int i = 0; i < 3; i++) { // Choose the "corner" of the circle double x = rgen.nextDouble(0, getWidth() - SIZE); double y = rgen.nextDouble(0, getHeight() - SIZE); // Initial direction of movement double angle = rgen.nextDouble(0.0, 2 * Math.PI); // Get the "speed" of movement double speed = rgen.nextDouble(MINIMUM_SPEED, MAXIMUM_SPEED); // Set up motion as determined by direction and speed double deltax = speed * Math.cos(angle); double deltay = speed * Math.sin(angle); // Draw the new moving circle add(new GMovingCircle(x, y, SIZE, rgen.nextColor(), deltax, deltay)); } // Make MAX_MOVES number of moves for (int i = 0; i < MAX_MOVES; i++) { // For every object in our window... for (int j = 0; j < getElementCount(); j++) { // the jth object GObject gObject = getElement(j); // Make sure it is a moving circle if (gObject instanceof GMovingCircle) { // Since we have a circle we cast it as such. GMovingCircle ball = (GMovingCircle) gObject; /******************************************************************* * Add and modify code here to remove the ball from the window if * it has been captured by a pocket. *******************************************************************/ ball.move(); } } pause(SLEEPTIME); } } // CONSTANT AND GLOBAL OBJECTS DECLARATION SECTION // A delay time for animation private static final double SLEEPTIME = 20; // Number of moves to make private static final int MAX_MOVES = 1000; // attributes of a pool ball: // size of a pool ball private static final double SIZE = 35; // Bounds on pool ball velocity private static final double MINIMUM_SPEED = 5; private static final double MAXIMUM_SPEED = 10; // A random generator object private RandomGenerator rgen = RandomGenerator.getInstance(); public static void main(String args[]) { (new PlayPool()).start(); } }