Login  Register

JOGL blank in AWT and Swing

Posted by ptr on Oct 03, 2010; 10:15am
URL: https://forum.jogamp.org/JOGL-blank-in-AWT-and-Swing-tp1623722.html

Hi I recently tried to run a java application with JOGL without much success.

The GLCanvas refuses to show unless I add an animator to it or if I resize the window.
Right now it displays the picture for a few milliseconds and then it disappears.
Is this a common problem or am I just missing out on some small detail.

package windows;

import javax.media.opengl.*;
import javax.media.opengl.awt.*;
import com.jogamp.opengl.util.FPSAnimator;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MainWindow implements GLEventListener
{
        JFrame f;
        GLProfile glp;
        GLCapabilities caps;
        GLCanvas canvas;
       
        public MainWindow()
        {
                glp = GLProfile.getDefault();
        caps = new GLCapabilities(glp);
        canvas = new GLCanvas(caps);
        canvas.addGLEventListener(this);
                f = new JFrame("Test");
                f.setSize(500,500);
                f.getContentPane().setLayout(null);
                canvas.setBounds(new Rectangle(0,0,400,400));
                f.getContentPane().add(canvas);
                f.add(canvas);
                f.setVisible(true);
               
        }
       
        public void update()
        {
        }
        public void render(GLAutoDrawable drawable)
        {
               
                GL2 gl = drawable.getGL().getGL2();
                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                gl.glBegin(GL.GL_TRIANGLES);
                gl.glColor3f(1,0,0);
                gl.glVertex2f(-1,-1);
                gl.glColor3f(0,1,0);
                gl.glVertex2f(0,1);
                gl.glColor3f(0,0,1);
                gl.glVertex2f(1,-1);
                gl.glEnd();
               
                drawable.swapBuffers();
                gl.glFlush();
               
        }

        @Override
        public void display(GLAutoDrawable arg0) {
                update();
            render(arg0);
               
        }

        @Override
        public void dispose(GLAutoDrawable arg0) {
                // TODO Auto-generated method stub
               
        }

        @Override
        public void init(GLAutoDrawable arg0) {
                // TODO Auto-generated method stub
               
        }

        @Override
        public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
                        int arg4) {
                // TODO Auto-generated method stub
               
        }
       
        public static void main(String[] args) {
                new MainWindow();
    }
       

}