Interactive Graphics Designing With JOGL

Ajay Tripathi (Sr. Lecturer)

Java OpenGL (JOGL) is a wrapper library that allows a standard graphics cross-platform application programming interface (API) called OpenGL to be used in Java. OpenGL is the premier environment for developing portable, interactive game designing those requires 2D and 3D graphics. Since 1992, OpenGL has become the industry's most widely used and supported 2D and 3D graphics API. It brings thousands of applications to a wide variety of computer platforms. Developers can leverage the power of OpenGL across all popular desktop and workstation platforms, ensuring wide application deployment. This article offers an overview of the main features of JOGL and provides some insight about how to use them.

Designing of JOGL

The base OpenGL C API is accessed in JOGL via Java Native Interface (JNI) calls; this means that the system must support OpenGL for JOGL to work. JOGL differs from some Java OpenGL wrapper libraries in that it merely exposes the procedural OpenGL API via methods on a few classes, rather than attempting to map OpenGL functionality onto the object-oriented programming paradigm.

The majority of the JOGL code is auto generated from the OpenGL C header files via a conversion tool named Gluegen. The procedural and state machine nature of OpenGL is inconsistent with the typical method of programming under Java, which is difficult to many programmers. The straightforward mapping of the OpenGL C API to Java methods makes conversion of existing C applications much simpler. The thin layer of abstraction provided by JOGL makes runtime execution efficient; however, it is more difficult to code than higher-level abstraction libraries like Java 3D.

Getting started with JOGL

In order to run any JOGL code, there is a need at least Sun J2SDK 1.4.2 and JOGL binaries. Java classpath is to be set before starting. To use the JOGL APIs, there is a need to include the JOGL package in the code.

To do this, include the following import directive:


Then there is a need to create a window in which all drawings will place.

In AWT, this is done with the new Frame (”Hello World”) constructor calling.Now after having a frame/window, there is a need to add a JOGL GLCanvas or GLJPanel to the frame so that OpenGL can be used.

Let’s create a new GLCanvas or a new GLJPanel. Once we have a new canvas/panel, we will add it to the frame, which will cause the frame to render the canvas/panel with the rest of the window. This means we could easily add OpenGL capabilities to an existing Java GUI.

public static void main(String[] args)
    Frame frame1 = new Frame("Hello World");
    GLCanvas canvas1 =
GLDrawableFactory.getFactory().createGLCanvas(new GLCapabilities());

We will set up a few more parameters for the frame, including window size, background color, and what to do when the user closes the window (in this case, we close the entire application). Now that we have these basic parameters set up, we just have to tell the frame to render itself and start accepting user input:

    frame.setSize(300, 300);
frame.addWindowListener(new WindowAdapter())
        public void windowClosing(WindowEvent e)

This provides a basic environment for JOGL/OpenGL rendering. The next step in the process is to add event handlers to GLCanvas/GLPanel so that we can respond to user events (i.e., keyboard and mouse events) and system events (i.e., draw/render events, timers, etc.). JOGL and AWT register user actions and system requests as events. The most basic set of events that we are interested in is the set of events created by OpenGL, namely the initialize, reshape, and display window events. One can subscribe to these events by creating a One can also subscribe to keyboard, mouse button, and mouse movement events (all from java.awt); these will allow for basic user interaction.

// make a new JOGLEventListener that is tied to this cavnas

    JoglEventListener listener = new JoglEventListener(canvas);

Next, we will create a new class called JoglEventListener, which implements all of the methods required for the four set of events to which we’ve subscribed. We will pass the associated GLCanvas to the constructor of the event listener. This is necessary to properly process the display/refresh event.

// necessary imports
 import java.awt.event.*;

// a class which listens for various events raised by our GLCanvas
 public class JoglEventListener implements GLEventListener,  
                 KeyListener, MouseListener, MouseMotionListener {
// keep pointer to associated canvas so we can refresh the screen (equivalent to glutPostRedisplay())    
     private GLCanvas canvas;      
    // constructor
     public JoglEventListener(GLCanvas canvas) {
        this.canvas = canvas;

    // ...
    // event handler functions
    // ...

There are four events that listen for when we implement GLEventListener: init(), display(), reshape(), and displayChanged().
init() is an initialization function that OpenGL/JOGL will call when setting up the graphics information when program starts. We typically apply global settings and initialize the GL instance with program’s options.

public void init(GLDrawable gLDrawable) {
        final GL gl = glDrawable.getGL();
        final GLU glu = glDrawable.getGLU();  
        glu.gluOrtho2D(-1.0f, 1.0f, -1.0f, 1.0f); // drawing square

display() is very similar to java.awt.Component.paint() in that it is called each time the canvas needs to be repainted. In the code below, it draws the objects (triangle, diamond, rectangle) on the specified GLDrawable canvas.

public void display(GLDrawable gLDrawable) {
        // clear the screen
        // make GLObjects to represent the current state of each object
         IGLObject glDiamond = this._diamond.makeGLObject();
        IGLObject glRectangle = this._rectangle.makeGLObject();
        IGLObject glTriangle = this._triangle.makeGLObject();   
 // draw the three shapes

reshape() is used to resize the display window. Here it is necessary to tell the canvas how to respond to the fact that the user has changed the window size — namely, we need to change the viewport size to match.

public void reshape(GLDrawable gLDrawable, int x, int y, int width, int height) {
        final GL gl = gLDrawable.getGL();
        final GLU glu = gLDrawable.getGLU();
        gl.glViewport(0, 0, width, height); // update the viewport

displayChanged() is the last fundamental method in JOGL. In some cases, the display mode can change while the program is running. It is not actually implemented in JOGL, but it may be at some point in the future, so be sure to check the JOGL JavaDocs prior to using it.

public void displayChanged(GLDrawable gLDrawable, boolean modeChanged, boolean deviceChanged) {
        // do nothing

After creating GLCanvas or GLJPanel and registered with OpenGL class to them, use OpenGL in a usual way like in C/C++.

Multithreading concerns

A multithreaded approach is taken in AWT and JOGL. This means that the AWT and JOGL libraries will spawn multiple threads to handle various events: a thread for handling mouse and keyboard events; a thread for system events; a thread for display/rendering events, etc. Programmers should make sure that program handles shared data in a synchronized manner.
For more information visit the web site


Author Details

Ajay Tripathi Sr. Lecturer
Jaipuria Institute of Management, Ghaziabad