Using JOGL in a Java applet

You can use JOGL in a Java applet, which lets you run a Java program embedded in a web page. This page shows an example of how to do this. The example program just draws one triangle that fills a fixed-size frame in a web page.

= Base class =

We'll use the same triangle-drawing base class for this example that we did in the Java Web Start example. Copy the code to a file called OneTriangle.java.

= Applet class =

Now we need a simple class that extends java.applet.Applet to form the top level of our program. Copy this code to a file called OneTriangleAWTApplet.java.

package name.wadewalker.jogl2tests.onetriangle;

import java.applet.*; import java.awt.*;

import javax.media.opengl.GLAnimatorControl; 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 com.jogamp.opengl.util.FPSAnimator;

/** * A minimal applet that draws with JOGL in a browser window. * * @author Wade Walker */ @SuppressWarnings("serial") public class OneTriangleAWTApplet extends Applet {

private GLAnimatorControl glanimatorcontrol;

public void init { GLProfile.initSingleton( false ); setLayout( new BorderLayout );

final GLCanvas glcanvas = new GLCanvas; 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 );            }        }); glcanvas.setSize( getSize ); add( glcanvas, BorderLayout.CENTER ); glanimatorcontrol = new FPSAnimator( glcanvas, 30 ); }

public void start { glanimatorcontrol.start; }   public void stop { glanimatorcontrol.stop; }   public void destroy { } }

= Applet web page =

Finally, the applet needs a web page to display in. Note that this file sets the fixed size of the applet window. Copy this code to a file called OneTriangleApplet.html.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> OneTriangle Applet Test

 Test of OneTriangle as an applet. 



     <param name="progressbar" value="true"> <param name="jnlpNumExtensions" value="1"> <param name="jnlpExtension1" value="JOGL.jnlp"> <param name="jnlp_href" value="OneTriangleApplet.jnlp">

</P>

 The applet should be above this. </P>

= Setting up the applet directory =


 * Create a directory to hold your applet.
 * Put your OneTriangleApplet.html</tt> file in that new directory.
 * Create a subdirectory name/wadewalker/jogl2tests/onetriangle</tt> inside your applet directory. Or if you changed the packages of the files above, create a subdirectory that matches your package names.
 * Put your OneTriangle.java</tt> and OneTriangleAWT.java</tt> files inside the subdirectory.
 * Copy all the JOGL JARs into the applet directory as described here.
 * Copy applet-launcher.jar</tt> into the applet directory. Currently this file is not part of the JOGL distribution. I built it from source I downloaded from Sven's repository. This launcher seems to be ignored when you launch the applet in IE, Firefox, and Chrome on Windows, but it is used when you launch in Safari or with the command-line applet viewer.

= Compiling and JARing your program =

You can compile and JAR the program exactly as we did for the Java Web Start case.

= Writing the JNLP files =

New-style Java applets use two Java Network Launching Protocol (JNLP) files to find the JAR files that make up the applet. Create both JNLP files exactly as in the Java Web Start case.

= Signing your JARs =

All the JARs in your applet must be signed to work correctly in a web browser. Do this just as in the Java Web Start case, with one addition for the applet launcher JAR:

jarsigner -keystore testKeys applet-launcher.jar ww

= Running the applet in a browser =

To run the applet in a browser on Windows, right-click the OneTriangleApplet.html</tt> file and click "Open with > Internet Explorer|Chrome|Firefox|Safari". The result should look like this:



= Clearing the applet cache =

Java caches applet JAR and JNLP files to reduce load time. Unfortunately, if you edit one of these files and re-launch, sometimes you won't see your changes because Java is still using the cached copy. To clear the cache on Windows, click Start > Control Panel > Java</tt>, then Temporary Internet Files > Settings... > Delete Files... > OK</tt>.

From the command line, you can just delete %USERPROFILE%\AppData\LocalLow\Sun\Java\Deployment\cache</tt>.

The Java applet launcher (if it's used) may also cache files. You can clear it by deleting %USERPROFILE%\.jnlp-applet</tt>.

= Running the applet with the Java applet viewer =

The Java JDK comes with a program called appletlauncher</tt>, which you can use to launch your applet from the command line without the use of a browser. This launcher works the way that older Java browser plugins used to work (and the way Safari's plugin still works), so it can help test compatibility.

When you launch with appletlauncher</tt>, it also uses the JNLPAppletLauncher</tt> class that we specified in OneTriangleApplet.html</tt>, which the newer Java plugins seem to ignore.

The downside of that is the JNLPAppletLauncher</tt> doesn't yet support relative paths, so you'll need to change the value of <tt>jnlpExtension1</tt> in <tt>OneTriangleApplet.html</tt> from <tt>"./"</tt> to an absolute path like <tt>"file://localhost/C:/Users/my/applet/dir/JOGL.jnlp"</tt>, and the value of <tt>codebase</tt> in <tt>JOGL.jnlp</tt> from <tt>"./"</tt> to an absolute path like <tt>"file://localhost/C:/Users/my/applet/dir/"</tt>.

Then, to give the applet the permissions it needs to run outside the browser, create a file named <tt>all.policy</tt> in your applet directory, and put this text in it:

grant { permission java.security.AllPermission; };

Then finally you can launch the applet from the command line like this:

<tt>appletviewer -J-Djava.security.policy=all.policy OneTriangleApplet.html</tt>

The result should look like this:



You can also use the applet viewer to remotely debug your code like this:

<tt>appletviewer -J-Djava.security.policy=all.policy -J-Xdebug -J-Djava.compiler=NONE -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 OneTriangleApplet.html</tt>

When you hit return, the applet will suspend on startup, so you can connect to the applet process with your debugger of choice.

= Running the applet from inside Eclipse =

*This section is unfinished*

If you set up your applet as an Eclipse project, you can run it in the applet viewer by right-clicking the applet project and clicking "Run As > Java Applet". This is similar to running the applet via <tt>appletviewer</tt> on the command line, but allows local instead of remote debugging.

= Running the applet from a web server =

*This section is unfinished*

Once you're sure that the applet launches correctly from the local file system, you're ready to deploy it to a web server.


 * Create or choose an applet directory on your web server.
 * Change the <tt>codebase</tt> entries in the JNLP from directory names to URLs that refer to the applet directory on your web server.
 * Copy all the HTML, JAR, and JNLP files from your local applet directory to the applet directory on your web server.
 * Put a link to the <tt>OneTriangleApplet.html</tt> file on a page on your web server so users can find your applet page.