This is a MIRROR of T.A. Ken Been's tutorial on Java Applet


Writing and Running Java Applets

Java applets are written for running within a web browser.

Writing Applets

An "applet" is a class that extends the java.applet.Applet class. For example, see the NestedApplet class on page 23 of your book. (Since java.applet.* is imported, Applet is shorthand for java.applet.Applet.)

Applets do not use a main() method to control execution. Instead, execution is controlled through the methods init(), start(), stop(), and destroy(). The Applet class has empty implementations of these methods; to make an applet that does something, you must override at least one of these with a method that does something.

The init() method is executed when the applet is first loaded into the browser.

The start() method is executed whenever the page containing the applet is entered. So, if you look at another page, then come back to the applet's page, start() will be executed again.

The stop() method is executed whenever the page containing the applet is exited.

The destroy() method is executed just before the applet is terminated. This would happen when the web browser itself is closed.

To make the applet display stuff to the screen, you will probably also want to override the paint() method. Here is a sample paint() method:

public void paint(Graphics g)
{
  g.drawOval(20, 20, 10, 10);
}
You must have
import java.awt.*;
or
import java.awt.Graphics;
at the top of the file, so that the compiler will recognize the Graphics class.

To get the paint() method to be called, you have to say repaint() at that point in your program where you want paint() to be called. This will ensure that paint() will be called with the right parameter.

The variable g is the "graphics context" through which the applet is able to display things to the screen. The documentation for Graphics shows you many other things you can do, like draw lines, rectangles, text, etc.

Running Applets

To run an applet you first compile it to a .class file, the same way you would a regular application. Then, create an html file which displays the applet to a browser. Here is a sample html file that references an applet: <html> <head> <title> A Nested Rectangle Applet </title> </head> <body> Here is the nested rectangle applet: <p> <applet code="NestedApplet.class" width=400 height=200> </applet> </body> </html> This html file can either be viewed with an appletviewer program or with a web browser. For example, for the third homework assignment you are instructed to put your applet in a directory called hw3, which is under your public_html directory on acf5. The .java, .class, and .html files must be world-readable. To do this, type
chmod a+r *
at the unix prompt, when you are working in the hw3 directory. Also, the directories themselves, public_html and hw3, must be world-readable and world-executable. To do this, type
chmod a+xr hw3
when you are working in the public_html directory, and type
chmod a+xr public_html
when you are working in your home directory (this is also described in Professor Yap's announcement .) Once these files and permissions are set up, you can view your applet on the web by going to the hw3 directory and clicking on the html file that references the applet.

If you make a change to the applet, and want to view the new version in the browser, then, after recompiling the applet, hold the "shift" key while clicking the "reload" button. This will instruct the browser to reload the class itself, not just the html page. (This works on Netscape; I don't know what the equivalent is in Internet Explorer.)