How-to: implementing custom OpenGL widgets

Hi all.

I would be very interested in creating my own OpenGL widget to perform my drawing operations on. In particular, I though it would be great if I could subclass GLJPanel so that I would be able to use Swing widgets on top of it.

But, looking through the jogl sources, it seems a bit harder than initially expected. Anybody can give some clues about what the guidelines are or how to do things properly? Specifically, should the drawing be made in the display() method or in the paint() method? I don’t know much about AWT internal mechanisms.

Any help would be welcome.

Thanks.

If all you want to do is add Swing components on top of a GLJPanel, you shouldn’t need to subclass it. It should be 100% Swing compatible and as it’s a JPanel subclass it should behave like a container. You can also use tooltips, pop-up menus, etc. with it, and put it into JInternalFrames.

Take a look at the source code for the JGears demo for an example of subclassing the GLJPanel. You would want to do any Swing-specific rendering in paintComponent() after a call to super.paintComponent(). Note also how that demo uses the OpenGL alpha value to cause Java2D rendering results to show through the transparent background of the 3D rendering, though the background gradient is drawn with another widget.

No, actually I want to do a bit more than just putting widgets on top of it. I want to simulate a “layered” canvas, where each GLEventListener is viewed as a (possibly translucent) layer drawing on the canvas following a certain order.
Of course I can do this without subclassing, but I would like to integrate this behaviour into the canvas and automate the process.

I’ll take a look at the mentioned example and see if I can get a grasp on how to put things down to code.

Thanks.

You may be able to implement this simply by overriding addGLEventListener/removeGLEventListener and each time you add a new GLEventListener object add one before the new one which just clears the OpenGL depth buffer.