Login  Register

Fullscreen issue on KeyListener event

Posted by laani on Mar 25, 2012; 1:21pm
URL: https://forum.jogamp.org/Fullscreen-issue-on-KeyListener-event-tp3855615.html

Hi, I'm new to JOGL.

I'm trying to get full screen mode to work but I have an issue on a key event when I toggle between fullscreen and window mode.

The funny thing is when I place this code in simpleScene.animator.pause(); in the keyPressed(KeyEvent e) function
it works.

Can anyone explain why its doing this?

Thanks in advance!

ERROR
Exception in thread "Timer-0" javax.media.opengl.GLException: javax.media.opengl.GLException: Unable to create temp OpenGL context for device context 0xc013661
        at jogamp.opengl.awt.AWTThreadingPlugin.invokeOnOpenGLThread(AWTThreadingPlugin.java:98)
        at jogamp.opengl.ThreadingImpl.invokeOnOpenGLThread(ThreadingImpl.java:197)
        at javax.media.opengl.Threading.invokeOnOpenGLThread(Threading.java:164)
        at javax.media.opengl.awt.GLCanvas.maybeDoSingleThreadedWorkaround(GLCanvas.java:830)
        at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:419)
        at com.jogamp.opengl.util.AWTAnimatorImpl.display(AWTAnimatorImpl.java:74)
        at com.jogamp.opengl.util.AnimatorBase.display(AnimatorBase.java:142)
        at com.jogamp.opengl.util.FPSAnimator$1.run(FPSAnimator.java:128)
        at java.util.TimerThread.mainLoop(Unknown Source)
        at java.util.TimerThread.run(Unknown Source)
