Java中的DoubleBuffering [英] DoubleBuffering in Java

查看:129
本文介绍了Java中的DoubleBuffering的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将DoubleBuffer实施到程序中时遇到一些麻烦.在晕倒文本墙之前,您应该知道其中有很多内容,以防万一您需要知道.我认为遇到问题的实际地方是一种方法.

I'm having some trouble implementing DoubleBuffer into my program. Before you faint from the wall of text, you should know that a lot of it is there just in case you need to know. The actual place where I think I'm having problems is in one method.

我最近在gpwiki上查找了有关双缓冲的教程,并决定尝试将他们所拥有的代码实施到我要实现双缓冲的代码中.出现以下错误:"java.lang.IllegalStateException:组件必须具有有效的对等项.

I've recently looked up a tutorial on the gpwiki about double buffering, and decided to try and implement the code they had into the code I have that I'm trying to implement doublebuffer in. I get the following error: "java.lang.IllegalStateException: Component must have a valid peer".

我不知道您是否知道它是否有所不同,但是以下是main方法的代码.这只是一个框架,在其中显示ChronosDisplay类.我用"..."省略了无关的代码

I don't know if it makes any difference if you know it or not, but the following is the code with the main method. This is just a Frame that displays the ChronosDisplay class inside it. I omitted irrelevant code with "..."

public class CDM extends JFrame
{
    public CDM(String str)
    {
        super("CD:M - "+str);
        try
        {
            ...
            ChronosDisplay theGame = new ChronosDisplay(str);
            ((Component)theGame).setFocusable(true);
            add(theGame);
        }
        catch(Exception e)
        {
            System.out.println("CDM ERROR: " +e);
        }
    }
    public static void main( String args[] )
    {
        CDM run = new CDM("DP_Mini");
    }
}

这是我认为问题所在的代码(我认为问题出在paint()方法中).此类显示在CDM类别中

Here is the code where I think the problem resides (I think the problem is in the paint() method). This class is displayed in the CDM class

public class ChronosDisplay extends Canvas implements  Runnable
{
    String mapName;
    public ChronosDisplay (String str)
    {
        mapName = str;
        new Thread(this).start();
        setVisible(true);
        createBufferStrategy(2);
    }
    public void paint( Graphics window )
    {
        BufferStrategy b = getBufferStrategy();
        Graphics g = null; 
        window.setColor(Color.white);
        try
        {
             g = b.getDrawGraphics();
            paintMap(g);
            paintUnits(g);
            paintBullets(g);
        }
        finally
        { g.dispose(); }
        b.show();
        Toolkit.getDefaultToolkit().sync(); 
    }
    public void paintMap( Graphics window )
    {
        TowerMap m = new TowerMap();
        try
        {
            m = new TowerMap(mapName);
            for(int x=0; x<m.getRows()*50; x+=50)
            {
                for(int y = 0; y<m.getCols()*50; y+=50)
                    {
                        int tileType = m.getLocation(x/50,y/50);
                        Image img;
                        if(tileType == 0)
                        {
                            Tile0 t = new Tile0(x,y);
                            t.draw(window);
                        }
                        ...// More similar if statements for other integers
            }
            catch(Exception e) ...
    }
    ...// Additional methods not shown here
    public void run()
    {
        try
        {
            while(true)
            {
                Thread.currentThread().sleep(20);
                repaint();
            }
        }
        catch(Exception e) ...
    }
}

如果您好奇(我怀疑这很重要),那么Tile0类中的draw()方法是:

If you're curious (I doubt it matters), the draw() method in the Tile0 class is:

public void draw( Graphics window )
{
    window.drawImage(img,getX(),getY(),50,50,null);
}

非常感谢任何指针,技巧或解决方案.谢谢你的时间! :D

Any pointers, tips, or solutions are greatly appreciated. Thanks for your time! :D

推荐答案

默认情况下,Swing是双缓冲的,因此您无需自己实现它.

Swing is double buffered by default so you don't need to implement it yourself.

您不应在Swing应用程序中使用Canvas类.那是不要混用AWT和Swing组件.

You should not be using the Canvas class in a Swing application. That is don't mix AWT and Swing components.

所有Swing组件都应在EDT上创建.阅读Swing教程,获取有关如何执行此操作的示例.

All Swing components should be created on the EDT. Read the Swing tutorial for examples on on how to do this.

这篇关于Java中的DoubleBuffering的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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