JOGL blank in AWT and Swing

classic Classic list List threaded Threaded
4 messages Options
ptr
Reply | Threaded
Open this post in threaded view
|

JOGL blank in AWT and Swing

ptr
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();
    }
       

}
Reply | Threaded
Open this post in threaded view
|

Re: JOGL blank in AWT and Swing

Demoscene Passivist
Administrator
It's supposed to work this way. U have to explicitly add an Animator wich programmatically calls display() to refresh the window content at regular intervals.
ptr
Reply | Threaded
Open this post in threaded view
|

Re: JOGL blank in AWT and Swing

ptr
Thank you, I don't get how it works but I'll take your word for it.
Reply | Threaded
Open this post in threaded view
|

Re: JOGL blank in AWT and Swing

Demoscene Passivist
Administrator
>Thank you, I don't get how it works but I'll take your word for it.

If u have difficulties getting up to speed with the underlying render loop logic I would recommend taking a look at the tutorials Justin provided for JOGL Beginners. They are very well written, and will explain the stuff u need to get started: https://sites.google.com/site/justinscsstuff/jogl-tutorials

Especially take a look at the 3rd tutorial wich explains the render loop: https://sites.google.com/site/justinscsstuff/jogl-tutorial-3