Do you expect that applets have a main method?

A good answer might be:

No. The methods of an applet are called by another program, not by its own main method.

An Example Applet

An applet is an object that is used by another program, typically a Web browser. The Web browser is the application program and (at least conceptually) holds the main() method. The applet part of a Web page provides services (methods) to the browser when the browser asks for them.

An applet object has many instance variables and methods. Most of this is in the definition of Applet. To access these definitions, your program should import java.applet.Applet and java.awt.*. Here is the code for a small applet. The name of the class is AEHousman and the name of the source file is

import java.applet.Applet;
import java.awt.*;

public class AEHousman extends Applet
  public void paint ( Graphics gr )
    setBackground( );
    gr.drawString("Loveliest of trees, the cherry now", 25, 30);
    gr.drawString("Is hung with bloom along the bough,", 25, 50);
    gr.drawString("And stands about the woodland ride", 25, 70 );
    gr.drawString("Wearing white for Eastertide." ,25, 90);
    gr.drawString("--- A. E. Housman" ,50, 130);

Compile the applet just like an application using javac This produces a bytecode file called AEHousman.class. Now it can be used in a Web page. Here is what this applet does:

Not all browsers can run applets. If you see this, yours can not. You should be able to continue reading these lessons, however.

This applet is explained in the next several pages.


Look at the code for the class AEHousman. How many methods does it define?