让你的英雄(就是那张动的图片)和你的背景一起刷新就行,利用双缓存,现将英雄和背景绘制到后缓存,然后再将整个缓存区的东西复制到屏幕上,用buffer创建缓存区,再利用Component类的createImage(int w,int h)方法生成后缓存,代码如下:
private Image doubleBuffer;
public void update(Graphics g)
{
Dimension size=getSize();
if(doubleBuffer==null||doubleBuffer.getWidth(this)!=size.width||doubleBuffer.getHeight(this)!=size.height)
{
doubleBuffer=createImage(size.width,size.height);
}
if(doubleBuffer!=null)
{
Graphics g2=doubleBuffer.getGraphcis();
paint(g2);
g2.dispose();
g.drawImage(doubleBuffer,0,0,null);
}
else
{
paint(g);
}
}