Home > database >  Index out of bound exception trying to create a window with Swing and JOGL
Index out of bound exception trying to create a window with Swing and JOGL

Time:11-04

This code is simply supposed to display a window and set its background color:

public class Code extends JFrame implements GLEventListener {

    private GLCanvas myCanvas;

    public Code() {
        setTitle("Chapter2 - program1");
        setSize(800, 600);
        setLocation(400, 400);
        myCanvas = new GLCanvas();
        myCanvas.addGLEventListener(this);
        this.add(myCanvas);
        setVisible(true);
    }

    public void display(GLAutoDrawable drawable)
    {  GL4 gl = (GL4) GLContext.getCurrentGL();
        float[] bkg = { 1.0f, 0.0f, 0.0f, 1.0f };
        FloatBuffer bkgBuffer = Buffers.newDirectFloatBuffer(bkg);
        gl.glClearBufferfv(GL_COLOR, 0, bkgBuffer);
    }

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

    public void init(GLAutoDrawable drawable) { }
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
    public void dispose(GLAutoDrawable drawable) { }

The error I get is this one:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 1
    at jogamp.opengl.windows.wgl.awt.WindowsAWTWGLGraphicsConfigurationFactory.chooseGraphicsConfigurationImpl(WindowsAWTWGLGraphicsConfigurationFactory.java:171)
    at com.jogamp.nativewindow.GraphicsConfigurationFactory.chooseGraphicsConfiguration(GraphicsConfigurationFactory.java:424)
    at com.jogamp.opengl.awt.GLCanvas.chooseGraphicsConfiguration(GLCanvas.java:1560)
    at com.jogamp.opengl.awt.GLCanvas.addNotify(GLCanvas.java:611)
    at java.desktop/java.awt.Container.addNotify(Container.java:2804)
    at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4839)
    at java.desktop/java.awt.Container.addNotify(Container.java:2804)
    at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4839)
    at java.desktop/java.awt.Container.addNotify(Container.java:2804)
    at java.desktop/javax.swing.JComponent.addNotify(JComponent.java:4839)
    at java.desktop/javax.swing.JRootPane.addNotify(JRootPane.java:729)
    at java.desktop/java.awt.Container.addNotify(Container.java:2804)
    at java.desktop/java.awt.Window.addNotify(Window.java:791)
    at java.desktop/java.awt.Frame.addNotify(Frame.java:495)
    at java.desktop/java.awt.Window.show(Window.java:1053)
    at java.desktop/java.awt.Component.show(Component.java:1728)
    at java.desktop/java.awt.Component.setVisible(Component.java:1675)
    at java.desktop/java.awt.Window.setVisible(Window.java:1036)
    at Code.<init>(Code.java:19)
    at Code.main(Code.java:30)

I'm using java 8 and sdk 17.0

CodePudding user response:

Finally I found the solution. I changed my OpenJDK version to 11. And it just worked and I don't know why. I don't know at which exact version the error appears, versions prior to 11 seems to work.

CodePudding user response:

Found this: https://forum.jogamp.org/Crash-in-WindowsAWTWGLGraphicsConfigurationFactory-td4040703.html

Source from https://github.com/JogAmp/jogl/blob/master/src/jogl/classes/jogamp/opengl/windows/wgl/awt/WindowsAWTWGLGraphicsConfigurationFactory.java

Sourcecode: 170: if( 0 > gcIdx ) { 171: chosenGC = configs[gcIdx]; Using a negative index on an array will always crash.

Looks like you are also using a negative index.

  • Related