Login  Register

JOGL and swing

Posted by cznlzq on Nov 13, 2013; 10:49pm
URL: https://forum.jogamp.org/JOGL-and-swing-tp4030623.html

I was trying to make a menu and then when I click the menu, I add the 3d canvas to the frame.

However, it doesn't work as I expected and it freezes there.

Here is the codes. The codes only work when I comment the final GLProfile glp = GLProfile.getDefault(); in the public void actionPerformed(ActionEvent e) function and uncomment the global one.

What's the difference there? Thanks.

By the way, I'm using the rc11 with JAVA 7 in Windows 7.

//=======================================================================
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.GLCanvas;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import com.jogamp.opengl.util.FPSAnimator;

public class SimpleSceneJOGL implements GLEventListener {

    private double theta = 0;
    private double s = 1;
    private double c = 1;

    public static void main(String[] args) {

        //final GLProfile glp = GLProfile.getDefault();
       
        final JFrame frame = new JFrame("AWT Window Test");
        frame.setSize(300, 300);
        JMenu file = new JMenu("File");
        file.setMnemonic('F');
        JMenuItem newItem = new JMenuItem("New");
        newItem.setMnemonic('N');
        file.add(newItem);
        JMenuItem openItem = new JMenuItem("Open");
        openItem.setMnemonic('O');
        file.add(openItem);
        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.setMnemonic('x');
        file.add(exitItem);
       
      //adding action listener to menu items
        newItem.addActionListener(
            new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e)
                {
                    System.out.println("New is pressed");
                   
                    final GLProfile glp = GLProfile.getDefault();
                    final GLCapabilities caps = new GLCapabilities(glp);
                    final GLCanvas canvas = new GLCanvas(caps);
                   
                    frame.add(canvas);
                   
                    canvas.addGLEventListener(new SimpleSceneJOGL());

                    FPSAnimator animator = new FPSAnimator(canvas, 60);
                    animator.add(canvas);
                    animator.start();
                   
                    frame.validate();
                    frame.repaint();
                }
            });
       


        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
       
        JMenuBar bar = new JMenuBar();
        frame.setJMenuBar(bar);
        bar.add(file);
       
        frame.setVisible(true);
    }

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

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

    @Override
    public void init(GLAutoDrawable drawable) {
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int w, int h) {
    }

    private void update() {
        theta += 0.01;
        s = Math.sin(theta);
        c = Math.cos(theta);
    }

    private void render(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();

        gl.glClear(GL.GL_COLOR_BUFFER_BIT);

        // draw a triangle filling the window
        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();
    }
}
//=======================================================================