开发者

How to use double buffering inside a thread and applet

开发者 https://www.devze.com 2022-12-30 19:38 出处:网络
I have a question about when paint and update method is called?? i have game applet where i want to use double buffering.But i cant use it.the problem is

I have a question about when paint and update method is called?? i have game applet where i want to use double buffering.But i cant use it.the problem is In my game there is a ball which is moving inside run() method.I want to know how to use double buffering to swap the offscreen image and current image.Someone plz help.

And when there is both update() and paint() method.w开发者_开发百科hich are called first,when and why ???


A method you can use is to add a Canvas to the applet and then create a buffer strategy for that canvas. Abstracts the code, and you may get hardware acceleration.

The code is here: http://www.gamedev.net/community/forums/topic.asp?topic_id=405663 -- extend AppletGameCore and define your own subclass that implements the required methods.

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

     }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消