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

📄 sharkprey.java

📁 Java程序设计技巧与开发实例附书源代码。
💻 JAVA
字号:

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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -