Using JOGL in AWT SWT and Swing: Difference between revisions
(Created) |
No edit summary |
||
Line 1: | Line 1: | ||
You can use JOGL with three different Java window toolkits: AWT, SWT, and Swing. This page shows examples of all three | You can use JOGL with three different Java window toolkits: AWT, SWT, and Swing. This page shows examples of all three toolkits, plus variations on two of them. | ||
= How to choose = | = How to choose a window toolkit = | ||
Usually the | Usually the window toolkit you use for JOGL is dictated by your application. If you're writing an Eclipse-based application, it makes sense to use SWT, since it's the native windowing system of Eclipse. Otherwise, Swing is probably the way to go. If you want to wrap native controls instead of drawing in software like Swing, and you're not writing an Eclipse app, AWT is your best choice. | ||
= Base class = | = Base class = | ||
Line 9: | Line 9: | ||
First, a base class that we'll use in all five examples. This class abstracts out all the pure OpenGL calls that don't depend on the choice of windowing toolkit. | First, a base class that we'll use in all five examples. This class abstracts out all the pure OpenGL calls that don't depend on the choice of windowing toolkit. | ||
<pre> | |||
import javax.media.opengl.GL; | |||
import javax.media.opengl.GL2; | |||
import javax.media.opengl.glu.GLU; | |||
public class OneTriangle { | |||
protected static void setup( GL2 gl2, int width, int height ) { | |||
gl2.glMatrixMode( GL2.GL_PROJECTION ); | |||
gl2.glLoadIdentity(); | |||
// coordinate system origin at lower left with width and height same as the window | |||
GLU glu = new GLU(); | |||
glu.gluOrtho2D( 0.0f, width, 0.0f, height ); | |||
gl2.glMatrixMode( GL2.GL_MODELVIEW ); | |||
gl2.glLoadIdentity(); | |||
gl2.glViewport( 0, 0, width, height ); | |||
} | |||
protected static void render( GL2 gl2, int width, int height ) { | |||
gl2.glClear( GL.GL_COLOR_BUFFER_BIT ); | |||
// draw a triangle filling the window | |||
gl2.glLoadIdentity(); | |||
gl2.glBegin( GL.GL_TRIANGLES ); | |||
gl2.glColor3f( 1, 0, 0 ); | |||
gl2.glVertex2f( 0, 0 ); | |||
gl2.glColor3f( 0, 1, 0 ); | |||
gl2.glVertex2f( width, 0 ); | |||
gl2.glColor3f( 0, 0, 1 ); | |||
gl2.glVertex2f( width / 2, height ); | |||
gl2.glEnd(); | |||
} | |||
} | |||
</pre> | |||
= JOGL in AWT = | = JOGL in AWT = |
Revision as of 21:21, 27 January 2011
You can use JOGL with three different Java window toolkits: AWT, SWT, and Swing. This page shows examples of all three toolkits, plus variations on two of them.
How to choose a window toolkit
Usually the window toolkit you use for JOGL is dictated by your application. If you're writing an Eclipse-based application, it makes sense to use SWT, since it's the native windowing system of Eclipse. Otherwise, Swing is probably the way to go. If you want to wrap native controls instead of drawing in software like Swing, and you're not writing an Eclipse app, AWT is your best choice.
Base class
First, a base class that we'll use in all five examples. This class abstracts out all the pure OpenGL calls that don't depend on the choice of windowing toolkit.
import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.glu.GLU; public class OneTriangle { protected static void setup( GL2 gl2, int width, int height ) { gl2.glMatrixMode( GL2.GL_PROJECTION ); gl2.glLoadIdentity(); // coordinate system origin at lower left with width and height same as the window GLU glu = new GLU(); glu.gluOrtho2D( 0.0f, width, 0.0f, height ); gl2.glMatrixMode( GL2.GL_MODELVIEW ); gl2.glLoadIdentity(); gl2.glViewport( 0, 0, width, height ); } protected static void render( GL2 gl2, int width, int height ) { gl2.glClear( GL.GL_COLOR_BUFFER_BIT ); // draw a triangle filling the window gl2.glLoadIdentity(); gl2.glBegin( GL.GL_TRIANGLES ); gl2.glColor3f( 1, 0, 0 ); gl2.glVertex2f( 0, 0 ); gl2.glColor3f( 0, 1, 0 ); gl2.glVertex2f( width, 0 ); gl2.glColor3f( 0, 0, 1 ); gl2.glVertex2f( width / 2, height ); gl2.glEnd(); } }
JOGL in AWT
The Abstract Window Toolkit (AWT) was Java's first window toolkit. It's a thin layer over each platform's native window toolkit.
JOGL in SWT
The Standard Widget Toolkit (SWT) is a thin layer over each platform's native window toolkit. SWT is the toolkit used in Eclipse, so it makes sense to use SWT in Eclipse-based applications.
JOGL in SWT using the SWT-AWT bridge
The SWT-AWT bridge was created to allow SWT applications to use the AWT GLCanvas. You should use this if you have problems with the SWT GLCanvas.
JOGL in Swing
Swing is the primary Java window toolkit since Java 1.2 in 1997. It's based on AWT at a low level, but draws all its own controls instead of wrapping native controls.
JOGL in Swing using the GLJPanel
In cases where the AWT GLCanvas has problems integrating with Swing components correctly, the GLJPanel can be used instead. It's supposed integrate 100% correctly into Swing, but with lower performance.