1

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?

mschuurmans
  • 1,088
  • 1
  • 12
  • 24
  • Instead of `display_parent.setSize(800,600); add(display_parent);` try `add(display_parent); validate();` – Andrew Thompson Feb 18 '13 at 10:37
  • @AndrewThompson when i do what you said it gives me the following error: org.lwjgl.opengl.OpenGLException: Invalid value (1281) java.lang.RuntimeException: Unable to create display at com.base.engine.GameApplet.init(GameApplet.java:204) at sun.applet.AppletPanel.run(AppletPanel.java:434) at java.lang.Thread.run(Thread.java:722) – mschuurmans Feb 20 '13 at 16:59
  • OK - add the `setSize(..)` back in and leave the `pack()` where it is. Report back. – Andrew Thompson Feb 20 '13 at 17:10
  • @AndrewThompson Ok, maybe this is a dumb question but what do you mean by pack()? – mschuurmans Feb 20 '13 at 17:35
  • My bad, by `pack()` I meant `validate()`. They do pretty much the same thing, except `pack()` is for top-level containers like `JFrame` where it also shrinks the frame to match the minimum size of the content. – Andrew Thompson Feb 20 '13 at 17:37
  • @AndrewThompson ah ok, here is the error im getting now, i added the setSize and let the validate() where is was: 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:206) at sun.applet.AppletPanel.run(AppletPanel.java:434) at java.lang.Thread.run(Thread.java:722) – mschuurmans Feb 20 '13 at 17:40
  • OK - so back to 'that damnable error' again. That's me out of ideas, sorry. – Andrew Thompson Feb 20 '13 at 17:41

0 Answers0