import objectdraw.*;
import java.applet.*;
import java.awt.*;

// Andrea Danyluk, March 2002.
// On a mouse press, the user can place balls on the canvas;
// On exiting the window, a simulation of a "chain reaction" begins:
// the most recently drawn ball hits into the second most recently drawn,
// which hits into the next ball, and so on.

public class ChainReaction extends WindowController {

        // x and y coordinates of the first ball drawn
        private static final int BALL_X = 300;
        private static final int BALL_Y = 100;

        // ball diameter
        private static final int SIZE = 10;

        // distance between balls
        private static final int DISP = 2 * SIZE;

        // x coordinate of the next ball to be drawn
        private int ballX = BALL_X;

        // the list of all balls
        private BallListInterface ballList;

        private AudioClip noise;

        public void begin() {
                // list of balls is empty at first
                ballList = new EmptyBallList();
                noise = getAudio("pop.wav");
        }


        public void onMousePress(Location point) {
                FilledOval ball = new FilledOval(ballX, BALL_Y, SIZE, SIZE, canvas);
                ballList = new NonEmptyBallList(ball, ballList);
                // update x coordinate for the next ball to be drawn
                ballX = ballX - DISP;
        }

        // cause the balls to begin hitting into each
        //  other in a "chain reaction"
        public void onMouseExit(Location point) {
                if (point.getY() < canvas.getHeight()) {
                        new BallMover(ballList, SIZE);
                } else {
                        new ColorfulBallMover(ballList, SIZE, noise);

                }

        }
}
