BallArray.java
This is the syntax highlighted version of BallArray.java
from 3.2 Data Types of
Introduction to Computer Science by
Robert Sedgewick and Kevin Wayne.
/*************************************************************************
* Compilation: javac BallArray.java
* Execution: java BallArray N
* Dependencies: ColoredBall.java StdDraw.java
*
* Creates an array of N balls and animates them.
*
* % java BallArray 10
*
* Remarks
* -------
* For some reason the compiler sometimes complains about the call
* to the method Ball.move. Is this a reserved name? Or compiler glitch?
*
*************************************************************************/
import java.awt.Color;
public class BallArray {
private ColoredBall[] balls;
// initialze an an array of N balls
BallArray(int N) {
balls = new ColoredBall[N];
for (int i = 0; i < N; i++)
balls[i] = new ColoredBall();
}
// move all of the balls
public void update() {
for (int i = 0; i < balls.length; i++)
balls[i].update();
}
// draw all of the balls
public void draw() {
for (int i = 0; i < balls.length; i++)
balls[i].draw();
}
// test client
public static void main(String[] args) {
int N = Integer.parseInt(args[0]); // number of bouncing balls
int SIZE = 512;
StdDraw.create(512, 512);
StdDraw.setScale(-1, -1, 1, 1);
BallArray balls = new BallArray(N);
// the animation loop
while(true) {
StdDraw.clear(Color.black);
balls.update();
balls.draw();
StdDraw.pause(20);
}
}
}
Last updated: Tue Jun 8 16:55:56 EDT 2004
.
Copyright © 2004, Robert Sedgewick and Kevin Wayne.