sharkprey.java

来自「java编程开发技巧与实例的编译测试通过的所有例程」· Java 代码 · 共 37 行

JAVA
37
字号
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));
	}
	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 + -
显示快捷键?