// // 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 // // Two constructor variant (bp) // // compare calling // // java TwoConsBallWorld // // to // // java TwoConsBallWorld default // import java.awt.*; public class TwoConsBallWorld extends Frame { public static void main (String [ ] args) { TwoConsBallWorld world; if ((args.length > 0) && (args[0].equals("default"))) world = new TwoConsBallWorld(); else world = new TwoConsBallWorld(Color.red); world.show (); } protected static final int FrameWidth = 600; protected static final int FrameHeight = 400; protected Ball aBall; protected int counter = 0; public TwoConsBallWorld (Color ballColor) { // constructor for new ball world // resize our frame setSize (FrameWidth, FrameHeight); setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setColor (ballColor); aBall.setMotion (1.0, 2.0); } public TwoConsBallWorld () { // constructor for new ball world // resize our frame setSize (FrameWidth, FrameHeight); setTitle ("Ball World"); // initialize object data field aBall = new Ball (10, 15, 5); aBall.setMotion (1.0, 2.0); } public void paint (Graphics g) { // first, draw the ball aBall.paint (g); // then move it slightly aBall.move(); if ((aBall.x() < 0) || (aBall.x() > FrameWidth)) aBall.setMotion (-aBall.xMotion(), aBall.yMotion()); if ((aBall.y() < 0) || (aBall.y() > FrameHeight)) aBall.setMotion (aBall.xMotion(), -aBall.yMotion()); // finally, redraw the frame counter = counter + 1; if (counter < 2000) repaint(1); else System.exit(0); } }