Caused by: javax.media.opengl.GLException: Unable to create temp OpenGL context for device context 0xc013661
        at jogamp.opengl.windows.wgl.WindowsWGLContext.createImpl(WindowsWGLContext.java:294)
        at jogamp.opengl.GLContextImpl.makeCurrentLocking(GLContextImpl.java:468)
        at jogamp.opengl.GLContextImpl.makeCurrent(GLContextImpl.java:399)
        at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:383)
        at javax.media.opengl.awt.GLCanvas$DisplayOnEventDispatchThreadAction.run(GLCanvas.java:956)
        at java.awt.event.InvocationEvent.dispatch(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$000(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.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)

CODE

        package windows;
       
        import java.awt.Color;
        import java.awt.Dimension;
        import java.awt.Frame;
        import java.awt.GraphicsDevice;
        import java.awt.GraphicsEnvironment;
        import java.awt.Toolkit;
        import java.awt.event.KeyAdapter;
        import java.awt.event.KeyEvent;
        import java.awt.event.WindowAdapter;
        import java.awt.event.WindowEvent;
       
        public class App
        {
                private static boolean fullScreenMode= true;
                private static int winW, winH;
                private static int winX, winY;
                private static int winWCurrent, winHCurrent;
                private static String winTitle = "Window Title";
                private final SimpleScene simpleScene;
       
                public App()
                {
                        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                       
                        winW = dim.width;
                        winH = dim.height - 40;
                        winX = winY = 0;
                       
                        simpleScene = new SimpleScene();
                       
                        final GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
                        final GraphicsDevice device = environment.getDefaultScreenDevice();
                        device.getDefaultConfiguration();
                       
                        final Frame frame = new Frame();

                        frame.setTitle(winTitle);
                        frame.setBackground(Color.BLUE);
                       
                        if(device.isFullScreenSupported())
                        {
                                frame.setUndecorated(true);
                                frame.setResizable(false);
                                device.setFullScreenWindow(frame);
                                fullScreenMode = true;
                        }
                        else
                        {
                                frame.setBounds(winX, winY, winW, winH);
                                frame.setResizable(true);
                                fullScreenMode = true;
                        }
                       
                        winWCurrent = frame.getWidth();
                        winHCurrent = frame.getHeight();

                        frame.add(simpleScene);
                        frame.setVisible(true);
                       
                        frame.addWindowListener(new WindowAdapter()
                        {
                                public void windowClosing(WindowEvent we)
                                {
                                        simpleScene.animator.stop();
                                        System.exit(0);
                                }
                        });
                       
                        frame.addKeyListener(new KeyAdapter()
                        {
                                @Override
                                public void keyPressed(KeyEvent e)
                                {
                                        simpleScene.animator.pause();
                                       
                                        int keyCode = e.getKeyCode();
                                        switch(keyCode)
                                        {
                                        case KeyEvent.VK_SPACE:
                                                fullScreenMode = !fullScreenMode;
                                                frame.setVisible(false);
                                                if(frame.isDisplayable())
                                                        frame.dispose();
                                                if(fullScreenMode)
                                                {
                                                        winW = frame.getWidth();
                                                        winH = frame.getHeight();
                                                        winX = frame.getX();
                                                        winY = frame.getY();
                                                       
                                                        if(device.isFullScreenSupported())
                                                        {
                                                                frame.setUndecorated(true);
                                                                frame.setResizable(false);
                                                                device.setFullScreenWindow(frame);
                                                        }
                                                }
                                                else
                                                {
                                                        frame.setUndecorated(false);
                                                        device.setFullScreenWindow(null);
                                                        frame.setBounds(winX, winY, winW, winH);
                                                        frame.setResizable(true);
                                                }
                                               
                                                winWCurrent = frame.getWidth();
                                                winHCurrent = frame.getHeight();
                                                frame.setVisible(true);
                                                break;
       
                                        case KeyEvent.VK_ESCAPE:
                                               
                                                simpleScene.animator.stop();
                                                System.exit(0);
                                                break;
                                        }
                                }
                        });
                        simpleScene.animator.start();

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


        package windows;
       
        import java.awt.Panel;
        import javax.media.opengl.*;
        import javax.media.opengl.awt.GLCanvas;
// import javax.media.opengl.glu.GLU;
        import com.jogamp.opengl.util.FPSAnimator;
       
        public class SimpleScene extends Panel implements GLEventListener
        {
                /**
                 *
                 */
                private static final long serialVersionUID = 1L;
               
                private static final int REFRESH_FPS = 60;
// private GLU glu; // GL Utility
                final FPSAnimator animator;
               
                private double theta = 0;
                private double sine = 0;
                private double cosine = 0;
               
                public SimpleScene()
                {
                        GLProfile glp = GLProfile.getDefault();
                        GLCapabilities glc = new GLCapabilities(glp);
                        GLCanvas canvas = new GLCanvas(glc);
                        canvas.setSize(500, 500);
                       
                        this.add(canvas);
                        canvas.setVisible(true);
                        canvas.addGLEventListener(this);
                        animator = new FPSAnimator(canvas, REFRESH_FPS, true);
                }
                       
                @Override
                public void display(GLAutoDrawable drawable)
                {
                        System.out.println("DISPLAY");
                        update();
                        render(drawable);
                }
       
                private void render(GLAutoDrawable drawable)
                {
                        System.out.println("RENDER");
                        GL2 gl = drawable.getGL().getGL2();
                       
                        // clear the color buffer
                        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
                       
                        // draw triangle filling the window
                        gl.glBegin(GL.GL_TRIANGLES);
                        gl.glColor3f(1, 0, 0);
                        gl.glVertex2d(-cosine, -cosine);
                        gl.glColor3f(0, 1, 0);
                        gl.glVertex2d(0, cosine);
                        gl.glColor3f(0, 0, 1);
                        gl.glVertex2d(sine, -sine);
                        gl.glEnd();
                }
       
                private void update()
                {
                        System.out.println("UPDATE");

                        theta += 0.01;
                        sine = Math.sin(theta);
                        cosine = Math.cos(theta);
                }
       
                @Override
                public void dispose(GLAutoDrawable arg0) {
                        // TODO Auto-generated method stub
                }
       
                @Override
                public void init(GLAutoDrawable drawable)
                {
                        System.out.println("INIT");
                        GL gl = drawable.setGL(new DebugGL2(drawable.getGL().getGL2()));
                }
       
                @Override
                public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
                                int arg4) {
                        // TODO Auto-generated method stub
                }
        }



IMAGES: BEFORE/AFTER