如何在线程和小程序内部使用双缓冲 [英] How to use double buffering inside a thread and applet

查看:63
本文介绍了如何在线程和小程序内部使用双缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

And when there is both update() and paint() method.which are called first,when and why ???

推荐答案

您可以使用的方法是将Canvas添加到applet,然后为该Canvas创建缓冲策略.抽象代码,您可能会获得硬件加速.

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.

代码在此处: http://www.gamedev.net/community/forums/topic.asp?topic_id = 405663 -扩展AppletGameCore并定义自己的实现所需方法的子类.

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

     }
}

这篇关于如何在线程和小程序内部使用双缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