2010-05-20 3 views
1

페인트 및 업데이트 메서드가 호출되는시기에 대해 질문이 있습니까 ?? 나는 더블 버퍼링을 사용하고자하는 게임 애플릿을 가지고있다. 그러나 나는 그것을 사용할 수 없다. 문제는 내 게임에는 run() 메소드 내부에서 움직이는 공이있다. 이중 버퍼링을 사용하여 스왑하는 법을 알고 싶다. 오프 스크린 이미지와 현재 이미지. 누군가 plz 도움.스레드와 애플릿 내에서 이중 버퍼링을 사용하는 방법

update() 및 paint() 메소드가 모두있는 경우, 먼저 호출됩니다. 언제, 왜 ???

답변

3

사용할 수있는 방법은 애플릿에 Canvas를 추가 한 다음 해당 캔버스에 대한 버퍼 전략을 만드는 것입니다. 코드를 추상화하면 하드웨어 가속을 얻을 수 있습니다.

코드는 여기에 있습니다 : http://www.gamedev.net/community/forums/topic.asp?topic_id=405663 - AppletGameCore를 확장하고 필요한 메소드를 구현하는 고유 한 서브 클래스를 정의하십시오.

import java.awt.Canvas; 
import java.awt.Graphics2D; 
import java.awt.Dimension; 
import java.awt.image.BufferStrategy; 
import java.applet.Applet; 

/** 
*AppletGameCore.java 
*@author David Graham 
*/ 

public abstract class AppletGameCore extends Applet implements Runnable 
{ 
    private BufferStrategy bufferStrategy; 
    private Canvas drawArea;/*Drawing Canvas*/ 
    private boolean stopped = false;/*True if the applet has been destroyed*/ 
    private int x = 0; 

    public void init() 
    { 
      Thread t = new Thread(this); 
      drawArea = new Canvas(); 
      setIgnoreRepaint(true); 
      t.start(); 
    } 

    public void destroy() 
    { 
      stopped = true; 

      /*Allow Applet to destroy any resources used by this applet*/ 
      super.destroy(); 
    } 

    public void update() 
    { 
      if(!bufferStrategy.contentsLost()) 
      { 
       //Show bufferStrategy 
       bufferStrategy.show(); 
      } 
    } 

    //Return drawArea's BufferStrategy 
    public BufferStrategy getBufferStrategy() 
    { 
      return bufferStrategy; 
    } 

    //Create drawArea's BufferStrategies 
    public void createBufferStrategy(int numBuffers) 
    { 
      drawArea.createBufferStrategy(numBuffers); 
    } 

    //Subclasses should override this method to do any drawing 
    public abstract void draw(Graphics2D g); 

    public void update(Graphics2D g) 
    { 
      g.setColor(g.getBackground()); 
      g.fillRect(0,0,getWidth(),getHeight()); 
    } 

    //Update any sprites, images, or primitives 
    public abstract void update(long time); 

    public Graphics2D getGraphics() 
    { 
      return (Graphics2D)bufferStrategy.getDrawGraphics(); 
    } 

    //Do not override this method  
    public void run() 
    { 
      drawArea.setSize(new Dimension(getWidth(),getHeight())); 
      add(drawArea); 
      createBufferStrategy(2); 
      bufferStrategy = drawArea.getBufferStrategy(); 

      long startTime = System.currentTimeMillis(); 
      long currTime = startTime; 

      //animation loop 
      while(!stopped) 
      { 
       //Get time past 
       long elapsedTime = System.currentTimeMillis()-currTime; 
       currTime += elapsedTime; 

       //Flip or show the back buffer 
       update(); 

       //Update any sprites or other graphical objects 
       update(elapsedTime); 

       //Handle Drawing 
       Graphics2D g = getGraphics(); 
       update(g); 
       draw(g); 

       //Dispose of graphics context 
       g.dispose(); 
      } 

    } 
} 
관련 문제