java编程开发技巧与实例的编译测试通过的所有例程
源代码在线查看: sharkprey.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));
}
public boolean isEatenBy(Shark shark)
{
return bounds.contains(shark.getTip());
}
public void paintComponent(Graphics g)
{
g.drawImage(animal, bounds.x, bounds.y, null);
}
}