// // Multiple Ball version of the Ball World Game // Described in Chapter 5 of // Understanding Object-Oriented Programming with Java // by Timothy A Budd // Published by Addison-Wesley // // see ftp://ftp.cs.orst.edu/pub/budd/java/ReadMe.html // for further information // import java.awt.*; public class MultiBallWorld extends Frame { public static void main (String [ ] args) { MultiBallWorld world = new MultiBallWorld (Color.red); world.show (); } private static final int FrameWidth = 600; private static final int FrameHeight = 400; private Ball [ ] ballArray; private static final int BallArraySize = 6; private int counter = 0; private MultiBallWorld (Color ballColor) { // constructor for new ball world // resize our frame setSize (FrameWidth, FrameHeight); setTitle ("Ball World"); // initialize object data field ballArray = new Ball [ BallArraySize ]; for (int i = 0; i < BallArraySize; i++) { ballArray[i] = new Ball(10, 15, 5); ballArray[i].setColor (ballColor); ballArray[i].setMotion (3.0+i, 6.0-i); } } public void paint (Graphics g) { for (int i = 0; i < BallArraySize; i++) { ballArray[i].paint (g); // then move it slightly ballArray[i].move(); if ((ballArray[i].x() < 0) || (ballArray[i].x() > FrameWidth)) ballArray[i].setMotion (-ballArray[i].xMotion(), ballArray[i].yMotion()); if ((ballArray[i].y() < 0) || (ballArray[i].y() > FrameHeight)) ballArray[i].setMotion (ballArray[i].xMotion(), -ballArray[i].yMotion()); } // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(1); else System.exit(0); } }