fallingtomato.java

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

JAVA
47
字号
import java.awt.Color;import objectdraw.DrawingCanvas;import objectdraw.FilledOval;// makes a falling tomatopublic class FallingTomato extends FallingObject {        // size of a tomato    private static final int SIZE = 40;    private static final int SPLATTER_DELAY = 1000;        private DrawingCanvas canvas;        // initialize the instance variables and start the active object    public FallingTomato(DrawingCanvas aCanvas, double x, double aSpeed, int aScreenHeight) {        // first, call "up" to the constructor of the FallingObject class        super(aScreenHeight - SIZE / 2, aSpeed);                canvas = aCanvas;                object = new FilledOval(x, -SIZE, SIZE, SIZE, canvas);        object.setColor(Color.RED);                start();    }        // we override the hitBottom method from FallingObject    // since we want extra functionality    protected void hitBottom() {                // first, remember our x position        double x = object.getX();                object.removeFromCanvas();                // but then a little extra        FilledOval splat =             new FilledOval(x - SIZE / 2, fallToPos, SIZE * 2,                            SIZE / 2, canvas);        splat.setColor(Color.red);        pause(SPLATTER_DELAY);        splat.removeFromCanvas();    }}

⌨️ 快捷键说明

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