java双缓冲实例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java双缓冲实例

java双缓冲实例

 2012/2/22 9:20:01  xp9802  程序员俱乐部  我要评论(0)
  • 摘要:importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassCartoonextendsJAppletimplementsRunnable{GraphicsscreenBuffer=null;//创建图形缓冲区ImagescreenImage=null;privateThreadrunner;privateintx=5;privateintmove=1;publicvoidinit()
  • 标签: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 );
}
}

?

发表评论
用户名: 匿名