sharkprey.java

来自「Java程序设计技巧与开发实例附书源代码。」· Java 代码 · 共 43 行

JAVA
43
字号

import java.awt.*;

public class SharkPrey
{
    private SharkAttack applet = null;
    private Image animal = null;
    private Rectangle bounds = new Rectangle();
    private int deltaX, amp;

    public SharkPrey(Image _animal, SharkAttack _applet)
    {
        animal = _animal;
        applet = _applet;
        deltaX = (int) (2 * Math.random()) + 1;
        amp = (int) (55 * Math.random()) + 10;
        bounds.x = (int) (applet.getSize().width * Math.random());
        bounds.y = 120 + (int) (amp * Math.sin(bounds.x / 20.0));
        bounds.height = animal.getHeight(null);
        bounds.width = animal.getWidth(null);
    }

    public void move()
    {
        if ( (bounds.x > applet.getSize().width) || (bounds.x < 0))
        {
            deltaX *= ( -1);
        }
        bounds.x += deltaX;
        bounds.y = 120 + (int) (amp * Math.sin(bounds.x / 20.0));
    }

    public boolean isEatenBy(Shark shark)
    {
        return bounds.contains(shark.getTip());
    }

    public void paintComponent(Graphics g)
    {
        g.drawImage(animal, bounds.x, bounds.y, null);
    }
}

⌨️ 快捷键说明

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