Example 3-2 is the source of the applet that is equivalent to the application in Example 3-1.
// // 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; extend 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> |