Applets are compiled Java programs which are run through the
Applet
Viewer or through any world wide web browser which
supports Java. Applets
are considered quite "safe" because of
their limited file and network
access, and are nevertheless impressive.
Each applet contains five functions to be called during its existence:
These will often be rewritten but an applet still has a default function for each.
The Java programmer writes the applet source code in files with
the
.java extension. The resulting compiled files have the same
name with the
.class extension.
eg.) Hello.java will be compiled into Hello.class
Here is the Java code for a tiny applet which draws a message:
// Hello.java // by Garry Morse import java.awt.*; import java.applet.*; public class Hello extends Applet { public void paint(Graphics g) { g.drawString("Hello there!",10,10); } }
The import statements are common to all applets which inherit
the
functionality of Java classes. Here, our class Hello is declared
as
a subclass of the Applet class. For C/C++ programmers, this is
rather like
the #include statement for header files.
Since users of the world wide web are viewing pages coded in
HTML, it
seems logical that Java applets are embedded in these
pages through the use
of the <APPLET> tag.
Here is the HTML for a blank web page containing a Java applet:
<HTML> <APPLET CODE="Hello.class" WIDTH=200 HEIGHT=50> </APPLET> </HTML>
There are required tags which follow the applet tag:
CODE - filename of executable code with class extension.
(required)
WIDTH - width of the window to open for the applet.
(required)
HEIGHT - height of the window to open for the applet.
(required)
These are other tags which will appear in later examples:
ALT - alternate image or text for web browsers which are not
Java-capable.
ALIGN - position applet on web page, left, right, or
center.
CODEBASE - location of Java code if in different directory
than HTML page.
HSPACE - amount of horizontal space around an
aligned applet.
NAME - symbolic name for applets on same page to
refer to one another by.
PARAM - repeated for applets designed to
take parameters such as filenames.
VSPACE - amount of vertical
space around an aligned applet.
Click here to see the Hello applet in action!
The Java Game Programming Tutorial and all tutorials within are created by Garry Morse, Copyright 1997