java双缓冲实例

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Cartoon extends JApplet implements Runnable
{
Graphics screenBuffer = null;//创建图形缓冲区
Image screenImage = null;
private Thread runner;
private int x = 5;
private int move = 1;
public void init ( )
{
screenImage = createImage ( 230, 160 );
screenBuffer = screenImage.getGraphics ( ); 
}
public void start ( )
{
if (runner == null)
{
runner = new Thread( this );
runner.start();
}
}
public void run( )
{
Thread circle = Thread.currentThread ( ); 
while ( runner == circle )//指向同一对象,便开始运行
{
x += move;
if ( ( x > 105 ) || ( x < 5 ))
move *= -1;
repaint ( );
}
}
public void drawCircle( Graphics gc )
{
Graphics2D g2D = ( Graphics2D ) gc;
g2D.setColor ( Color.blue );
g2D.fillRect ( 0, 0, 100, 100 );
g2D.setColor ( Color.yellow );
g2D.fillRect ( 0, 0, 100, 100 );
g2D.setColor ( Color.red );
g2D.fillOval ( x, 5, 90, 90 );
} 
public void paint( Graphics g )
{
screenBuffer.setColor ( Color.white );
screenBuffer.fillRect (100,0,96,60); 
drawCircle ( screenBuffer );
//将缓冲区的图像复制到主缓冲区中
g.drawImage ( screenImage, 0, 0, this );
}
}

 

你可能感兴趣的:(java)