Note:
To run an applet written with JavaTM 2 APIs in a browser, the browser must
be enabled for the Java 2 Platform. If your browser is not enabled
for the Java 2 Platform, you have to use appletviewer to run the applet
or install Java
Plug-in. Java Plug-in lets you run applets on web pages
under the 1.2 version of the Java VM instead
of the web browser's default Java VM.
Applet Structure and Elements
The Java API Applet
class provides what you need to
design the appearance and manage the behavior of an applet.
This class provides a graphical user interface (GUI) component
called a Panel
and a number of methods.
To create an applet, you extend (or subclass) the Applet
class and implement the appearance and behavior you want.
The applet's appearance is created by drawing onto the Panel
or by attaching other GUI components such as push buttons, scrollbars, or
text areas to the Panel
. The applet's
behavior is defined by implementing the methods.
Extending a Class
Most classes of any complexity extend other classes. To extend another
class means to write a new class that can use the fields and methods
defined in the class being extended. The class being extended is the
parent class, and the class doing the extending is the child class.
Another way to say this is the child class inherits the fields and
methods of its parent or chain of parents. Child classes either call or
override inherited methods. This is called single inheritance.
The SimpleApplet
class extends Applet
class,
which extends the Panel
class, which extends the
Container
class. The Container
class extends
Object
, which is the parent of all Java API classes.
The Applet
class provides the init
,
start
, stop
, destroy
,
and paint
methods you saw in the example applet.
The SimpleApplet
class overrides these methods
to do what the SimpleApplet
class needs them to do.
The Applet
class provides no functionality for
these methods.
However, the Applet
class does provide functionality
for the setBackground
method,which is called in the
init
method. The call to setBackground
is an
example of calling a method inherited from a parent class in contrast
to overriding a method inherited from a parent class.
You might wonder why the Java language provides methods without
implementations. It is to provide conventions for everyone
to use for consistency across Java APIs. If everyone
wrote their own method to start an applet, for example, but
gave it a different name such as begin
or
go
, the applet code would not be interoperable with
other programs and browsers, or portable across
multiple platforms. For example, Netscape and Internet Explorer know how
to look for the init
and start
methods.
Behavior
An applet is controlled by the software that runs it.
Usually, the underlying software is a browser, but it can also
be appletviewer
as you saw in the example. The
underlying software controls the applet by calling the
methods the applet inherits from the Applet
class.
The init Method:
The init
method is called when the applet is first
created and loaded by the underlying software. This method performs
one-time operations the applet needs for its operation such
as creating the user interface or setting the font.
In the example, the init
method initializes the text
string and sets the background color.
The start Method:
The start
method is called when the applet is visited such as
when the end user goes to a web page with an applet on it.
The example prints a string to the console to tell you the
applet is starting. In a more complex applet, the start
method would do things required at the start of the applet
such as begin animation or play sounds.
After the start
method executes, the event thread
calls the paint
method to draw to the applet's
Panel
. A thread is a single sequential flow
of control within the applet, and every applet can run in
multiple threads. Applet
drawing methods
are always called from a dedicated drawing and event-handling
thread.
The stop and destroy Methods:
The stop
method stops the applet when
the applet is no longer on the screen such as when the end user
goes to another web page. The example prints a string to
the console to tell you the applet is stopping. In a more
complex applet, this method should do things like
stop animation or sounds.
The destroy
method is called when the browser exits.
Your applet should implement this method to do final cleanup
such as stop live threads.
Appearance
The Panel
provided in the Applet
class
inherits a paint
method from its parent
Container
class. To draw something onto the Applet's
Panel
, you implement the paint
method to do
the drawing.
The Graphics
object passed to the paint
method defines a graphics context for drawing on the
Panel
. The Graphics
object has methods
for graphical operations such as setting drawing colors, and drawing
graphics, images, and text.
The paint
method for the SimpleApplet
draws the I'm a simple applet string in red inside a blue
rectangle.