IT_Programming/Java

[자바 애플릿] 더블버퍼링 예제

JJun ™ 2008. 3. 30. 02:47

import! java.awt.*;
import! java.applet!.*;

 

public class testDubleBuffer extends Applet! implements Runnable

{
 
 int x, y;
 boolean posX, posY;
 Image offImg;
 Graphics gc;

 
 Thread thr;
 
 public void init(){
  this.x = 0;
  this.y = 0;
  this.posX = true;
  this.posY = true;  
 }
 
 public void start()
 {
 
 offImg = this.createImage(this.getWidth(), this.getHeight());
  gc = this.offImg.getGraphics();
  this.thr = new Thread(this);
  this.thr.start();
 }
 
 public void destroy(){}
 public void stop(){}
 
 
public void update(Graphics g) {
        paint(g);
 }
 
 public void paint(Graphics g)
 {
  g.drawImage(this.offImg, 0, 0, this);
 }
 
 public void paintComp(Graphics g){
  g.setColor(Color.GREEN);
  g.fillRect(0, 0, this.getWidth(), this.getHeight());
  g.setColor(Color.WHITE);
  g.fillOval(x, y, 30, 30);
 }
 
 public void run()
 {
  while(true)

  {
     this.paintComp(this.gc);
     try

     {
            if(this.posX)

                 this.x+=3;

            else

                 this.x-=3;

            if(this.posY)

                 this.y+=3;

            else

                 this.y-=3;
       
            if(x + 30 > this.getWidth()) this.posX = false;
            if(y + 30 > this.getHeight()) this.posY = false;
            if(x < 0) this.posX = true;
            if(y < 0) this.posY = true;
            Thread.sleep(25);
     }catch(Exception e){}
         this.repaint();
    }
 }
}