How to use double buffering inside a thread and applet

前端 未结 1 1348
我在风中等你
我在风中等你 2021-01-15 22:16

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 i

相关标签:
1条回答
  • 2021-01-15 22:30

    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 讨论(0)
提交回复
热议问题