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!
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.
import java.awt.*;
public class hi extends java.applet.Applet {
public void paint(Graphics gc) {
gc.drawString("Hi, Mom!", 100, 90);
}
}
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
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.
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.