APPLETS



Now that you should be somewhat familiar with the Java language let's explore one of the new and interesting features of Java: APPLETS. An applet is a Java program that runs under a Java-compatible browser such as Netscape or HotJava. This feature allows users to display graphics and to run programs over the Internet via the WWW relatively easily. An applet allows web documents to be both animated and interactive!

A First Applet

In this section of the tutorial, we are going to create a basic applet for a web browser. First of all, a class must be defined that inherits from the class java.applet.Applet. The Applet class is the standard class inherited for writing applets. It contains the methods to paint to the screen and create the window. The inherited class must be declared public. In this example, we will create an applet that says "Hi, Mom!".

Before you start, take a look at what you will be building by executing a finished applet. To return to this section after viewing the applet, use the Back option on your browser. You can see the "Hi Mom!" applet execute by clicking here on hi.applet.

The Applet ProgramRAM

To create the "Hi, Mom" applet, create (e.g., with a text editor) a file called hi.java consisting of the following code:
import java.awt.*;

public class hi extends java.applet.Applet {
	public void paint(Graphics gc) {
			gc.drawString("Hi, Mom!", 100, 90);
	}
}

(Remember that the name of your code file must be the name of your public class followed by .java. This feature is why the above program must be saved in a file called hi.java.)

Compiling the Program

Once you have saved your program, you need to compile it using the Java compiler. At your command line, enter the command "javac hi.java". This command will compile your code so that you now have a hi.class file. If you receive any error messages, look back at the above code and make the necessary corrections.

Viewing and Interacting with an Applet

To display and interact with an applet, an html document that uses the applet must be created. The applet can then be displayed and interacted with in one of two ways:
  1. Using the Java appletviewer program.
  2. Using a Java-compatible html viewer (web browser) to view the document.

In general, it is best to develop an applet using the applet viewer, because some web browsers do not reload an applet even when directed to reload the html document. In order to obtain a revised applet when using such a browser, you must exit the browser (completely) and restart it after the applet class has been recompiled.

The applet viewer is invoked from the command line by the command

appletviewer htmlfile

where htmlfile is the name of the file that contains the html document. The applet class must have previously been compiled by the Java compiler, so that there is a file classname.class in the directory containing the html file.

The .class file is the file that you will use in an html page to execute the applet. For a discussion on how to create the html file and execute the applet, click on applet html.

Explanation of the Code

The paint() method defined in class hi overrides the paint() method that is inherited from the Applet class. The paint() method is called by Java when it's time for the applet to draw itself on the screen. The argument is a graphics object (sometimes called a graphics context) that is defined by the Graphics class that is imported from jave.awt.Graphics.