【发布时间】:2015-02-01 12:30:58
【问题描述】:
当我开始我的游戏时,我总是间歇性地出现这个错误:
Exception in thread "main" java.lang.InternalError: couldn't create component peer
at sun.awt.windows.WComponentPeer.checkCreation(Unknown Source)
at sun.awt.windows.WComponentPeer.<init>(Unknown Source)
at sun.awt.windows.WCanvasPeer.<init>(Unknown Source)
at sun.awt.windows.WPanelPeer.<init>(Unknown Source)
at sun.awt.windows.WWindowPeer.<init>(Unknown Source)
at sun.awt.windows.WFramePeer.<init>(Unknown Source)
at sun.awt.windows.WToolkit.createFrame(Unknown Source)
at java.awt.Frame.addNotify(Unknown Source)
at java.awt.Window.show(Unknown Source)
at java.awt.Component.show(Unknown Source)
at java.awt.Component.setVisible(Unknown Source)
at java.awt.Window.setVisible(Unknown Source)
at Joehot200.TerrainDemo.setUpDisplay(TerrainDemo.java:3569)
at Joehot200.TerrainDemo.startGame(TerrainDemo.java:3640)
at StartScreenExperiments.Test2.resartTDemo(Test2.java:55)
at StartScreenExperiments.Test2.main(Test2.java:41)
我无法重现它,而且它似乎只是在我开始游戏时随机发生 - 有时,它开始正常,而其他时候,它有这个错误。
我正在使用以下代码启动显示:
private void setUpDisplay() {
//System.setProperty("org.lwjgl.librarypath", new File("D:/Downloads/mcp/ship/Ship/lib/natives-win").getAbsolutePath());
try {
DisplayMode displayMode = null;
DisplayMode[] modes = Display.getAvailableDisplayModes();
for (int i = 0; i < modes.length; i++)
{
if (modes[i].isFullscreenCapable())
{
displayMode = modes[i];
}
}
if (displayMode == null){
displayMode = new DisplayMode(100, 100);
}
displayMode = Display.getDesktopDisplayMode();
//Display.setDisplayModeAndFullscreen(new DisplayMode(200, 200));
//Display.setDisplayMode(new DisplayMode(WINDOW_DIMENSIONS[0], WINDOW_DIMENSIONS[1]));
Display.setDisplayMode(displayMode);
Display.setResizable(true);
Display.setTitle("Pirate game");
//Display.setFullscreen(true);
try {Thread.sleep(3000);} catch (InterruptedException e1) {}
frame = new JFrame();
Canvas canvas = new Canvas();
frame.add(canvas);
frame.setSize(500, 500);
frame.setVisible(true);
try {
try {Thread.sleep(3000);} catch (InterruptedException e1) {}
Display.setParent(canvas);
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
try {Thread.sleep(3000);} catch (InterruptedException e1) {}
frame.setVisible(true);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.repaint();
} catch (LWJGLException e) {
e.printStackTrace();
//cleanUp(true);
}
}
显然这与正在创建的 JFrame 有关(因为如果我在没有 JFrame 的情况下进行显示,它可以正常工作),但是我目前无法解决问题(不放弃 JFrame,这是我做不到的) .
我不确定这个问题是否与 LWJGL/OpenGL 有任何关系,所以我还是将其标记为这样。
【问题讨论】:
-
你可以尝试在 gl 上下文创建之前(和之后)添加 thread.sleep 几秒钟
-
@huseyintugrulbuyukisik 我猜你的意思是
try {Thread.sleep(100);} catch (InterruptedException e1) {}在 Display.create() 之前和之后? -
是的,但像 sleep(3000) 或类似的。
-
您应该在 EDT 上启动您的 UI,方法是将其包装到
SwingUtilities.invokeLater块中。另外,删除那些Thread.sleep,它们只会造成延迟,不会有任何改进。 -
See also this link 了解更多信息。