读书人

java双缓冲范例

发布时间: 2012-09-01 09:33:02 作者: rapoo

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 );}}

?

读书人网 >编程

热点推荐