使用图象:
现在,我们将重写paintFrame()来使图象动起来。这也就带来一些
问题,图象往往相当大,被一点点调入,将图象全部画出将花费很多时间,尤其
是通过一个较慢的连接,这也就是为什么drawImage带四个参数的原因,其中
第四个参数为一个ImageObserver对象。通过调用getImage()得到图象。
在屏幕上移动一幅图象:
world.gif作为背景,car.gif作为移动物体,且被绘制了两次,造成
一个两辆车比赛的场景。
Image world;
Image car;
public void init() {
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 10;
delay = (fps > 0) ? (1000 / fps) : 100;
world = getImage(getCodeBase(), "world.gif");
car = getImage(getCodeBase(), "car.gif");
}
public void paint(Graphics g) {
update(g);
}
public void paintFrame(Graphics g) {
Dimension d = size();
int w = world.getWidth(this);
int h = world.getHeight(this);
if ((w > 0) && (h > 0)) {
g.drawImage(world, (d.width - w)/2, (d.height - h)/2,
this);
}
w = car.getWIdth(this);
h = car.getHeight(this);
if ((w > 0) && (h > 0)) {
w += d.width;
g.drawImage(car, d.width - ((frame * 5) % w),
(d.height - h)/3, this);
g.drawImage(car, d.width - ((frame * 7) % w),
(d.height - h)/2, this);
}
}
显示一系列图象:
通过每一帧显示一幅图象来创建动画。我们仍用双缓冲的方法减小
闪烁。原因是我们显示的每一幅图象有一部分是透明的,因此需要在显示下
一幅前擦除当前的,如果不使用双缓冲的技术将导致闪烁。
Image frames[];
public void init() {
String str = getParameter("fps");
int fps = (str != null) ? Integer.parseInt(str) : 10;
delay = (fps > 0) ? (1000 / fps) : 100;
frames = new Image[10];
for (int i = 0; i < 10; i++) {
frames[i] = getImage(getCodeBase(), "duke/T" + i +
".gif");
}
}
public void paint(Graphics g) {
update(g);
}
public void paintFrame(Graphics g) {
g.drawImage(frames[frame % 10], 0, 0, null);
}
Java动画编程基础第四部分
最新推荐文章于 2020-04-27 22:20:01 发布
本文介绍了使用Java编程实现图像动画的方法。包括重写paintFrame()使图像动起来,解决图像加载慢的问题;在屏幕上移动图像,营造两辆车比赛的场景;还通过显示一系列图像创建动画,采用双缓冲技术减小闪烁。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
EmotiVoice
AI应用
EmotiVoice是由网易有道AI算法团队开源的一块国产TTS语音合成引擎,支持中英文双语,包含2000多种不同的音色,以及特色的情感合成功能,支持合成包含快乐、兴奋、悲伤、愤怒等广泛情感的语音。
1907

被折叠的 条评论
为什么被折叠?



