Using JOGL in Java Web Start

From JogampWiki
Revision as of 21:58, 30 July 2011 by Wwalker (talk | contribs)
Jump to navigation Jump to search

You can use JOGL in a Java Web Start (JWS) application, which lets you launch a full-featured, standalone Java program from a web page. This page shows an example of how to do this. The example program just draws one triangle that fills a resizable window.

Base class

First, a base class that we've used before here. This class draws one triangle, abstracting out all the pure OpenGL calls that don't depend on the choice of window toolkit.

package name.wadewalker.jogl2tests.onetriangle;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.glu.GLU;

public class OneTriangle {
    protected static void setup( GL2 gl2, int width, int height ) {
        gl2.glMatrixMode( GL2.GL_PROJECTION );
        gl2.glLoadIdentity();

        // coordinate system origin at lower left with width and height same as the window
        GLU glu = new GLU();
        glu.gluOrtho2D( 0.0f, width, 0.0f, height );

        gl2.glMatrixMode( GL2.GL_MODELVIEW );
        gl2.glLoadIdentity();

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

    protected static void render( GL2 gl2, int width, int height ) {
        gl2.glClear( GL.GL_COLOR_BUFFER_BIT );

        // draw a triangle filling the window
        gl2.glLoadIdentity();
        gl2.glBegin( GL.GL_TRIANGLES );
        gl2.glColor3f( 1, 0, 0 );
        gl2.glVertex2f( 0, 0 );
        gl2.glColor3f( 0, 1, 0 );
        gl2.glVertex2f( width, 0 );
        gl2.glColor3f( 0, 0, 1 );
        gl2.glVertex2f( width / 2, height );
        gl2.glEnd();
    }
}

Drawing a triangle with AWT

Now, a class that draws the triangle in an AWT Frame. Java Web Start can use any Java windowing toolkit, we've just chosen this one for convenience.

package name.wadewalker.jogl2tests.onetriangle;

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

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * A minimal program that draws with JOGL in an AWT Frame.
 *
 * @author Wade Walker
 */
public class OneTriangleAWT {

    static {
        // setting this true causes window events not to get sent on Linux if you run from inside Eclipse
        GLProfile.initSingleton( false );
    }

    public static void main( String [] args ) {
        GLProfile glprofile = GLProfile.getDefault();
        GLCapabilities glcapabilities = new GLCapabilities( glprofile );
        final GLCanvas glcanvas = new GLCanvas( glcapabilities );

        glcanvas.addGLEventListener( new GLEventListener() {
            
            @Override
            public void reshape( GLAutoDrawable glautodrawable, int x, int y, int width, int height ) {
                OneTriangle.setup( glautodrawable.getGL().getGL2(), width, height );
            }
            
            @Override
            public void init( GLAutoDrawable glautodrawable ) {
            }
            
            @Override
            public void dispose( GLAutoDrawable glautodrawable ) {
            }
            
            @Override
            public void display( GLAutoDrawable glautodrawable ) {
                OneTriangle.render( glautodrawable.getGL().getGL2(), glautodrawable.getWidth(), glautodrawable.getHeight() );
            }
        });

        final Frame frame = new Frame( "One Triangle AWT" );
        frame.add( glcanvas );
        frame.addWindowListener( new WindowAdapter() {
            public void windowClosing( WindowEvent windowevent ) {
                frame.remove( glcanvas );
                frame.dispose();
                System.exit( 0 );
            }
        });

        frame.setSize( 640, 480 );
        frame.setVisible( true );
    }
}

Setting up the application directory

  • Create a directory to hold your JWS application
  • Create a subdirectory =name/wadewalker/jogl2tests/onetriangle inside= your application directory. Or if you changed the packages of the files above, create a subdirectory that matches your package names.
  • Save the files above as =OneTriangle.java= and =OneTriangleAWT.java= inside the subdirectory.
  • Copy all JOGL JARs into the application directory. You can get the JARs from a JOGL autobuild here. Usually I pick the latest =jogl-<build number>-<date>= link, download all the =.7z= files inside, and unzip them with 7zip. These are the JARs you should have in your directory:
gluegen-rt.jar
nativewindow.all.jar
jogl.all.jar
newt.all.jar

gluegen-rt-natives-windows-i586.jar
jogl-natives-windows-i586.jar
nativewindow-natives-windows-i586.jar
newt-natives-windows-i586.jar

gluegen-rt-natives-windows-amd64.jar
jogl-natives-windows-amd64.jar
nativewindow-natives-windows-amd64.jar
newt-natives-windows-amd64.jar

gluegen-rt-natives-linux-i586.jar
jogl-natives-linux-i586.jar
nativewindow-natives-linux-i586.jar
newt-natives-linux-i586.jar

gluegen-rt-natives-linux-amd64.jar
jogl-natives-linux-amd64.jar
nativewindow-natives-linux-amd64.jar
newt-natives-linux-amd64.jar

gluegen-rt-natives-macosx-universal.jar
jogl-natives-macosx-universal.jar
nativewindow-natives-macosx-universal.jar
newt-natives-macosx-universal.jar

Compiling and JARing your program

Testing your program outside JWS

jarsigner -keystore testKeys gluegen-rt.jar ww
jarsigner -keystore testKeys nativewindow.all.jar ww
jarsigner -keystore testKeys jogl.all.jar ww
jarsigner -keystore testKeys newt.all.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-windows-i586.jar ww
jarsigner -keystore testKeys jogl-natives-windows-i586.jar ww
jarsigner -keystore testKeys nativewindow-natives-windows-i586.jar ww
jarsigner -keystore testKeys newt-natives-windows-i586.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-windows-amd64.jar ww
jarsigner -keystore testKeys jogl-natives-windows-amd64.jar ww
jarsigner -keystore testKeys nativewindow-natives-windows-amd64.jar ww
jarsigner -keystore testKeys newt-natives-windows-amd64.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-linux-i586.jar ww
jarsigner -keystore testKeys jogl-natives-linux-i586.jar ww
jarsigner -keystore testKeys nativewindow-natives-linux-i586.jar ww
jarsigner -keystore testKeys newt-natives-linux-i586.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-linux-amd64.jar ww
jarsigner -keystore testKeys jogl-natives-linux-amd64.jar ww
jarsigner -keystore testKeys nativewindow-natives-linux-amd64.jar ww
jarsigner -keystore testKeys newt-natives-linux-amd64.jar ww

jarsigner -keystore testKeys gluegen-rt-natives-macosx-universal.jar ww
jarsigner -keystore testKeys jogl-natives-macosx-universal.jar ww
jarsigner -keystore testKeys nativewindow-natives-macosx-universal.jar ww
jarsigner -keystore testKeys newt-natives-macosx-universal.jar ww