javax.media.opengl

Class GLJPanel

Implemented Interfaces:
ComponentEvents, GLAutoDrawable, GLDrawable

public class GLJPanel
extends JPanel
implements GLAutoDrawable

A lightweight Swing component which provides OpenGL rendering support. Provided for compatibility with Swing user interfaces when adding a heavyweight doesn't work either because of Z-ordering or LayoutManager problems.

The GLJPanel can be made transparent by creating it with a GLCapabilities object with alpha bits specified and calling setOpaque(boolean)(false). Pixels with resulting OpenGL alpha values less than 1.0 will be overlaid on any underlying Swing rendering.

Notes specific to the Reference Implementation: This component attempts to use hardware-accelerated rendering via pbuffers and falls back on to software rendering if problems occur. Note that because this component attempts to use pbuffers for rendering, and because pbuffers can not be resized, somewhat surprising behavior may occur during resize operations; the GLEventListener.init(GLAutoDrawable) method may be called multiple times as the pbuffer is resized to be able to cover the size of the GLJPanel. This behavior is correct, as the textures and display lists for the GLJPanel will have been lost during the resize operation. The application should attempt to make its GLEventListener.init() methods as side-effect-free as possible.

Constructor Summary

GLJPanel()
Creates a new GLJPanel component with a default set of OpenGL capabilities and using the default OpenGL capabilities selection mechanism.
GLJPanel(GLCapabilities capabilities)
Creates a new GLJPanel component with the requested set of OpenGL capabilities, using the default OpenGL capabilities selection mechanism.
GLJPanel(GLCapabilities capabilities, GLCapabilitiesChooser chooser, GLContext shareWith)
Creates a new GLJPanel component.

Method Summary

void
addGLEventListener(GLEventListener listener)
void
addNotify()
Overridden to track when this component is added to a container.
GLContext
createContext(GLContext shareWith)
void
display()
boolean
getAutoSwapBufferMode()
GLContext
getContext()
GL
getGL()
protected void
paintComponent(Graphics g)
Overridden to cause OpenGL rendering to be performed during repaint cycles.
void
removeGLEventListener(GLEventListener listener)
void
removeNotify()
Overridden to track when this component is removed from a container.
void
reshape(int x, int y, int width, int height)
Overridden to cause GLDrawableHelper.reshape(GLAutoDrawable,int,int,int,int) to be called on all registered GLEventListeners.
void
setAutoSwapBufferMode(boolean onOrOff)
void
setGL(GL gl)
void
setOpaque(boolean opaque)
void
setRealized(boolean realized)
boolean
shouldPreserveColorBufferIfTranslucent()
For a translucent GLJPanel (one for which setOpaque(false) has been called), indicates whether the application should preserve the OpenGL color buffer (GL_COLOR_BUFFER_BIT) for correct rendering of the GLJPanel and underlying widgets which may show through portions of the GLJPanel with alpha values less than 1.
void
swapBuffers()

Constructor Details

GLJPanel

public GLJPanel()
Creates a new GLJPanel component with a default set of OpenGL capabilities and using the default OpenGL capabilities selection mechanism.

GLJPanel

public GLJPanel(GLCapabilities capabilities)
Creates a new GLJPanel component with the requested set of OpenGL capabilities, using the default OpenGL capabilities selection mechanism.

GLJPanel

public GLJPanel(GLCapabilities capabilities,
                GLCapabilitiesChooser chooser,
                GLContext shareWith)
Creates a new GLJPanel component. The passed GLCapabilities specifies the OpenGL capabilities for the component; if null, a default set of capabilities is used. The GLCapabilitiesChooser specifies the algorithm for selecting one of the available GLCapabilities for the component; a DefaultGLCapabilitesChooser is used if null is passed for this argument. The passed GLContext specifies an OpenGL context with which to share textures, display lists and other OpenGL state, and may be null if sharing is not desired. See the note in the overview documentation on context sharing.

Method Details

addGLEventListener

public void addGLEventListener(GLEventListener listener)
Specified by:
addGLEventListener in interface GLAutoDrawable

addNotify

public void addNotify()
Overridden to track when this component is added to a container. Subclasses which override this method must call super.addNotify() in their addNotify() method in order to function properly.

Overrides:

addNotifyjava.awt.Component

createContext

public GLContext createContext(GLContext shareWith)
Specified by:
createContext in interface GLDrawable

display

public void display()
Specified by:
display in interface GLAutoDrawable

getAutoSwapBufferMode

public boolean getAutoSwapBufferMode()
Specified by:
getAutoSwapBufferMode in interface GLAutoDrawable

getContext

public GLContext getContext()
Specified by:
getContext in interface GLAutoDrawable

getGL

public GL getGL()
Specified by:
getGL in interface GLAutoDrawable

paintComponent

protected void paintComponent(Graphics g)
Overridden to cause OpenGL rendering to be performed during repaint cycles. Subclasses which override this method must call super.paintComponent() in their paintComponent() method in order to function properly.

Overrides:

paintComponentjavax.swing.JComponent

removeGLEventListener

public void removeGLEventListener(GLEventListener listener)
Specified by:
removeGLEventListener in interface GLAutoDrawable

removeNotify

public void removeNotify()
Overridden to track when this component is removed from a container. Subclasses which override this method must call super.removeNotify() in their removeNotify() method in order to function properly.

Overrides:

removeNotifyjava.awt.Component

reshape

public void reshape(int x,
                    int y,
                    int width,
                    int height)
Overridden to cause GLDrawableHelper.reshape(GLAutoDrawable,int,int,int,int) to be called on all registered GLEventListeners. Subclasses which override this method must call super.reshape() in their reshape() method in order to function properly.

Overrides:

reshapejava.awt.Component

setAutoSwapBufferMode

public void setAutoSwapBufferMode(boolean onOrOff)
Specified by:
setAutoSwapBufferMode in interface GLAutoDrawable

setGL

public void setGL(GL gl)
Specified by:
setGL in interface GLAutoDrawable

setOpaque

public void setOpaque(boolean opaque)

setRealized

public void setRealized(boolean realized)
Specified by:
setRealized in interface GLDrawable

shouldPreserveColorBufferIfTranslucent

public boolean shouldPreserveColorBufferIfTranslucent()
For a translucent GLJPanel (one for which setOpaque(false) has been called), indicates whether the application should preserve the OpenGL color buffer (GL_COLOR_BUFFER_BIT) for correct rendering of the GLJPanel and underlying widgets which may show through portions of the GLJPanel with alpha values less than 1. Most Swing implementations currently expect the GLJPanel to be completely cleared (e.g., by glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)), but for certain optimized Swing implementations which use OpenGL internally, it may be possible to perform OpenGL rendering using the GLJPanel into the same OpenGL drawable as the Swing implementation uses.

swapBuffers

public void swapBuffers()
Specified by:
swapBuffers in interface GLDrawable

Copyright 2005 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms.