Graphics 版 (精华区)

发信人: killest (矛盾), 信区: Graphics
标  题: Java小应用程序中的动画技术(2)
发信站: 紫 丁 香 (Sat May 23 09:58:20 1998), 转信

    一个简单的补救方法如下:
    public void run() {
    long tm = System.currentTimeMillis();
    while (Thread.currentThread() == animator) {
        repaint();
        try {

      tm += delay;
       Thread.sleep(Math.max(0,tm - System.currentTimeMillis
())); 
      }
        catch (InterruptedException e) {break;}
        frame++;
    }

    绘制每一帧
    为了产生动画效果,需要将每一帧图片绘出。在上面的示例中调
用了repaint()方法来绘出每一帧图片。事实上也可以如下绘制:
    public void paint(Graphics g) {
    g.setColor(Color.black);
    g.drawString("Frame " + frame, 0, 30);

    图形的生成
    动画中用到的图片可以用计算机生成。下例绘制了一个正弦曲线
的组合,对于每一个变量x画一条短竖线,全部竖线组成一个图形,并且
每帧图形并不相同。
    public void paint(Graphics g)
    Dimension d = size();
    int h = d.height / 2;
    for (int x = 0 ; x < d.width; x++) {
     int y1 = (int)((1.0 + Math.sin((x - frame) * 0.05)) * h
);
      int y2 = (int)((1.0 + math.sin((x + frame) * 0.05)) * 
h);
     g.DrawLine(x, y1, x, y2);
    }

    闪烁的避免
    上例所绘制的图形有些闪烁,其原因有两个:一是绘制每一帧花费
的时间太长(因为重绘时要求的计算量较大),二是在每次调用paint()
 前整个背景被清除,在进行下一帧的计算时,用户看到的是背景。清
除背景和绘制图形间的短暂时间被用户看见,就是闪烁。在PC上闪烁
比在X Window上明显,这是因为X Window的图像被缓存过,使得闪烁的
时间比较短。
    有两种办法可以明显地减弱闪烁:重载update() 或使用双缓冲。
    1.重载update()当AWT接收到一个小应用程序的重绘请求时,它就
调用Applet的update ()。按默认情况,update() 清除小应用程序的
背景,然后调用paint()。重载update(),将以前在paint() 中的绘图
代码包含在update()中,从而避免每次重绘时将整个区域清除。背景
不再自动清除需要在update() 中完成。在绘制新线之前将竖线擦除,
就完全消除了闪烁。
    public void paint(Graphics g)
    update(g);public void update(Graphics g)
    Color bg = getBackground();
    Dimension d = size();
    int h = d.height / 2;
    for (int x = 0; x < d.width; x++) {
      int y1 = (int)((1.0 + Math.sin((x - frame) * 0.05)) * 
h);
      int y2 = (int)((1.0 + Math.sin((x + frame) * 0.05)) * 
h);
        if (y1 > y2) {
       int t = y1;
       y1 = y2;
       y2 = t;
        }
        g.setColor(bg);
        g.drawLine(x, 0, x, y1);
        g.drawLine(x, y2, x, d.height);
        g.setColor(Color.black);
        g.drawLine(x, y1, x, y2);
    }

    2.双缓冲技术
    另一种减小帧之间闪烁的方法是使用双缓冲。双缓冲的主要原理
是创建一个后台图像, 将一帧画入图像,然后调用drawImage() 将整
个图像一次画到屏幕上去。它的优点是大部分绘制是离屏的,将离屏
图像一次绘至屏幕上比直接在屏幕上绘制要有效得多。
    双缓冲技术可以使动画平滑,但有一个缺点:要分配一张后台图像
,如果图像相当大,这将需要很大内存。
    使用双缓冲技术时,也应重载update()。
    Dimension offDimension;Image offImage;Graphics offGraphi
cs;public void upda te(Graphics g)
    Dimension d = size();
    if ((offGraphics == null) ‖(d.width != offDimension.wid
th)‖ (d.height !=o ffDimension.height))
    {
        offDimension = d;
        offImage = createImage(d.width, d.height);
        offGraphics = offImage.getGraphics();
    }
    offGraphics.setColor(getBackground());
    offGraphics.fillRect(0, 0, d.width, d.height);
    offGraphics.setColor(Color.black);
    paintFrame(offGraphics);
    g.drawImage(offImage, 0, 0, null);public void paint(Grap
hics g)
    if (offImage != null) {
        g.drawImage(offImage, 0, 0, null);
    }public void paintFrame(Graphics g)
    Dimension d = size();
    int h = d.height / 2;
    for (int x = 0; x < d.width; x++) {
        int y1 = (int)((1.0 + Math.sin((x - frame) * 0.05)) 
* h);
        int y2 = (int)((1.0 + Math.sin((x + frame) * 0.05)) 
* h);
        g.drawLine(x, y1, x, y2);
    }




--

                  oo                                                 
         il    bb    yy   il      ..... 与尔同消万古愁               
        i    bbb  ll  yyy   i                                        
     iii       bb    yy      iii                                     
                  oo                                                 

※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: victor.hit.edu.c]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.711毫秒