在Linux上,Swing/AWT双缓冲非常慢 [英] Swing/AWT Double Buffering very slow on Linux

查看:62
本文介绍了在Linux上,Swing/AWT双缓冲非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JFrame中有一个AWT画布(我知道不应同时使用Swing和AWT,但它在Windows上可以很好地工作,所以我认为问题不是由此引起的),并使用Canvas中的BufferStrategy在屏幕上绘制.它在Windows上运行非常顺畅,但是当我尝试在Ubuntu 12.04上运行时,它变得异常缓慢.不仅渲染而且JFrame也没有响应.当我注释掉双缓冲和渲染部分时,它再次变得平滑.我有一个不再支持的ATI图形卡,所以我使用的是开源视频驱动程序,但是当我运行另一个使用OpenGL的程序时,它的运行速度不会像Swing/AWT那样慢.为什么会这样呢?

I have an AWT Canvas within a JFrame (I know that Swing and AWT shouldn't be used simultaneously but it works well on Windows so I don't think the problem is caused by this) and using a BufferStrategy from the Canvas to draw on screen. It runs pretty smooth on Windows but when I tried to run it on Ubuntu 12.04 it became unbearably slow. Not just the rendering but JFrame is also unresponsive. When I comment out the double buffering and rendering part it's smooth again. I have an ATI graphics card which is no longer supported so I'm using the open source video drivers, but when I run another program which uses OpenGL it's not slow like Swing/AWT one. Why could this be happening?

主渲染:

...

private void render()
{
    bs = gamePanel.getBufferStrategy();

    Graphics g = bs.getDrawGraphics();

    currentState.render(g);

    g.dispose();
    bs.show();
}

...

除了填充屏幕的黑色矩形之外,在currentstate.render()方法中我什么也没画.

I don't draw anything in currentstate.render() method except a black rectangle that fills the screen.

edit:好的,所以我找到了真正的问题,这是我的游戏循环.我在新线程中使用while循环来更新和渲染(上面提供的渲染代码).如果我使用Canvas的 paint()方法,它可以平稳运行,但是我对帧速率没有任何控制.为什么while循环会减慢我的程序的速度?

edit: OK so I tracked down the real problem, it's my game loop. I'm using a while loop within a new Thread to update and render(render code provided above). If I use paint() method of Canvas it runs smoothly but then I don't have any control over my frame rate. Why is a while loop slowing down my program?

游戏循环如下:

    while(running)
    {
        update();
        render();
    }

推荐答案

我将 Toolkit.getDefaultToolkit().sync(); 放入我的渲染方法中,该方法似乎可以解决该问题.但是,只有在Linux下才应该放
我用下面的代码来做到这一点

I put Toolkit.getDefaultToolkit().sync(); in my render method which appears to fix it. However you should put only if it's Linux
I used the code bellow to do that

public static String getOsName() {
    String OS = null;
    if(OS == null) { OS = System.getProperty("os.name"); }
    return OS;
}

public static boolean isUnix() { return getOsName().startsWith("Linux"); }

这篇关于在Linux上,Swing/AWT双缓冲非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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