当前线程中没有找到OpenGL上下文(但我在主线程上?)

时间:2014-06-12 18:29:07

标签: java lwjgl startup

我一直在制作LWJGL游戏很长一段时间了。它取得了很大的进步,所以我决定使用一个开始画面! 因为只有用于创建开始屏幕的垃圾LWJGL库,所以我目前正在使用纯java创建一个。 但是,当我从开始屏幕类调用TerrainDemo.startGame()(请忽略名称中的“demo”,我需要更改它)时,它会抛出此错误:

Exception in thread "AWT-EventQueue-0" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glPushMatrix(GL11.java:2582)
at Joehot200.TerrainDemo.update(TerrainDemo.java:2017)
at Joehot200.TerrainDemo.startGame(TerrainDemo.java:2029)
at Joehot200.Main$2.actionPerformed(Main.java:59)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

然而,当从它自己的类(例如public void main(String[] args){ startGame})调用startGame()时,它完全正常。

这是我的开始屏幕代码:

package Joehot200;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JMenu;
import javax.swing.JSplitPane;
import javax.swing.JProgressBar;
import javax.swing.JMenuBar;

public class Main extends JFrame {

private JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Main frame = new Main();
                frame.setVisible(true);
                frame.setTitle("Pirate game");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
/**
 * Create the frame.
 */
public Main() {

    JMenuBar menuBar = new JMenuBar();
    setJMenuBar(menuBar);
    JButton btnEnterBattlefield = new JButton("Enter battlefield!");
    menuBar.add(btnEnterBattlefield);
    btnEnterBattlefield.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e)
        {
            //Execute when button is pressed
            System.out.println("You clicked the button");
            TerrainDemo.startGame();
        }
    });      

    JMenu mnLogIn = new JMenu("Log in");
    menuBar.add(mnLogIn);

    JButton btnLogIntoGame = new JButton("Log into game.");
    mnLogIn.add(btnLogIntoGame);

    JButton btnRegisterNewAccount = new JButton("Register new account");
    mnLogIn.add(btnRegisterNewAccount);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    FlowLayout flowLayout = (FlowLayout) contentPane.getLayout();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    //contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
      JLabel background=new JLabel(new ImageIcon("C:/CraftBukkit/background.png"));

        add(background);

        background.setLayout(new FlowLayout());
    //JProgressBar progressBar = new JProgressBar();
    //contentPane.add(progressBar);

    //JMenu mnWelcomeToA = new JMenu("Welcome to a pirate game!");
    //contentPane.add(mnWelcomeToA);

    //JButton btnStartGame = new JButton("Start game");
    //mnWelcomeToA.add(btnStartGame);

    //JSplitPane splitPane = new JSplitPane();
    //mnWelcomeToA.add(splitPane);

    //JButton btnRegister = new JButton("Register");
    //splitPane.setLeftComponent(btnRegister);

    //JButton btnLogin = new JButton("Login");
    //splitPane.setRightComponent(btnLogin);


}

}

我不相信我有任何代码可以让我脱离主线程,这就是为什么它让我困惑为什么它会抱怨它。

那么如何解决这个问题并让LWJGL代码工作呢?

谢谢!

0 个答案:

没有答案