Web Tutorial 2: Build an Applet

Author: Sean Luke

In this tutorial we will convert Conway's Game of Life from Tutorials 1 and 2 into an applet.

Modify the simulation.classes File

The simulation.classes file, located in the sim/display directory, specifies which files will appear in the pop-up list when the user chooses "New Simulation...". Edit this file to include only those simulations you wish the user to have access to. If you put the word 'ONLY' on a line all by itself, the user will not be able to type in a simulation classname by hand. If the file consists solely of the world 'ONLY', then the "New Simulation..." menu will be grayed out and the user won't be able to make a new simulation at all.

Build the Jar File

To make an applet you need two things: a web page which sets up the applet in HTML, and the applet code, typically embedded as a jar file. The jar file should contain all the non-standard classes that might be loaded during the course of running the applet, plus any pictures or auxillary data loaded by the applet.

Quick And Dirty Approach

We begin by building a basic jar file.

jar cvf mason.jar mason
This dumps the entire mason directory, java files and all, into a jar file called mason.jar.

Smaller Approach

You don't need all those files -- you just need:

To extract just these files and no others from the MASON directory and stuff them into a jar file, the easiest approach is (in UNIX or on a Mac) to go into the mason directory and type

make jar

Custom Jar Files

This is still a big file for users to download. If you really want to shrink this, you'll need to identify which classes and related files have to be extracted. Generally speaking the only images that need to be extracted for MASON to operate are the PNG files in the display and util/gui directories. The HTML files you need are probably just those in the application directories you wish to make available. And you'll need the simulation.classes file. As to the class files: generally you're safe if you just grab all the class files in the mason directory EXCEPT for various sim/app/... applications you don't wish to include. Certain tutorials have big pictures, and you'll save a lot of space by tossing those tutorials out. If you're not doing Java3D, you can toss out the files in the portrayal3d and display3d directories as well.

Construct the HTML Page

Applets have changed over the years and certain web browsers <cough>IE</cough> have their own weird ways of doing things. We've constructed a sample web page that you should be able to modify. It's called SimApplet.html and is located in the sim/display/ directory. In fact, the file you're reading is just an extension of it.

Your mason.jar file should be located on the website right next to this HTML file. Here's the result:

The following applet loads the entire MASON toolkit so it may take a second. Please note:

  • 3D demos cannot be run unless your computer has Java3D installed.
  • One or two 3D demos cannot be run as an applet period: they use too much memory.
  • Mac web browsers create some funkiness. Their Java is not hardware-accelerated and the 2D graphics is very slow. You'll have more luck running MASON directly as a Jav a 1.3.1 application. See the main web page for more information.
  • Mac web browsers except for Safari operate best when the Java 1.4.2 plugin is running.
  • Once a simulation is running, you can start another simulation under the File menu.
  • If you close a display, it is hidden -- you can get it back under the Displays tab in the console window. If you close the the console window, the simulation is quit.

If you want to fire up MASON directly on the simulation of your choice, you'll need to change an applet parameter called Simulation in your HTML file. Its value is by default sim.display.Console, but you can change it to a simulation class, for example, sim.app.heatbugs.HeatBugsWithUI

You can also change what is placed on the button by changing the Name parameter. Note that these parameters have to be changed in several locations in our version of the file, because different web browsers handle HTML in subtly different ways.