Question

I am trying to get JOGL working correctly with Swing. I have used WindowBuilder and hacked it together with a few JOGL examples. It works fine, but there is a problem. When I start the program, it starts with blank window. Even the Swing components do not come up:

enter image description here

Everything starts to work when I move mouse cursor over the window content or another event forces it to redraw - like change of focus, window move, re-size etc.

Here is my code:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLJPanel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.jogamp.opengl.util.Animator;

public class OpenGLTestMin {
    private JFrame frame;
    private Animator animator;
    private double theta = 0;
    private double s = 0;
    private double c = 0;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    OpenGLTestMin window = new OpenGLTestMin();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public OpenGLTestMin() {
        frame = new JFrame();

        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowevent) {
                animator.stop();
                frame.dispose();
                System.exit(0);
            }
        });

        // frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setSize(640, 480);
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = new int[]{0, 0};
        gridBagLayout.rowHeights = new int[]{0, 0};
        gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        frame.getContentPane().setLayout(gridBagLayout);

        JPanel panelMain = new JPanel();
        panelMain.setBorder(new EmptyBorder(5, 5, 5, 5));
        GridBagConstraints gbc_panelMain = new GridBagConstraints();
        gbc_panelMain.fill = GridBagConstraints.BOTH;
        gbc_panelMain.gridx = 0;
        gbc_panelMain.gridy = 0;
        frame.getContentPane().add(panelMain, gbc_panelMain);
        GridBagLayout gbl_panelMain = new GridBagLayout();
        gbl_panelMain.columnWidths = new int[]{0, 0, 0};
        gbl_panelMain.rowHeights = new int[]{0, 0};
        gbl_panelMain.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
        gbl_panelMain.rowWeights = new double[]{1.0, Double.MIN_VALUE};
        panelMain.setLayout(gbl_panelMain);

        JPanel panelButton = new JPanel();
        GridBagConstraints gbc_panelButton = new GridBagConstraints();
        gbc_panelButton.fill = GridBagConstraints.BOTH;
        gbc_panelButton.gridx = 1;
        gbc_panelButton.gridy = 0;
        panelMain.add(panelButton, gbc_panelButton);
        GridBagLayout gbl_panelButton = new GridBagLayout();
        gbl_panelButton.columnWidths = new int[]{0, 0};
        gbl_panelButton.rowHeights = new int[]{0, 0, 0};
        gbl_panelButton.columnWeights = new double[]{0.0, Double.MIN_VALUE};
        gbl_panelButton.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        panelButton.setLayout(gbl_panelButton);

        JButton btn1 = new JButton("Button 1");
        GridBagConstraints gbc_btn1 = new GridBagConstraints();
        gbc_btn1.insets = new Insets(0, 0, 5, 0);
        gbc_btn1.gridx = 0;
        gbc_btn1.gridy = 0;
        panelButton.add(btn1, gbc_btn1);

        JButton btn2 = new JButton("Button 2");
        GridBagConstraints gbc_btn2 = new GridBagConstraints();
        gbc_btn2.gridx = 0;
        gbc_btn2.gridy = 1;
        panelButton.add(btn2, gbc_btn2);


        GLProfile glprofile = GLProfile.getDefault();
        GLCapabilities glcapabilities = new GLCapabilities(glprofile);
        GLJPanel glcanvas = new GLJPanel(glcapabilities);

        glcanvas.addGLEventListener(new GLEventListener() {
            @Override
            public void reshape(GLAutoDrawable glautodrawable, int x, int y, int width, int height) {
            }

            @Override
            public void init(GLAutoDrawable glautodrawable) {
            }

            @Override
            public void dispose(GLAutoDrawable glautodrawable) {
            }

            @Override
            public void display(GLAutoDrawable glautodrawable) {
                theta += 0.01;
                s = Math.sin(theta);
                c = Math.cos(theta);

                GL2 gl = glautodrawable.getGL().getGL2();
                gl.glClear(GL.GL_COLOR_BUFFER_BIT);
                gl.glBegin(GL.GL_TRIANGLES);
                gl.glColor3f(1, 0, 0);
                gl.glVertex2d(-c, -c);
                gl.glColor3f(0, 1, 0);
                gl.glVertex2d(0, c);
                gl.glColor3f(0, 0, 1);
                gl.glVertex2d(s, -s);
                gl.glEnd();
            }
        });

        GridBagConstraints gbc_panel = new GridBagConstraints();
        gbc_panel.insets = new Insets(0, 0, 0, 5);
        gbc_panel.fill = GridBagConstraints.BOTH;
        gbc_panel.gridx = 0;
        gbc_panel.gridy = 0;
        panelMain.add(glcanvas, gbc_panel);

        animator = new Animator(glcanvas);
        animator.start();
    }
}

Any ideas how to fix this? I am on Windows 7.

Was it helpful?

Solution

I have found the solution. GLJPanel should be initialized directly in main method, not in EventQueue. Then it works just fine. Also note that GLCanvas has MUCH better performance than GLJPanel, however sometimes glitches a bit when resizing window.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top