Java Reference
In-Depth Information
If this HTML page is saved with the name
Greeting.html
, then the contained
applet may be executed by loading the HTML page into the appletviewer with the
following command:
appletviewer Greeting.html
N.B. This example assumes that both
Greeting.html
and
AppletGreeting.class
are in the current folder. If
AppletGreeting.class
is in a sub-folder, then the
CODE
attribute must specify the relative path. For example:
CODE = "folder1\folder2\AppletGreeting.class"
For a directory elsewhere, attribute
CODEBASE
must be used to specify that
directory. For example:
CODEBASE = "..\otherfolder"
(Attribute
CODE
must still also be used to specify the applet's
.class
fi le, of course.)
The output from this applet under the appletviewer is shown in Fig.
12.1
below.
Fig. 12.1
Output from
AppletGreeting
under the
appletviewer
In order to run a Swing applet in a browser, we must have the
Java Plug-in
installed and the browser must know that it is to use this plug-in (rather than the
JVM). Both Chrome and IE9 will automatically use the Java Plug-in when accessing
Swing applets. The output from each of these when referencing
Greeting.html
is
shown in Figs.
12.2
and
12.3
. (Note the grey background, as for the appletviewer,
now serving the purpose of distinguishing the applet from the HTML page.)
12.3
The Internal Operation of Applets
As its name implies, method
init
carries out any initialisation required by the applet, such
as initialisation of variables or loading of images. Since applets cannot call the
paint
method directly, it is necessary to circumvent direct painting via the following steps:
create a subclass of
JPanel
(within the applet body) and place the painting code
inside method
paintComponent
of this class;
Search WWH ::
Custom Search