I am trying to get familiar with JOGL but cannot seem to get around this little stumbling block. I downloaded demo code for the "Gears" demo from
https://jogl-demos.dev.java.net/. The code works fine if used as is.
<<============ Trimmed version of the demo code =============>>
package joglapplication1;
/** imports and JavaDoc **/
public class Main implements GLEventListener, MouseListener, MouseMotionListener {
public static void main (String[] args) {
Frame frame = new Frame ("Gear Demo");
GLCanvas canvas = new GLCanvas ();
canvas.addGLEventListener (new Main ());
frame.add (canvas);
frame.setSize (300, 300);
//final Animator animator = new Animator(canvas);
frame.addWindowListener (new WindowAdapter () {
public void windowClosing (WindowEvent e) {
// Run this on another thread than the AWT event queue to
// make sure the call to Animator.stop() completes before
// exiting
new Thread (new Runnable () {
public void run () {
//animator.stop();
System.exit (0);
}
}).start ();
}
});
frame.setVisible (true);
//animator.start();
}
/** private variables **/
public void init (GLAutoDrawable drawable) {...}
public void reshape (GLAutoDrawable drawable, int x, int y, int width, int height) {...}
public void display (GLAutoDrawable drawable) {...}
public void displayChanged (GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
public static void gear (GL gl, float inner_radius, float outer_radius, float width, int teeth, float tooth_depth) {...}
// Methods required for the implementation of MouseListener
...
// Methods required for the implementation of MouseMotionListener
...
}
As you can see, the GLCanvas is added to the Frame and occupies the whole display area. I want only a part of the frame to be displaying OpenGL stuff and want to use the rest for showing other information. So I create a custom class that extends JFrame; add a JPanel to it; and add GLCanvas to the JPanel instead to the form. Something great happens now and OpenGL stuff is nowhere to be seen.
To simplify stuff I removed the JPanel from MyFrame and added the GLCanvas directly to MyFrame. Still no result. Anyways, I finally figured that initComponents() being called from MyFrame ctor is the root cause because somehow it stops events from reaching the GLEventListener. I comment out the call to initComponents() and the wonderful gears show up. but then that defeats the whole purpose of the MyFrame and the GUI to design the form.
Here is the code for initComponents().
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
setDefaultCloseOperation(j
avax.swing
.WindowCon
stants.EXI
T_ON_CLOSE
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(ge
tContentPa
ne());
getContentPane().setLayout
(layout);
layout.setHorizontalGroup(
layout.createParallelGroup
(javax.swi
ng.GroupLa
yout.Align
ment.LEADI
NG)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup
(javax.swi
ng.GroupLa
yout.Align
ment.LEADI
NG)
.addGap(0, 300, Short.MAX_VALUE)
);
}// </editor-fold>
The code for MyListener is unchanged except that main() is moved to a different file. My guess is that setting layout causes the problems. Any ideas how to reopen the channel to GLEventListener?