Do you expect that applets will have a main
method?
No. An applet is an object whose methods are called by the browser.
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:
This applet will be explained in the next several pages.