⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animationdecorator.java

📁 开源(Open Source)项目JHotDraw的文档和源程序
💻 JAVA
字号:
/*
 * @(#)AnimationDecorator.java 5.2
 *
 */

package CH.ifa.draw.samples.javadraw;

import java.awt.*;
import java.util.*;
import java.io.IOException;
import CH.ifa.draw.framework.*;
import CH.ifa.draw.standard.*;
import CH.ifa.draw.util.*;


public class AnimationDecorator extends DecoratorFigure {

    private int fXVelocity;
    private int fYVelocity;

    /*
     * Serialization support.
     */
    private static final long serialVersionUID = 7894632974364110685L;
    private int animationDecoratorSerializedDataVersion = 1;

    public AnimationDecorator() { }

    public AnimationDecorator(Figure figure) {
        super(figure);
        fXVelocity = 4;
        fYVelocity = 4;
    }

    public void velocity(int xVelocity, int yVelocity) {
        fXVelocity = xVelocity;
        fYVelocity = yVelocity;
    }

    public Point velocity() {
        return new Point(fXVelocity, fYVelocity);
    }

    public void animationStep() {
	    int xSpeed = fXVelocity;
	    int ySpeed = fYVelocity;
	    Rectangle box = displayBox();

	    if ((box.x + box.width > 300) && (xSpeed > 0))
    		xSpeed = -xSpeed;

	    if ((box.y + box.height > 300) && (ySpeed > 0))
    		ySpeed = -ySpeed;

        if ((box.x < 0) && (xSpeed < 0))
            xSpeed = -xSpeed;

        if ((box.y < 0) && (ySpeed < 0))
            ySpeed = -ySpeed;

	    velocity(xSpeed, ySpeed);
	    moveBy(xSpeed, ySpeed);
	}

	// guard concurrent access to display box

	public synchronized void basicMoveBy(int x, int y) {
	    super.basicMoveBy(x, y);
	}

    public synchronized void basicDisplayBox(Point origin, Point corner) {
        super.basicDisplayBox(origin, corner);
    }

    public synchronized Rectangle displayBox() {
        return super.displayBox();
    }

    //-- store / load ----------------------------------------------

    public void write(StorableOutput dw) {
        super.write(dw);
        dw.writeInt(fXVelocity);
        dw.writeInt(fYVelocity);
    }

    public void read(StorableInput dr) throws IOException {
        super.read(dr);
        fXVelocity = dr.readInt();
        fYVelocity = dr.readInt();
    }

}

⌨️ 快捷键说明

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