fallingobject.java

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

JAVA
42
字号
import objectdraw.ActiveObject;import objectdraw.Drawable2DInterface;// FallingObject class to encapsulate the behavior of ActiveObjects that// fall until they reach a certain y-position then disappearpublic class FallingObject extends ActiveObject {        protected static final int DELAY_TIME = 33;        // Fall until we get to this position    protected int fallToPos;        // how fast?    protected double ySpeed;        // what is falling?    protected Drawable2DInterface object;        // Constructor    // Parameters: thePos - how far down the object should fall    //             theSpeed - how far to fall each time around    public FallingObject(int thePos, double theSpeed) {                ySpeed = theSpeed;        fallToPos = thePos;                // note that it is the responsibility of the derived class        // to call start();    }        // The run method    public void run() {                while (object.getY() < fallToPos) {                        pause(DELAY_TIME);            object.move(0, ySpeed);        }        object.removeFromCanvas();    }}

⌨️ 快捷键说明

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