The goal of this section is the construction of a very simple applet from components in the library. This tutorial is adapted from the article The Probability/Statistics Object Library
, published in the Journal of Online Mathematics and Its Applications (2004). To work through this example yourself, you will need the Java Software Development Kit (available at the Sun Java), a simple text editor (such as Windows Notepad), and of course, your Java-enabled browser.
Note that this tutorial is not relevant if you simply want to use one of the applets without modification. In that case, you simply need to download the Java Archive (JAR) file objects.jar and write the appropriate applet tag in your HTML page. Detailed instructions are given on the page for each applet.
The Hello World applet will require two objects from the library: the Experiment object which provides a basic shell for a random experiment, and the Coin object. We will need to modify the objects, so we will need the source files. First, download objects.zip, which contains the source and resource files for all objects in the library. Next extract the files in this ZIP archives into a folder of your choice. Next, create a new folder, and copy the following files from the object library into the new folder. (The file Coin.java is in the devices package; the other files are in the experiments package.)
The first two are the Java source files for our Experiment and Coin objects, while the next four are tiny PNG image files for the buttons on the main tool bar.
Next open the file Experiment.java with your text editor and remove the first programming line:
package edu.uah.math.experiments;This step merely simplifies the names of the objects and allows us to put all of our files in one folder. Similarly, open the file Coin.java and remove the first programming line:
package edu.uah.math.devices;
Next, create a new file called HelloWorld.java, type the following lines, and save the file to your folder:
public class HelloWorld extends Experiment{ private Coin coin = new Coin(); public void init(){ super.init(); coin.setHeadLabel("Hello"); coin.setTailLabel("World"); addComponent(coin, 0, 0, 1, 1); } public void doExperiment(){ super.doExperiment(); coin.toss(); } public void update(){ super.update(); coin.setTossed(true); } public void reset(){ super.reset(); coin.setTossed(false); } }
Let's try to understand what we have done.
At this point, you should be able to compile HelloWorld.java without errors using the Java compiler in the Java Software Development kit (or using a more sophisticated Java development environment if you have one).
Our final task is to create a stub HTML (web) file so that we can view the applet in our browser. With your text editor, create a new file called HelloWorld.html, type the lines below, and save the file to your folder.
<html> <head> <title>Hello World</title> </head> <body> <p><applet code="HelloWorld.class" width="450" height="300">HelloWorld</applet></p> </body> </html>
The important tag is the applet tag, which merely has a reference to the HelloWorld class file and specifies the width and height of the applet.
That's it! You should be able to open HelloWorld.html with your browser and play with your applet. Click on the step button to toss the coin and see either Hello
or World
, depending on whether you coin landed heads or tails. Click on the run button and practice changing the update and stop settings. A screen shot of the applet is given below; you can click on the image to see the live applet, if you did not construct your own applet.