java.lang.RuntimeException: No OpenGL context found in the current thread

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-13 02:44:53

问题


im trying to create a applet for a little game i created. when i run the game normally (no applet) it works just fine but when im trying to run it as an applet it gives me the following error.

im using lwjgl 2.8.4 opengl

java.lang.RuntimeException: No OpenGL context found in the current thread.
java.lang.RuntimeException: Unable to create display
    at com.base.engine.GameApplet.init(GameApplet.java:202)
    at sun.applet.AppletPanel.run(AppletPanel.java:434)
    at java.lang.Thread.run(Thread.java:722)

here is the code for the GameApplet.java

package com.base.engine;

import com.base.game.Game;
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;

public class GameApplet extends Applet {

    Canvas display_parent;

    /** Thread which runs the main game loop */
    Thread gameThread;

    /** is the game loop running */
    private static boolean running = false;

    private static Game game;

    public void startLWJGL() {
        //gameThread = new Thread() {
                       // @Override
            //public void run() {
                running = true;
                try {
                                        initDisplay();
                    Display.setParent(display_parent);
                    System.out.println("hij loopt");
                                        //Display.create();
                                        System.out.println("hij loopt");

                                        initGL();
                                        initGame();

                                        gameLoop();

                                        cleanUp();

                } catch (LWJGLException e) {
                    return;
                }
                //gameLoop();                                
            //}

        //};
        //gameThread.start();
    }

    private static void initGame()
    {
        game = new Game();
    }

    private static void getInput()
    {
        System.out.print("before input");
        game.getInput();
        System.out.print("after input");
    }

    private static void update()
    {
        game.update();
    }

    private static void render()
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glLoadIdentity();

        game.render();

        Display.update();
        Display.sync(60);
    }

    private static void gameLoop()
    {  

            System.out.print("before while");
            while(!Display.isCloseRequested())
            {
                while(running) {
                    System.out.print("before running shit");
                    Display.sync(60);
                    Display.update();

                    Display.destroy();

                    //getInput();
                    update();
                    render();
                    System.out.println("running gameLoop..");
                }
            }
            System.out.print("after while");
    }

    private static void initGL()
    {        

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(0, Display.getWidth(), 0, Display.getHeight(), -1, 1);
        glMatrixMode(GL_MODELVIEW);

        glDisable(GL_DEPTH_TEST);

        glClearColor(0,0,0,0);
    }

    private static void cleanUp()
    {
        Display.destroy();
        Keyboard.destroy();
    }

    private static void initDisplay()
    { 
        try 
        {

            Display.setDisplayMode(new DisplayMode(800,600));
            Display.create();
            Keyboard.create();
            System.out.println("Keyboard created");
            Display.setVSyncEnabled(true);
        } catch (LWJGLException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } 
    }

    public static ArrayList<GameObject> sphereCollide(float x, float y, float radius)
    {
        return game.sphereCollide(x, y, radius);
    }

    /**
     * Tell game loop to stop running, after which the LWJGL Display will 
     * be destoryed. The main thread will wait for the Display.destroy().
     */

    private void stopLWJGL() {
        running = false;

    }

        @Override
    public void start() {
        //startLWJGL();
    }

        @Override
    public void stop() {

    }

    /**
     * Applet Destroy method will remove the canvas, 
     * before canvas is destroyed it will notify stopLWJGL()
     * to stop the main game loop and to destroy the Display
     */
        @Override
    public void destroy() {
        remove(display_parent);
        super.destroy();
    }

        @Override
    public void init() {
        setLayout(new BorderLayout());
        try {
            display_parent = new Canvas() {
                                @Override
                public final void addNotify() {
                    super.addNotify();
                    startLWJGL();
                }
                                @Override
                public final void removeNotify() {
                    stopLWJGL();
                    super.removeNotify();
                }
            };
            display_parent.setSize(800,600);
            add(display_parent);
                        System.out.println("gaat ie lekker");
            display_parent.setFocusable(true);
            display_parent.requestFocus();
            display_parent.setIgnoreRepaint(true);
            setVisible(true);
        } catch (Exception e) {
            System.err.println(e);
            throw new RuntimeException("Unable to create display");
        }
    }

}

can someone tell me what im doing wrong?

来源:https://stackoverflow.com/questions/14926929/java-lang-runtimeexception-no-opengl-context-found-in-the-current-thread

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!