如何在swing应用程序中正确放置libgdx应用程序? [英] How to put properly a libgdx application inside swing application?

查看:138
本文介绍了如何在swing应用程序中正确放置libgdx应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的游戏创建一个关卡编辑器,而且我在使用LwjglCanvas和JFrame时遇到了问题。我使用JFrame(而不是LwjglFrame)来保持引擎和关卡编辑器尽可能独立。
我有两个JAR:WorldEditor.jar和GameEngine.jar。在WorldEditor中,我有一个名为test的按钮,假设加载GameEngine.jar(如果尚未加载)并启动(如果已加载则将其重新启动)到应用程序主框架中。
实际上,我所做的是将WorldEditor游戏容器(例如JFrame中的JPanel)注入游戏应用程序,并使用 Gdx.app.postRunnable 来将lwjglcanvas添加到注入的游戏容器中:

I'm creating a level editor for my game, and I ave a problem using LwjglCanvas with a JFrame. I use a JFrame (not a LwjglFrame) to keep engine and level editor as independent as possible. I have two JARs: WorldEditor.jar, and GameEngine.jar. Inside WorldEditor, I have a button called "test", that is suppose to load GameEngine.jar (if not already loaded) and launch (resart it if already loaded) it into the application main frame. Actually, what I do is injecting the WorldEditor game container (a JPanel inside the JFrame for example) to the game app, and use Gdx.app.postRunnable to add the lwjglcanvas to the injected game container :

世界编辑器方:

JPanel _gameContainer = new JPanel(); // is inside a JFrame
MyGame game = loadGame(_gameContainer); // load the GameEngine JAR, and retrive the game

GameEngine方:

GameEngine side:

// container is the _gamecontainer of above
public void createGame(final Container gameContainer)  
{
    LwjglCanvas canvas = new LwjglCanvas(myapp, myconfig);
    Gdx.app.postRunnable(new Runnable()
    {
       public void run()
       {
           gameContainer.add(canvas.getCanvas());
       }
    });
}

事实是 postRunnable (由于应用程序之前没有显示,我错了吗?)
我已经尝试了很长时间但没有结果......

The fact is that the postRunnable is never called (due to the fact that the app doesn't before being visible, am I wrong ?) I have been trying for a long time but no result ...

有人知道我能做些什么来解决这个问题吗?或者至少另一种(让我们说更容易)方法来做到这一点?

Does someone have an idea of what I could do to fix this problem ? Or a least another (let's say easier) method to do that ?

推荐答案

你必须使用SwingUtilites.invokelater,因为postRunnable发布到没有运行的游戏循环。我会尝试从MyGame获取一个Component并添加它。如果您返回一个Component,则表示您没有dep。到LwjglCanvas。它不是那么好,因为现在MyGame界面有一个dep。摇摆,但值得一试,看看它是否解决了你的问题。

You have to use SwingUtilites.invokelater because postRunnable posts to the game loop which is not running. I would try to get a Component from MyGame and add this. If you return a Component you don't have a dep. to the LwjglCanvas. It's not that nice because now the MyGame Interface has a dep. to swing but it's worth a shot to see if its solves your problem.

这篇关于如何在swing应用程序中正确放置libgdx应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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