System Interface Guide

An Applet

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


Example 2-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.

The applet is compiled by

$ 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>