Applet_00
"Applet_00" shows the main building blocks of a "normal" Java applet. Its
source code provides a template that may be used as a basis for other (more
sophisticated) applets.
Please, also consider my "Hints for Reading"
and the "List of Recent Changes"!
|
When scrolling up and down within your web browser (especially on a busy
machine) it sometimes happens that parts of an applet window won't be updated
- from time to time the whole applet might even "disappear".
In these cases try to "refresh" your display or scroll page-wise(!) until
the applet receives a correct update event and repaints itself. |
Basic Applet Methods
The source code for this little applet contains
templates for all "basic" applet methods:
-
public void init()
is called by a web browser whenever the applet is loaded or reloaded. This
method should be used to set up and initialize data structures and (static)
user interface components.
-
public void start()
is called by a web browser whenever the underlying HTML document of an applet
has been entered (i.e., when it has become visible) - the applet itself might
still be outside the visible region of that page. This method should be used
to actually start applet processing.
-
public void stop()
is called by a web browser whenever the underlying HTML document of an applet
has been left (i.e., when it has become invisible). This method should be
used to stop actual applet processing (in order to reduce the system load).
The applet remains "loaded", however, and might be started again when the
user decides to re-enter its web page.
-
public void destroy()
is called by a web browser whenever the underlying HTML document of an applet
is going to be removed from the stack of active web pages and the applet
itself is going to be unloaded. This method can be used to clean up system
resources or to execute any other finalization code.
-
public String getAppletInfo()
AppletViewers often offer a menu function that provides some information
about an applet's author, the current version number and copyright notices.
This description is taken from the outcome of getAppletInfo. An
applet should implement this method and return a single String
containing this information in an arbitrary format - linefeeds may be used
to break up the text into separate lines. Example:
public String getAppletInfo() {
return "Applet_00 - a simple applet template (Rev. 1.0)\n" +
"Andreas Rozek\n" +
"Feel free to use the source code for your own developments";
};
The "default" implementation returns null.
-
public String[][] getParameterInfo()
AppletViewers often offer a menu function that provides some information
about name and format of any foreseen applet parameter. This description
is taken from the outcome of getParameterInfo. An applet should
implement this method and return a two-dimensional array of type
String containing this information. Example:
final static String[][] ParameterInfo = {
{"Background", "URL", "GIF file with background texture"},
{"BackgroundX", "integer", "initial horizontal texture displacement"},
{"BackgroundY", "integer", "initial vertical texture displacement"},
};
public String[][] getParameterInfo() {
return ParameterInfo;
};
The "default" implementation returns null.
Additional Code
The current implementation of paint(...) draws a border around
the applet that produces a slight "3D" effect - an insets() method
protects this border from being occupied by other window components.
The window components of Applet_00 are arranged using a
GridBagLayout layout manager. As it's quite tedious to specify
the numerous GridBagConstraints for a given component, the whole
task of adding a new component and assigning proper constraints has been
stuffed into an add_GridItem method - a single call of this method
now achieves the same result as a whole set of assignments and procedure
calls. Although this approach introduces a small additional overhead, it
greatly reduces the amount of code that has to be written and increases its
clarity - as long as there is only a small number of components to be added
this way, the resulting performance loss will be neglectible.
Unfortunately, the Java AWT doesn't foresee fixed graphical elements in an
user interface (besides text labels, of course). The thin separator below
the applet's title therefore requires the introduction of an additional class
(ColorBox) that has been derived from java.awt.Canvas.
Finally, an instance of class Arena provides the illusion of some
applet "contents", although this extension of class Canvas is
as static and fixed as the abovementioned ColorBox.
Embedding an Applet in an HTML Document
After compilation, an applet may be placed on a web page by means of the
<applet> directive. A Java-capable (and -enabled) browser
will then load and execute the applet while any other will display alternate
text or alternate HTML code instead.
As there are still enough people disabling Java in their browsers (mainly
for security reasons) it is always a good idea to provide alternatives such
as a screen snapshot of the applet or any other placeholder as shown in the
following example:
<applet code=Applet_00.class width=300 height=200 alt="Applet_00">
<img src="Applet_00.gif" alt="Applet_00 snapshot">
</applet>
Source Code
The source code of this applet is available for download:
|