import java.awt.*;
import java.applet.*;
public class HelloWorld extends Applet
{
private Image imgBuf;
private Graphics gBuf;
Thread lithread=null;
private int i=0;
public void init()
{
imgBuf=createImage(getSize().width,getSize().height);
gBuf=imgBuf.getGraphics();
gBuf.setColor(Color.black);
gBuf.fillRect(0,0,getSize().width,getSize().height);
gBuf.setColor(Color.white);
gBuf.drawLine(0,10,getSize().width-1,10);
gBuf.drawLine(0,30,getSize().width-1,30);
gBuf.drawLine(0,50,getSize().width-1,50);
gBuf.drawLine(0,70,getSize().width-1,70);
gBuf.drawLine(0,90,getSize().width-1,90);
gBuf.drawLine(0,110,getSize().width-1,110);
gBuf.drawLine(0,130,getSize().width-1,130);
gBuf.setColor(Color.red);
gBuf.drawRect(0,0,getSize().width-1,getSize().height-1);
}
public void start()
{
if(lithread==null)
{
lithread=new Thread();
lithread.start();
}
}
public void stop()
{
lithread=null;
}
public void paint(Graphics g)
{
g.drawImage(imgBuf,0,0,this);
gBuf.setColor(Color.black);
gBuf.fillRect(0,0,getSize().width,getSize().height);
gBuf.setColor(Color.white);
gBuf.drawLine(0,10,getSize().width-1,10);
gBuf.drawLine(0,30,getSize().width-1,30);
gBuf.drawLine(0,50,getSize().width-1,50);
gBuf.drawLine(0,70,getSize().width-1,70);
gBuf.drawLine(0,90,getSize().width-1,90);
gBuf.drawLine(0,110,getSize().width-1,110);
gBuf.drawLine(0,130,getSize().width-1,130);
gBuf.setColor(Color.green);
gBuf.drawRect(50-i,50-i,200-i,100-i);
try
{
lithread.sleep(1000);
}
catch(InterruptedException e)
{
}
i++;
repaint();
}
public void update(Graphics g)
{
paint(g);
}
}
双缓存技术:
图像闪烁的根本原因是当一幅图像显示完需要花下一幅时,首先将屏幕用背景色清除,然后再画下一幅,因此人们能看到背景色因此出现闪烁.
当一幅图像在显示的时候,下一幅要显示的图像已经在内存中写好,因此,当需要下一幅时可以直接通过函数从内存中一次性独到显示区,由于从内存中读出速度很快,所以看不到闪烁.
http://www.matrix.org.cn/thread.shtml?topicId=15665&forumId=19
http://blog.chinaunix.net/u/18021/showart_1102572.html