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

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

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

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

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

        public BallMover(BallListInterface aList, int distance) {
                ballList = aList;
                distanceToMove = distance;

                start();
        }

        // Move the parameter distanceToMove pixels to the right
        // in an animated way
        private void moveOneBall(FilledOval ball) {
                double pixelsMoved = 0;
                while (pixelsMoved < distanceToMove) {
                        ball.move(SPEED, 0);
                        pause(PAUSETIME);
                        pixelsMoved = pixelsMoved + SPEED;
                }
        }

        // Traverse the ballList, moving each ball in turn
        public void run() {
                while (!ballList.isEmpty()) {
                        FilledOval nextBall = ballList.getFirst();
                        moveOneBall(nextBall);
                        ballList = ballList.getRest();
                }
        }
}
