LAB CPCS-391, CPIT-285
JOGL EASY LEARNING
Graphical Shape Rhombus
Important interfaces and classes
GLAutoDrawable interface
This interface supplies an event based mechanism (GLEventListener) for
performing OpenGL rendering. GLAutoDrawable automatically creates a
primary rendering context which is associated with GLAutoDrawable for
the lifetime of the object.
Interface: GLAutoDrawble
Package: javax.media.opengl
GLCapabilities class
This class specifies a set of OpenGL capabilities. It takes GLCapabilities
object as a parameter. The GLCapabilities class describes the desired
capabilities that a rendering context must support, such as the OpenGL
profile.
class: GLCapabilities package: javax.media.opengl
GLEventListener interface
You can find GLEventListener interface in javax.media.opengl package.
Interface: GLEventListener
Package: javax.media.opengl
LAB CPCS-391, CPIT-285
JOGL EASY LEARNING
Graphical Shape Rhombus
import javax.media.opengl.GLAutoDrawable
A higher-level abstraction than GLDrawable which supplies an event based
mechanism (GLEventListener) for performing OpenGL rendering
Rendering is the process of generating an image from a 2D or 3D model (or models
in what collectively could be called a scene file), by means of computer programs.
Also, the results of such a model can be called a rendering. A scene file contains
objects in a strictly defined language or data structure; it would contain geometry,
viewpoint, texture, lighting, and shading information as a description of the virtual
scene.
import javax.media.opengl.GLCapabilities
Fetches the GLCapabilities corresponding to the chosen OpenGL capabilities (pixel
format / visual / GLProfile) for this drawable. On some platforms, the pixel format
is not directly associated with the drawable; a best attempt is made to return a
reasonable value in this case
Specifies
a
set
of
OpenGL
capabilities.
At
creation
time
of
a GLDrawable using GLDrawableFactory, an instance of this class is passed, describing the
desired capabilities that a rendering context must support. such as the OpenGL profile.
import avax.media.opengl.GLEventListener
Declares events which client code can use to manage OpenGL rendering into
a GLAutoDrawable. At the time any of these methods is called.
Help:
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. The
example program just draws one triangle that fills a resizable window.
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
import javax.media.opengl.awt.GLCanvas
Creates a new GLCanvas component with a default set of OpenGL capabilitie
import javax.swing.JFrame;
Constructs a frame that is initially invisible.
1. Import Libraries
import
javax.media.opengl.GLAutoDrawable;
import
javax.media.opengl.GLCapabilities; import avax.media.opengl.GLEventListener;
LAB CPCS-391, CPIT-285
JOGL EASY LEARNING
import javax.media.opengl.GLProfile; import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
2. Define Class
public class Rhombus
3. Display Method
public void display( GLAutoDrawable drawable)
4. Define Constant
final GL2 gl = drawable.getGL().getGL2();
5. Draw edge1
gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( 0.0f,0.75f,0 );
gl.glVertex3f( -0.75f,0f,0 );
6. Draw edge1
edge2 gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( -0.75f,0f,0 );
gl.glVertex3f( 0f,-0.75f, 0 );
7. Draw edge3
gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( 0f,-0.75f, 0 );
gl.glVertex3f( 0.75f,0f, 0 );
8. Draw edge4
gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( 0.75f,0f, 0 );
gl.glVertex3f( 0.0f,0.75f,0 );
flush
overrdie
9. Define Methods
public void dispose
public void init
public void reshape
override
public void reshape( GLAutoDrawable arg0, int arg1, int arg2,
int arg3,
int arg4 ) {
10.
Deinf Main
public static void main( String[] args ) {
11.
In Last
Getting the capabilities object of GL2 profile
LAB CPCS-391, CPIT-285
JOGL EASY LEARNING
final GLProfile profile = GLProfile.get( GLProfile.GL2 );
GLCapabilities capabilities = new GLCapabilities( profile );
The canvas
GLCanvas glcanvas = new GLCanvas( capabilities );
Rhombus rhombus = new Rhombus();
final glcanvas.addGLEventListener( rhombus );
glcanvas.setSize( 400, 400 );
creating frame
final JFrame frame = new JFrame ( "Rhombus" );
JFrame ( "Rhombus" );
Adding canvas to frame
frame.getContentPane().add( glcanvas );
frame.setSize( frame.getContentPane().getPreferredSize() );
frame.setVisible( true );
Let us go through a program to draw a rhombus using GL_LINES:
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import avax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
public class Rhombus implements GLEventListener{
@Override
public void display( GLAutoDrawable drawable ) {
final GL2 gl = drawable.getGL().getGL2();
//edge1
gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( 0.0f,0.75f,0 );
gl.glVertex3f( -0.75f,0f,0 );
LAB CPCS-391, CPIT-285
JOGL EASY LEARNING
gl.glEnd();
//edge2
gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( -0.75f,0f,0 );
gl.glVertex3f( 0f,-0.75f, 0 );
gl.glEnd();
//edge3
gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( 0f,-0.75f, 0 );
gl.glVertex3f( 0.75f,0f, 0 );
gl.glEnd();
//edge4
gl.glBegin( GL2.GL_LINES );
gl.glVertex3f( 0.75f,0f, 0 );
gl.glVertex3f( 0.0f,0.75f,0 );
gl.glEnd();
gl.glFlush();
}
@Override
public void dispose( GLAutoDrawable arg0 ) {
//method body
}
@Override
public void init(GLAutoDrawable arg0 ) {
// method body
}
@Override
public void reshape( GLAutoDrawable arg0, int arg1, int arg2, int arg3,
LAB CPCS-391, CPIT-285
int arg4 ) {
// method body
}
public static void main( String[] args ) {
JOGL EASY LEARNING
LAB CPCS-391, CPIT-285
JOGL EASY LEARNING
//getting the capabilities object of GL2 profile
final GLProfile profile = GLProfile.get( GLProfile.GL2 );
GLCapabilities capabilities = new GLCapabilities( profile );
// The canvas
final GLCanvas glcanvas = new GLCanvas(
capabilities ); Rhombus rhombus = new Rhombus();
glcanvas.addGLEventListener( rhombus );
glcanvas.setSize( 400, 400 );
//creating frame final JFrame frame = new
JFrame ( "Rhombus" );
//adding canvas to frame frame.getContentPane().add(
glcanvas ); frame.setSize(
frame.getContentPane().getPreferredSize() );
frame.setVisible( true );
}
}
Rhombus.java
If you compile and execute the above program, you get following output. It shows
a rhombus generated using GL_LINES of glBegin() method.
LAB CPCS-391, CPIT-285
JOGL EASY LEARNING
© Copyright 2026 Paperzz