现在的位置: 首页 > 综合 > 正文

java applet中双缓冲运用

2013年03月12日 ⁄ 综合 ⁄ 共 1128字 ⁄ 字号 评论关闭

很简单,看完例子就会明白

/*
*用十副图像(T1.gif~T10.gif)产生一个动画
*
*/

import java.applet.Applet;
import java.awt.HeadlessException;
import java.awt.*;

public class DoubleBuffer extends Applet
{
  
private Image[] imgs;
  
private Image imgBuf;

  
//屏幕外图像缓存
  private Graphics gBuf;

  
private int totalImages=10;
  
private int currentImage;
  
private int i=0;
 
  
public void init()
  
{
    imgBuf
=createImage(600,400);
    gBuf
=imgBuf.getGraphics();
    gBuf.setColor(Color.white);
    gBuf.fillRect(
0,0,600,400);

    imgs
=new Image[totalImages];
   
    
for(int i=0;i<totalImages;i++)
    
{
      imgs[i]
=getImage(getDocumentBase(),"T"+(i+1)+".gif");
    }

  }


  
public void start()
  
{
    currentImage
=0;
    gBuf.drawImage(imgs[currentImage],
0,0,this);
    currentImage
=1;
  }


  
public void paint(Graphics g)
  
{
    g.drawImage(imgBuf,
0,0,this);
    gBuf.fillRect(
00600400);
    gBuf.drawImage(imgs[currentImage], 
00this);
    currentImage 
= ++currentImage % 10;
    }

    
try {
      Thread.sleep(
500);
    }

    
catch (Exception ex) {
      ex.printStackTrace();
    }

    repaint();
  }

//减轻闪烁 直接调用panit()
  public void update(Graphics g)
  
{
    paint(g);
  }


}

抱歉!评论已关闭.