Aqua Phoenix
     >>  Lectures >>  Java 4  


4.1 Overview

Java applets are applications ( applet) that have limited functionality. "Limited" here refers to resources that are restricted to mouse, keyboard, high-level graphics, and network. It is not possible to interact with File I/O, and other OS resources. Applets are thus considered to run in "sandboxes", which means that they run in an isolated environment.

Applets make use of a certain set of packages that include functionality for GUI objects and event handlers. These packages are:

  • java.applet: Applet skeleton
  • java.awt: Layout Managers, GUI objects, Color, etc.
  • java.awt.event: Event handlers

These libraries are included in an applet source file (in no particular order).

Using these packages, applets can be built for many kinds of GUI applications found in GUI-style environments, e.g. Forms, menu-driven applications, image manipulation applications, animated graphics, etc.

Java applets are built similar to Java applications (command-line or GUI-style): There exists a main Java source file that is "invoked", and which builds the applet and all of its dependent components.

Java applets are compiled in the same manner as other Java applications (command-line or GUI style). However, unlike Java applications, Java applets are not "run" using the java command. In order to simulate the sandbox environment, Java applets use appletviewer , which simulates a web browser. For this purpose a Java applet must have a corresponding HTML page, which includes the APPLET tag. This is exactly the same APPLET tag that would appear on a web page containing an applet.

Assuming an applet with a main class named MyFirstApplet.class, a representative APPLET HTML tag would be:

<APPLET CODE="MyFirstApplet" WIDTH=600 HEIGHT=400>
Attributes WIDTH and HEIGHT are used to dimension the applet in terms of pixels.

It is also possible to specify the URL for the web location of the applet. The location can also be an absolute path to a directory on a file system, for example, when the HTML file is loaded from a directory that does not contain the Java class files:

<APPLET ARCHIVE="web.jar" CODEBASE="" CODE="MyFirstApplet" WIDTH=600 HEIGHT=400>

<APPLET ARCHIVE="web.jar" CODEBASE="c:\applets" CODE="MyFirstApplet" WIDTH=600 HEIGHT=400>
Finally, if the applet classes are part of a Java ARchive (JAR), then the archive filename must be specified separately:

<APPLET ARCHIVE="applets.jar" CODEBASE="" CODE="MyFirstApplet" WIDTH=600 HEIGHT=400>