Java Reference
In-Depth Information
with the remote application server using sockets (see lower part of Figure
15.11).
Sidebar 15.1 Java Applet
An applet is implemented by specializing class
java.applet.Applet
or
javax.swing.
JApplet
. These classes offer four basic methods, namely
init()
,
start()
,
paint()
and
destroy()
. The applet lifecycle is depicted in the figure below. These methods are
overridden in every concrete subclass.
Applet classes do not have a main method, since they are created by the
browser and do not stand alone. They also do not have a constructor, which is
replaced by method
init()
.
The browser
loads the page
start
init
Displays the page
Reloads the page
paint
Leaves the page
stop
destoy
Quits
The code below exemplifies the implementation of a simple applet.
package
example;
import
java.applet.*
public class
Simple
extends
Applet {
public void
init()
{System.out.println("initializing..."); }
public void
start()
{System.out.println("starting... "); }
public void
paint(Graphics g)
{System.out.println("painting..."); }
public void
stop()
{System.out.println("stopping... "); }
public void
destroy()
{System.out.println("unloading..."); }
}
Once the code has been compiled, the applet is embedded in a web page as
shown in the following HTML code.
<html><body>
<APPLET CODE
#
"example.Simple.class" CODEBASE
#
"." WIDTH
#
"700"
HEIGHT
#
"600">
<PARAM NAME
#
MAPLIST VALUE
#
"value" >
</APPLET>
</body></html>