繁体   English   中英

如何在线程和小程序内部使用双缓冲

[英]How to use double buffering inside a thread and applet

我有一个关于何时调用paint and update方法的问题? 我有要使用双缓冲的游戏小程序,但我不能使用它。问题是在我的游戏中有一个正在run()方法内移动的球。我想知道如何使用双缓冲来交换屏幕外图片和当前图片。请帮助。

当同时有update()和paint()方法时,什么时候以及为什么?

可以使用的方法是将Canvas添加到小程序,然后为该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();
           }

     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM