colorfulballmover.java

来自「书籍"Java_面向事件编程"的附带光盘代码」· Java 代码 · 共 50 行

JAVA
50
字号
import objectdraw.*;import java.awt.*;import java.applet.*;// Animator for a BallList, changes colors while movingpublic 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();    }       // move the specified ball    private void moveOneBall(FilledOval ball) {        double pixelsMoved = 0;        while (pixelsMoved < distanceToMove) {            ball.move(SPEED, 0);            pause(PAUSETIME);            pixelsMoved = pixelsMoved + SPEED;        }    }        // traverse the list, move each ball once, and change its color    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();        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?