Java on Solaris 7 Developer's Guide

An Applet

Example 3-2 is the source of the applet which is equivalent to the application in Example 3-1.


Example 3-2 A Java Applet


//
// HelloWorld Applet
//
import java.awt.Graphics;
import java.applet.Applet;
 
public class HelloWorld extends Applet {
    public void paint (Graphics g) {
        g.drawstring ("Hello World", 25, 25);
    }
}

In an applet, all referenced classes must be explicitly imported. The keywords public and void mean the same as in the application; extends says that the class HelloWorld inherits from the class Applet.

To compile the applet, enter


$ javac HelloWorld.java

The applet is invoked in a browser by HTML code. A minimum HTML page to run the applet is:


<title>Test</title>
<hr>
<applet code="HelloWorld.class" width=100 height=50>
</applet>
<hr>