如何在 Swing 应用程序中正确放置 libgdx 应用程序?

2023-03-03Java开发问题
6

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

问题描述

我正在为我的游戏创建关卡编辑器,但我在使用带有 JFrame 的 LwjglCanvas 时遇到了问题.我使用 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 获取一个组件并添加它.如果你返回一个组件,你就没有一个 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 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13