Search code examples
javamavenopengljogl

Java OpenGL window closes as soon as it opens


I'm using maven to import the jogamp dependencies.

Here is the pom.xml content :

<dependencies>
    <dependency>
        <groupId>org.jogamp.gluegen</groupId>
        <artifactId>gluegen-rt-main</artifactId>
        <version>2.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.jogamp.jogl</groupId>
        <artifactId>jogl-all-main</artifactId>
        <version>2.3.2</version>
    </dependency>
</dependencies>

The code below should create a window.

import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;

public class Renderer {

    private static GLWindow window = null;

    public static void init(){
        GLProfile.initSingleton();
        GLProfile profile = GLProfile.get(GLProfile.GL2);
        GLCapabilities caps = new GLCapabilities(profile);

        window = GLWindow.create(caps);
        window.setSize(640, 360);
        window.setResizable(false);
        window.setVisible(true);
    }

    public static void main(String[] args){
        init();
    }
}

In my case, it creates a window that closes as soon as it opens, and it says Process finished with exit code 0. I followed these instructions, but even by adding the joal and jocl support into maven it did not work.


Solution

  • You need FPSAnimator

    public static void init(){
            GLProfile.initSingleton();
            GLProfile profile = GLProfile.get(GLProfile.GL2);
            GLCapabilities caps = new GLCapabilities(profile);
    
            window = GLWindow.create(caps);
            window.setSize(640, 360);
            window.setResizable(false);
            window.setVisible(true);
            FPSAnimator animator = new FPSAnimator(window, 30);
            animator.start();
        }