BouncingBalls.java


Below is the syntax highlighted version of BouncingBalls.java from §3.4 Case Study: N-Body Simulation.


/******************************************************************************
 *  Compilation:  javac BouncingBalls.java
 *  Execution:    java BouncingBalls n
 *  Dependencies: Ball.java StdDraw.java
 *
 *  Creates an array of n bouncing balls and animates them.
 *
 *  % java BouncingBalls 10
 *
 ******************************************************************************/

public class BouncingBalls {

    public static void main(String[] args) {

        // number of bouncing balls
        int n = Integer.parseInt(args[0]);

        // set boundary to box with coordinates between -1 and +1
        StdDraw.setXscale(-1.0, +1.0);
        StdDraw.setYscale(-1.0, +1.0);

        // create an array of n random balls
        Ball[] balls = new Ball[n];
        for (int i = 0; i < n; i++)
            balls[i] = new Ball();

        // do the animation loop
        StdDraw.enableDoubleBuffering();
        while (true) {

            // move the n balls
            for (int i = 0; i < n; i++) {
                balls[i].move();
            }

            // draw the n balls
            StdDraw.clear(StdDraw.GRAY);
            StdDraw.setPenColor(StdDraw.BLACK);
            for (int i = 0; i < n; i++) {
                balls[i].draw();
            }
            StdDraw.show();
            StdDraw.pause(20);
        }
    }
}


Copyright © 2000–2022, Robert Sedgewick and Kevin Wayne.
Last updated: Thu Aug 11 10:26:41 EDT 2022.