绘 制 动 画 十 分 简 单, 只 需 遵 照 以 下 三 步 即 可;(1) 擦 去 整 个 小 程 序 绘 图 区;(2) 重 画 动 画 背 景;(3) 在 新 的 位 置 绘 制 图 形。 但 是, 当 动 画 连 续 帧 之 间 运 动 和 颜 色 不 连 续 时 就 会 发 生 闪 烁 现 象( 例 子 略)。 问 题 出 在 小 程 序 区 的 屏 幕 刷 新 上, 有 两 种 方 法 可 以 防 止 动 画 闪 烁。 第 一 种 方 法 是 只 做 局 部 刷 新, 即 每 次 只 擦 除 改 变 的 部 分。 例 如: 如 果 要 绘 制 一 幅" 飞 行 的 星 空" 动 画, 每 次 刷 新 操 作, 先 擦 去 前 一 位 置 的 星 星, 再 在 新 的 位 置 绘 制 一 个。 但 是, 如 果 动 画 比 较 复 杂, 运 动 部 分 重 叠 较 多, 则 局 部 刷 新 操 作 很 繁 琐 也 影 响 程 序 运 行 速 度。 在 这 种 情 况 下, 可 以 用 另 外 一 种 方 法( 双 缓 存) 来 解 决 闪 烁 问 题。 它 的 核 心 思 想 是 在 屏 幕 外 完 成 一 帧 的 全 部 动 画, 然 后 把 最 后 绘 制 完 的 帧 显 示 在 小 程 序 屏 幕 上。 过 程 如 图3 所 示。
示 例 程 序 如 下:
import java.awt.*;
public class ball extends multithreadapplet {
image ball=null;
image applet=null;
graphics appletg,ballg;
public void run( ){
if(ball= =null){
applet=createlmage(bounds( ).width,bounds( ).
height);
ball=create image(70,70);
appletg=applet.getgraphics( );
ballg=ball.getgraphics( );
ballg.setcolor(color.gray);
ballg.draworal(0,0,70,70); {
for(int x=0;x <400;x++) { double angle="((double)x)/20;" int y="(int)(math.abs(math.sin(angle))*80);" appletg.clearrect(0,0,bounds( ).width.bounds( ), helght); drawbackground(appletg); appletg.drawimage(ball,x.80-y,this); this.getgraphics( ).drawimage(applet,0,0,this); try { thread.sleep(25); } catch(exception ignored) {}} } private void drawbackground(graphics g) { for(int i="0;i" < 1;i++){ g.drawline(0.i*10,400,i*10); } } }
小 程 序 首 先 用createimage( ) 取 得 与 小 程 序 大 小 完 全 相 同 的 屏 外 图 形 缓 存, 赋 给 变 量applet, 然 后 得 到 缓 存 的 绘 图 对 象appletg。 以 后 对 帧 的 刷 新 操 作 过 程都 是 针 对appletg。 这 包 括 清 除 帧、 绘 制 背 景、 在 新 位 置 绘 制 图 形。最后再用drawimage ( ) 方 法 把 缓 存 复 制 到 小 程 序 的 屏 幕 显 示 区。 运 行 这 个 小 程 序, 你 会 发 现 动 画 非 常 平 滑, 不 存 在 闪 烁 现 象。
(2) 使用双缓冲技术
另一种减小帧之间闪烁的方法是使用双缓冲,它在许多动画applet中被使用。其主要原理是创建一个后台图像,将需要绘制的一帧画入图像,然后调用drawimage()将整个图像一次画到屏幕上去;好处是大部分绘制是离屏的,将离屏图像一次绘至屏幕上比直接在屏幕上绘制要有效得多,大大提高做图的性能。
双缓冲可以使动画平滑,但有一个缺点,要分配一张后台图像,如果图像相当大,这将需要很大一块内存;当你使用双缓冲技术时,应重载 update()。
下面举一个时钟的例子来说明如何处理动画
//animatordemo.java
import java.util.*;
import java.awt.*;
import java.applet.*;
import java.text.*;
public class animatordemo extends applet implements runnable
{
thread timer; // 用于显示时钟的线程
int lastxs, lastys, lastxm,
lastym, lastxh, lastyh;
simpledateformat formatter; //格式化时间显示
string lastdate; // 保存当前时间的字符串
font clockfacefont; //设置显示时钟里面的数字的字体
date currentdate; // 显示当前时间
color handcolor; // 用于显示时针、分针和表盘的颜色
color numbercolor; // 用于显示秒针和数字的颜色
public void init()
{
int x,y;
lastxs = lastys = lastxm = lastym = lastxh = lastyh = 0;
formatter = new simpledateformat ("yyyy eee mmm dd hh:mm:ss ");
currentdate = new date();
lastdate = formatter.format(currentdate);
clockfacefont = new font("serif", font.plain, 14);
handcolor = color.blue;
numbercolor = color.darkgray;
try {
setbackground(new color(integer.parseint(getparameter("bgcolor"),16)));
} catch (exception e) { }
try {
handcolor = new color(integer.parseint(getparameter("fgcolor1"),16));
} catch (exception e) { }
try {
numbercolor = new color(integer.parseint(getparameter("fgcolor2"),16));
} catch (exception e) { }
resize(300,300); // 设置时钟窗口大小
}
// 计算四分之一的圆弧
public void plotpoints(int x0, int y0, int x, int y, graphics g)
{
g.drawline(x0+x,y0+y,x0+x,y0+y);
g.drawline(x0+y,y0+x,x0+y,y0+x);
g.drawline(x0+y,y0-x,x0+y,y0-x);
g.drawline(x0+x,y0-y,x0+x,y0-y);
g.drawline(x0-x,y0-y,x0-x,y0-y);
g.drawline(x0-y,y0-x,x0-y,y0-x);
g.drawline(x0-y,y0+x,x0-y,y0+x);
g.drawline(x0-x,y0+y,x0-x,y0+y);
}
// 用bresenham算法来画圆,其中(x0,y0)是圆的中心,r为圆半径
public void circle(int x0, int y0, int r, graphics g)
{
int x,y;
float d;
x=0;
y=r;
d=5/4-r;
plotpoints(x0,y0,x,y,g);
while (y>x) {
if (d<0) {
d=d+2*x+3;
x++;
}
else {
d=d+2*(x-y)+5;
x++;
y–;
}
plotpoints(x0,y0,x,y,g);
}
}
public void paint(graphics g)
{
int xh, yh, xm, ym, xs, ys, s = 0, m = 10, h = 10, xcenter, ycenter;
string today;
currentdate = new date();
simpledateformat formatter = new simpledateformat("s",locale.getdefault());
try {
s = integer.parseint(formatter.format(currentdate));
} catch (numberformatexception n) {
s = 0;
}
formatter.applypattern("m");
try {
m = integer.parseint(formatter.format(currentdate));
} catch (numberformatexception n) {
m = 10;
}
formatter.applypattern("h");
try {
h = integer.parseint(formatter.format(currentdate));
} catch (numberformatexception n) {
h = 10;
}
formatter.applypattern("eee mmm dd hh:mm:ss yyyy");
today = formatter.format(currentdate);
//设置时钟的表盘的中心点为(80,55)
xcenter=80;
ycenter=55;
// a= s* pi/2 – pi/2 (to switch 0,0 from 3:00 to 12:00)
// x = r(cos a) + xcenter, y = r(sin a) + ycenter
xs = (int)(math.cos(s * 3.14f/30 – 3.14f/2) * 45 + xcenter);
ys = (int)(math.sin(s * 3.14f/30 – 3.14f/2) * 45 + ycenter);
xm = (int)(math.cos(m * 3.14f/30 – 3.14f/2) * 40 + xcenter);
ym = (int)(math.sin(m * 3.14f/30 – 3.14f/2) * 40 + ycenter);
xh = (int)(math.cos((h*30 + m/2) * 3.14f/180 – 3.14f/2) * 30 + xcenter);
yh = (int)(math.sin((h*30 + m/2) * 3.14f/180 – 3.14f/2) * 30 + ycenter);
//画时钟最外面的圆盘其中心在(xcenter,ycenter)半径为50
g.setfont(clockfacefont);
g.setcolor(handcolor);
circle(xcenter,ycenter,50,g);
//画时钟表盘里的数字
g.setcolor(numbercolor);
g.drawstring("9",xcenter-45,ycenter+3);
g.drawstring("3",xcenter+40,ycenter+3);
g.drawstring("12",xcenter-5,ycenter-37);
g.drawstring("6",xcenter-3,ycenter+45);
// 如果必要的话抹去然后重画
g.setcolor(getbackground());
if (xs != lastxs || ys != lastys) {
g.drawline(xcenter, ycenter, lastxs, lastys);
g.drawstring(lastdate, 5, 125);
}
if (xm != lastxm || ym != lastym) {
g.drawline(xcenter, ycenter-1, lastxm, lastym);
g.drawline(xcenter-1, ycenter, lastxm, lastym); }
if (xh != lastxh || yh != lastyh) {
g.drawline(xcenter, ycenter-1, lastxh, lastyh);
g.drawline(xcenter-1, ycenter, lastxh, lastyh); }
g.setcolor(numbercolor);
g.drawstring("", 5, 125);
g.drawstring(today, 5, 125);
g.drawline(xcenter, ycenter, xs, ys);
g.setcolor(handcolor);
g.drawline(xcenter, ycenter-1, xm, ym);
g.drawline(xcenter-1, ycenter, xm, ym);
g.drawline(xcenter, ycenter-1, xh, yh);
g.drawline(xcenter-1, ycenter, xh, yh);
lastxs=xs; lastys=ys;
lastxm=xm; lastym=ym;
lastxh=xh; lastyh=yh;
lastdate = today;
currentdate=null;
}
//applet的启动方法
public void start()
{
timer = new thread(this);
timer.start();
}
// applet的停止方法
public void stop()
{
timer = null;
}
//线程的run方法
public void run()
{
thread me = thread.currentthread();
while (timer == me) {
try {
thread.currentthread().sleep(1000);
}
catch (interruptedexception e) {
}
repaint();
}
}
//注意:这里重写了update()方法,只是调用了paint()方法来消除闪烁现象
public void update(graphics g)
{
paint(g);
}
}
下面是运行该applet 需要的animatordemo.html 的内容
<html>
<head>
<title>一个时钟的例子</title>
</head>
<body>
<hr>
<applet codebase="." align=middle code="animatordemo.class" width=200 height=150>
</applet>
</body>
</html>
