JOGL - Canvas with AWT


Advertisements

This chapter explains you how to draw a JOGL basic frame using Canvas with AWT frame. In here we will construct a AWT Frame and add the canvas object to the AWT frame using the add() method of the frame class.

Below given are the steps to write a program which creates a JOGL basic frame with the combination of JOGL's Canvas class and AWT's Frame class.

Step1: Creating the Class

Initially create a class that implements GlEventListener interface and import the package javax.media.opengl. Implement all four methods display(), dispose(), reshape(), init(). Since this is the basic frame, primitive tasks such as creating canvas class, adding it to frame were discussed. All the GLEVentListener interface methods were left unimplemented.

Step2: Preparing the Canvas

(a) Constructing the GLCanvas class object

final GLCanvas glcanvas = new GLCanvas( xxxxxxx );

//here capabilities obj should be passed as parameter

(b) Instantiating the GLCapabilities class

GLCapabilities capabilities = new GLCapabilities( xxxxx );

//here profile obj should be passed as parameter

(c) Generating GLProfile object

As it is the static method, it is invoked using class name. Since this tutorial is about JOGL2, let us generate GL2 interface object.

final GLProfile profile = GLProfile.get( GLProfile.GL2 );

// both, variable and method are static hence both are called using class name.

Let us see the code snippet for canvas.

//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);

(d) Now add GLEventListener to the canvas using the method addGLEventListener(). This method needs object of GLEventListener interface as parameter. Hence, pass object of a class that implements GLEventListener.

BasicFrame basicframe = newBasic Frame( );// class which implements
GLEventListener interface
glcanvas.addGLEventListener( basicframe );

(e) Set size of the frame using setSize() method inherited by GLCanvas from javax.media.opengl.awt.AWTGLAutoDrawable.

glcanvas.setSize( 400, 400 );

Now you are ready with GLCanvas.

Step3: Creating the Frame

Create the frame by instantiating the Frame class Object of JSE AWT frame component.

Add canvas to it and make the frame visible.

//creating frame
final Frame frame = new frame( " Basic Frame" );

//adding canvas to frame
frame.add( glcanvas );
frame.setVisible( true ); 

Step 4: Viewing the Frame in Full Screen

To view the frame in full screen, get the default screen size using java.awt.Toolkit class. Now, using those default screen size dimensions, set the frame size using setSize() method.

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frame.setSize(screenSize.width, screenSize.height);

Let us go through the program to generate the basic frame using AWT −

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

import javax.swing.JFrame;

public class BasicFrame implements GLEventListener {

   @Override
   public void display(GLAutoDrawable arg0) {
      // method body
   }
	
   @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, int arg4) {
      // method body
   }
	
   public static void main(String[] args) {
   
      //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);
      BasicFrame b = new BasicFrame();
      glcanvas.addGLEventListener(b);        
      glcanvas.setSize(400, 400);
        
      //creating frame
      final Frame frame = new Frame (" Basic Frame");
        
      //adding canvas to frame
      frame.add(glcanvas);
      frame.setSize( 640, 480 );
      frame.setVisible(true);
   }
	
}

If you compile and execute the above program, the following output is generated. It shows a basic frame formed when we use GLCanvas class with AWT −

Basic Frame
Advertisements