OpenGL Drawable: Frame

Frame container for JOGL:

A container with a title and borders is called a Frame. Some other components such as buttons, labels, checkbox, radio buttons, lists are included in Frame for generating graphics.

Steps to create JOGL application using Frame:

  1. We have to create a new eclipse project. Give proper name to it and then click on the Finish button.
  2. Add required jar files for JOGL applications.
  3. Create a new java class in the package and import required packages.
  4. The class must implement the GLEventListener interface and override all required methods.
  5. The methods to be overridden are:
    • void display(GLAutoDrawable drawable) - GLAutoDrawable object is used to call this method. It is used to start OpenGL rendering on the client side. The logic required to draw graphical objects is contained in this method. It is mainly used to render OpenGL graphics.
    • void init(GLAutoDrawable drawable) - Once the OpenGL context is initialized, GLAutoDrawable’s object is used to call this method. It is generally used for one-time tasks such as lighting. This method is executed only once.
    • void reshape(GLAutoDrawble drawobj, int x, int y, int width, int height) - Whenever there is a need to change the position of an object on a window, this method is called using GLAutoDrawable’s object. It is called during the first repaint after resizing.
    • void dispose(GLAutoDrawable drawobj) - It is called before the OpenGL context is destroyed. It is called using GLAutoDrawable’s object. It is used to free all OpenGL resources used.
  1. Create the object of GLProfile class.
  2. Use the get() method of GLProfile class and mention proper OpenGL API.
  3. Create the object of GLCapabilities class using the constructor and pass GLProfile object to it.
  4. Construct GLCanvas object using constructor and pass GLCapabilities object to it.
  5. Call addGLEventListener() method with GLCanvas object.
  6. Using the setSize() method with GLCanvas object, we can set size of the frame.

JOGL Frame can be created using two ways,

  1. Using JOGL GLCanvas
  2. Using JOGL GLJPanel

1. GLCanvas:

This class is present in javax.media.opengl.awt package. It is the heavy-weight AWT component. It is the subclass of java.awt.Canvas class. It provides GUI to implement GLAutoDrawable functionalities. It cannot be perfectly integrated with swing but suitable to use with AWT components which are heavy-weight.

Template to create JOGL frame using GLCanvas:

GLCanvas gc = new GLCanvas();
JFrame f = new JFrame();        
f.getContentPane().add(gc); 
gc.addGLEventListener(GLEventListener listener);

2. GLJpanel:

This class is present in javax.media.opengl.awt package. It is lightweight. It is the subclass of javax.swing.JPanel class. It has OpenGL rendering support.

Template to create JOGL frame using GLCanvas:

GLJPanel gp = new GLJPanel();
JFrame f = new JFrame();
f.getContentPane().add(gp); 
frame.setContentPane(gp);
gp.addGLEventListener(GLEventListener listener);

GLCanvas is mainly used when we want to work with AWT components. GLJPanel is used with swing light-weight components. GLJPanel is slower than GLJCanvas. To define common functionalities both classes uses a common interface called GLAutoDrawable. Also GLEventListener’s method addGLEventListener() is used to perform desired action. setSize() method is further used to set frame dimensions. GLCanvas or GLJPanel object is first created and then this object is added with AWT or Swing frame. The add() method of Frame class is used for this purpose.