Java教程-在Applet中的动画

在Applet中的动画
Applet通常用于游戏和动画。为此,需要移动图像。
在applet中实现动画的示例:
import java.awt.*;
import java.applet.*;
public class AnimationExample extends Applet {
Image picture;
public void init() {
picture =getImage(getDocumentBase(),"bike_1.gif");
}
public void paint(Graphics g) {
for(int i=0;i<500;i++){
g.drawImage(picture, i,30, this);
try{Thread.sleep(100);}catch(Exception e){}
}
}
}
在上面的示例中,使用Graphics类的drawImage()方法来显示图像。drawImage()方法的第四个参数是ImageObserver对象。Component类实现了ImageObserver接口。因此,当前类对象也将被视为ImageObserver,因为Applet类间接地扩展了Component类。
myapplet.html
<html>
<body>
<applet code="DisplayImage.class" width="300" height="300">
</applet>
</body>
</html>