// // FILE: MultiBallWorldFrame.java // AUTHOR: Eugene Wallingford // DATE: 2012/10/11 // COMMENT: a frame with a bunch of moving balls and a button // that causes the balls to move // // MODIFIED: 2012/10/28 -- Eugene Wallingford // CHANGE: modified to demonstrate generic ball decorator // added factory methods for different kinds of ball // import java.awt.Button; import java.awt.Frame; import java.awt.Graphics; public class MultiBallWorldFrame extends CloseableFrame { private static final int FrameWidth = 600; private static final int FrameHeight = 400; private static final int Iterations = 2000; private Ball[] ball; private int counter; public MultiBallWorldFrame( int numberOfBalls ) { super(); setSize ( FrameWidth, FrameHeight ); setTitle( "Multi Ball World" ); ball = new Ball[numberOfBalls]; for (int i = 0; i < numberOfBalls; i++) { int dx = (int) (20 * Math.random() ); int dy = (int) (10 * Math.random() ); // ball[i] = makeDeceleratingBall( dx, dy ); // case 1 // ball[i] = makeExpandingBall( dx, dy ); // case 2 ball[i] = makeComboBall( dx, dy ); // case 3 } counter = 0; } public void paint( Graphics g ) { for (int i = 0; i < ball.length; i++) { ball[i].paint( g ); ball[i].move(); } counter++; if ( counter >= Iterations ) System.exit( 0 ); repaint(); } protected Ball makeExpandingBall( int dx, int dy ) { return new ExpandingBall( new BoundedBall( 100, 100, 10, dx, dy, this ) ); } protected Ball makeDeceleratingBall( int dx, int dy ) { return new DeceleratingBall( new BoundedBall( 100, 100, 10, dx, dy, this ) ); } protected Ball makeComboBall( int dx, int dy ) { return new ExpandingBall( new DeceleratingBall( new BoundedBall(100, 100, 10, dx, dy, this ))); } }