Do you expect that applets will have a main method?

A good answer might be:

No. An applet is an object whose methods are called by the browser.

An Example Applet

When you define an applet, you are defining an object that will be used by another program, the web browser. The web browser is the application program and (at least conceptually) has the main() method. The applet object web provides services (methods) to the browser when the browser asks for them.

An applet object has many variables and methods. Most of this comes along automatically when you use the definition of Applet from the JDK. You get this definition when you import java.applet.Applet. Almost always, you will also import java.awt.*. This contains many classes that are useful for windows and graphics. Here is the code for a small applet. The name of the class is AEHousman.

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

public class AEHousman extends Applet
{
  public void paint ( Graphics gr )
  { 
    setBackground( Color.pink );
    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);
   }
}

This applet must be compiled into bytecode (just like an application.) Then 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 will be explained in the next several pages.

QUESTION 2:

Look at the paint() method in the above. What is the class of the parameter, gr?

public void paint ( Graphics gr )