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

// Animator for a BallList
public class ColorfulBallMover extends ActiveObject {

        private static final int PAUSETIME = 20;
        private static final double SPEED = 1;

        private AudioClip noise;

        // the list of balls to animate
        private BallListInterface ballList;

        // distance that each ball should move
        private int distanceToMove;

        public ColorfulBallMover(BallListInterface aList, int distance, AudioClip theNoise) {
                ballList = aList;
                distanceToMove = distance;
                noise = theNoise;

                start();
        }

        private void moveOneBall(FilledOval ball) {
                double pixelsMoved = 0;
                while (pixelsMoved < distanceToMove) {
                        ball.move(SPEED, 0);
                        pause(PAUSETIME);
                        pixelsMoved = pixelsMoved + SPEED;
                }
        }

        public void run() {
                RandomIntGenerator gen = new RandomIntGenerator(0, 255);
                while (!ballList.isEmpty()) {
                        FilledOval nextBall = ballList.getFirst();
                        Color c = new Color(gen.nextValue(), gen.nextValue(), gen.nextValue());
                        nextBall.setColor(c);
                        noise.play();
                        moveOneBall(nextBall);
                        ballList = ballList.getRest();
                }
        }
}
