|
04 Applets
A Java applet is a Java program which adheres to specific conventions that
allow it to run within a Java
-
enabled browser. Java applets can also be
viewed in a free program called appletviewer.
A web page can download an applet from a server, then run the applet
within a window as defined by the calling HTML statements
:
------
Sample applet
import
java.awt.Graphics;
public
class
MyApplet
extends
Applet
{
public
void
paint (Graphics g)
{
g.drawString(
"Have a nice day."
, 50,25);
}
}
-----
Sample calling HTML file
<
html
>
<
head
>
<
title
>
Applet Template
</
title
>
</
head
>
<
body
>
<
hr
>
<
applet code
=
"MyApplet.class"
Width
=
200 Height
=
200
>
<
param Name
=
message Value
=
"string"
>
</
applet
>
<
hr
>
</
body
>
</
html
>
---------
Applet tags (
for
use in HTML file)
<
applet
>
...
</
applet
>
Code
-
Name of the applet (does not include path),
case
-
sensitive
CodeBase
-
Options
-
relative path to the
class
(could be URL)
Width
-
Pixel width of frame that holds the applet
Height
-
Pixel height of frame that holds the applet
Align
-
HSpace
-
border space around the applet (pixels)
VSpace
-
border space around the applet (pixels)
You can pass parameters, each within its own tag
:
-
name is
case
-
sensitive
:
-
all values are sent as strings
-
go between the
<
applet
>
and
</
applet
>
tags
<
param Name
=
"parameter_name"
Value
=
"parameter_value"
>
For non
-
Java
-
compatible browsers, you can include the
<
img
>
tag (just before the
</
applet
>
tag is preferred).
If the browser cannot read the
<
applet
>
it will display
the image. Make sure the image is same size
/
location as
the applet.
-------
Freeware applet viewer
Applets can be viewed in a freeware application called appletviewer
:
-
reads an HTML file and runs only the applet
-
if
the page has multiple applets, all are run in own instance of appletviewer
appletviewer sample.html
|