fallingcow.java

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

JAVA
64
字号
import objectdraw.*;import java.applet.*;import java.awt.*;// creates a falling cow imagepublic class FallingCow extends FallingObject {        private static final int COW_HEIGHT = 70;    private static final int COW_WIDTH = 100;    private static final int COW_PAUSE = 30;    private static final int COW_WALK_SPEED = -3;    private static final int COW_FALL_SPEED = 4;            private DrawingCanvas canvas;        // image for walking cow    private Image cow;        // be sure to import java.applet.awt    private AudioClip moo;        // Creates a falling purple cow    public FallingCow(DrawingCanvas aCanvas,                      Image upsideDownCow,                      Image aCow,                      AudioClip aMoo,                      double x, int groundHeight) {        // Create a generic falling object        super (groundHeight - COW_HEIGHT, COW_FALL_SPEED);                canvas = aCanvas;        cow = aCow;        moo = aMoo;                // Specialize it to be an upside down cow.        object = new VisibleImage(upsideDownCow, x,                                  -COW_HEIGHT, canvas);                // Start it falling        start();    }        protected void hitBottom() {        // play moooing noise        moo.play();                // let superclass do what it wants to        super.hitBottom();                // make cow walk back to the left        VisibleImage walkingCow =            new VisibleImage(cow, object.getLocation(), canvas);                while (walkingCow.getX() + COW_WIDTH > 0) {            pause(COW_PAUSE);            walkingCow.move(COW_WALK_SPEED, 0);        }                walkingCow.removeFromCanvas();    }}

⌨️ 快捷键说明

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