Using LWJGL with Applets

Support for Applets was introduced in LWJGL 1.0 Beta 2. It was implemented by adding a new class org.lwjgl.util.applet.LWJGLInstaller which temporarily installs the binaries needed by LWJGL in the users temp dir, and instructs the LWJGL core to load from the temp directory, instead of the commonly used Classpath or java.library.path. To make LWJGLInstaller work, it needs to be signed. The binaries that it installs needs to be signed with the same certificate, else it will fail.
Upon release of LWJGL 1.0, we will provide proper signed packages so that any user can run applets without having to invest in a certificate. It will still be possible to sign using your own certificate, should you wish to.

Installing binaries

In order to install the binaries and load those you only need to add the following block of code

try {
  LWJGLInstaller.tempInstall();
} catch (Exception le) {
 /* handle exception */
}

Packaging

For all of this to work, LWJGLInstaller expects a few things. First of all a jar file that has all required natives in it, is expected to be on classpath. A simple one-shot jar could look like this (generated by the LWJGL ant build file target called applet):

natives.jar
|
|-- windows_natives.jar (signed)
|-- linux_natives.jar (signed)
|-- macosx_natives.jar (signed)

However you're free to add a platform specific jar instead of one containing all of the platforms. The critical thing for all of this to work, is that a file called <platform>_natives.jar is available on classpath (where platform is either windows, linux or macosx). The contents of this file will be checked against the LWJGLInstall'ers certificate and extracted to the user temp directory (java.io.tmpdir property) in a subdirectory called .lwjglinstall/<timestamp>/.
Each LWJGLInstall.tempInstall() call will install into a semi-unique dir, so that different applets will load their own specific natives. Please note that applet with same classpath will be loaded with same classloader, and will therefor use the binaries installed on the first tempInstall() call.
Upon VM termination - unless configured otherwise - the LWJGLInstaller will run an uninstall method, which cleans up any installations. However due to the nature of natively loaded libraries, it isn't possible to uninstall the currently installed binaries, but rather any “old” installations not currently installed by the VM.

Example

The following code is a complete example of an LWJGL Applet, spinning a cube

package org.lwjgl.test.applet;
 
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Canvas;
 
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.AWTGLCanvas;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.applet.LWJGLInstaller;
 
public class AppletTestComplete extends Applet {
 
  public void init() {
 
    /* Install applet binaries */
    try {
      LWJGLInstaller.tempInstall();
    } catch (Exception le) {
      le.printStackTrace();
    }
 
    setLayout(new BorderLayout());
    try {
      Canvas canvas = new AppletCanvas();
      canvas.setSize(getWidth(), getHeight());
      add(canvas);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
  /*
   * @see org.lwjgl.opengl.AWTGLCanvas
   */
  public class AppletCanvas extends AWTGLCanvas {
 
    float angle = 0;
 
    /*
     * @see org.lwjgl.opengl.AWTGLCanvas#AWTGLCanvas()
     */
    public AppletCanvas() throws LWJGLException {
      // Launch a thread to repaint the canvas 60 fps
      Thread t = new Thread() {
        public void run() {
          while (true) {
            if (isVisible()) {
              repaint();
            }
            Display.sync(60);
          }
        }
      };
      t.setDaemon(true);
      t.start();      
    }
 
    /*
     * @see org.lwjgl.opengl.AWTGLCanvas#paintGL()
     */
    public void paintGL() {
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
      GL11.glMatrixMode(GL11.GL_PROJECTION_MATRIX);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, 640, 0, 480, 1, -1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW_MATRIX);
 
      GL11.glPushMatrix();
      GL11.glTranslatef(320, 240, 0.0f);
      GL11.glRotatef(angle, 0, 0, 1.0f);
      GL11.glBegin(GL11.GL_QUADS);
      GL11.glVertex2i(-50, -50);
      GL11.glVertex2i(50, -50);
      GL11.glVertex2i(50, 50);
      GL11.glVertex2i(-50, 50);
      GL11.glEnd();
      GL11.glPopMatrix();
 
      angle += 1;
 
      try {
        swapBuffers();
      } catch (Exception e) {
      }
    }   
  }
}
 
lwjgl/tutorials/applet-lwjglinstaller.txt · Last modified: 2008/02/03 20:45 by matzon
 
Recent changes RSS feed Creative Commons License Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki