Contents / Previous / Next


JOGL Program Template ( JOGLframe.java )

import javax.media.opengl.*;
import javax.media.opengl.glu.*;
import com.sun.opengl.util.*; // GLUT, FPSAnimator

public class JOGLframe extends Frame
  implements GLEventListener
{
  /////////////////////// main ///////////////////////

  public static void main(String[] args) {
    JOGLframe frame = new JOGLframe();
  }

  //////////////// Variables /////////////////////////

  // Viewing Window Frame size.
  int width = 500;
  int height = 400;
  final boolean enableFullscreen = false;
  final boolean hideCursor = true;

  // Global canvas for event handling.
  GLCanvas canvas;

  // GUT and GLUT are global objects so that
  // they do not have to be newed in each frame.
  GLUT glut = new GLUT();
  GLU glu = new GLU();

  // Vars for animation.
  FPSAnimator animator;

  // Functionality from local modules.
  View view = new View();
  [...]

  ///////////////// Functions /////////////////////////

  public JOGLframe()
    {
      super("JOGL in an AWT Frame");

     [...]

      setSize( width, height );
      setLayout(new BorderLayout());

      initJOGL();

     [...]

      setVisible( true );
      requestFocus();
      canvas.requestFocusInWindow();
      pack();
    }


  private void initJOGL()
    {
      GLCapabilities caps = new GLCapabilities();
      caps.setDoubleBuffered( true );
      caps.setHardwareAccelerated( true );

      canvas = new GLCanvas( caps );
      canvas.addGLEventListener( this );

      add( canvas, BorderLayout.CENTER );

      // UserEventListener is tied to this canvas.
      UserEventMediator listener = new UserEventMediator
	( this, inputDevice, view, light, canvas );
      canvas.addKeyListener( listener );
      canvas.addMouseListener( listener );
      canvas.addMouseMotionListener( listener );

      // Canvas gets focus whenever frame is activated.
      addWindowListener( new WindowAdapter() {
	  public void windowActivated( WindowEvent e ) {
	    canvas.requestFocusInWindow(); } } );

      animator = new FPSAnimator( canvas, 20 );
      animator.setRunAsFastAsPossible( false );
      animator.start();
    }

  
  ////////// Methods defined by GLEventListener ///////////

  public void init( GLAutoDrawable drawable )
    {
      GL gl = drawable.getGL();

      drawable.setGL( new DebugGL(drawable.getGL() ));
      System.out.println("Init GL is " + gl.getClass().getName());

      // On some systems the reshape call does not seem to 
      // happen automatically on init.
      // Set the projection and viewport.
      reshape( drawable, 0, 0, width, height );

      gl.glClearColor( 0f, 0f, 0f, 0.0f );

      // GL.GL_FRONT[_AND_BACK], GL.GL_LINE, GL.GL_FILL
      gl.glPolygonMode( GL.GL_FRONT_AND_BACK, GL.GL_FILL );

      [...]

      blend.init( gl );
      light.init( gl );

      [...]
    }


  public void reshape(GLAutoDrawable drawable,
		      int x, int y, int width, int height) 
    { 
      //System.out.println("reshape()");

      GL gl = drawable.getGL();

      gl.glMatrixMode(GL.GL_PROJECTION);
      gl.glLoadIdentity();

      gl.glFrustum( -0.1f, 0.1f, -0.1f, 0.1f, 0.2f, 20.0f );
      //glu.gluOrtho2D(0f, (float)width, 0f, (float)height );

      [...]

      gl.glViewport( 0, 0, width, height );
    }


  public void displayChanged( GLAutoDrawable drawable, 
			      boolean modeChanged, 
			      boolean deviceChanged ) 
    { 
      System.out.println ("displayChanged()");
    }


  public void display(GLAutoDrawable drawable)
    {
      //System.out.println("display");//ddd
      GL gl = drawable.getGL();

      // Clear Framebuffer and Z-Buffer.
      gl.glClear( GL.GL_COLOR_BUFFER_BIT | 
		  GL.GL_DEPTH_BUFFER_BIT );
      // Default color.
      gl.glColor3f(1f, 1f, 1f);

      gl.glMatrixMode(GL.GL_MODELVIEW);
      gl.glLoadIdentity();
	
      // Viewing Transformation.
      view.setCamera( gl );
      //setCameraLookAt( glu );

      // Model Transformation and Draw Content.
      drawSomething( gl );

      //getAndPrintError( gl );

      gl.glFlush();

      //imaging.saveFrameAsPNG( gl, null );
    }

  /////////////////// Error and Exit //////////////////

  int getAndPrintError( GL gl ) 
    {
      int errorCode = gl.glGetError();
      System.out.println( errorCode );
      return errorCode;
    }

  // Called via user event and on window closing.
  void exit()
    {
      animator.stop();
      if( enableFullscreen ) { fullscreen.exit(); }
      System.exit( 0 ); 
    }


    /////////////////////// Drawings ////////////////////

    private void drawSomething( GL gl ) 
    {
       // Start here ... 
    }

}