Applets



General Information

Introduction

Two Java libraries and provide the mechanisms for building graphical user interfaces (GUI) and applets. GUI's allow programs to communicate with users using bitmap displays and gestures with a mouse (as opposed to a textual, command-line approach). Applets are GUI applications that are part of HTML documents and are delivered over the internet using WWW browsers.

The library is small; to build applets requires not only the applet library, but also the library--the abstract window toolkit. The package is large, with over 42 classes. And is even larger.

Applet Overview

An applet is not a typical, self-contained main program under control of the user. Rather it is invoked by a WWW browser and is under its control. The WWW browser allocates a rectangular region of the document to the applet.

Java Class: Applet

Demonstration of Applet, JApplet, and parameters.

Applet running in browswer

Applet running in appletviewer

For purposes of developing and debugging applets it is convenient to have a program that just runs applets--a stripped down version of a browser. The appletviewer is such a program.

Tip: Put an applet tag in the comments of the java source file. Then the source file can be used by appletviewer to test and debug the applet without writing a separate html document.

How does it Work?

Client-side execution

Contrast with CGI.

The "applet" tag in HTML

An applet is embedded in an HTML document much like other elements. The document controls the allocation of a rectangular area for the applet using the applet tag. An example:
<applet code="ConicApplet" width=400 height=400></applet>

Attributes

The attributes width and height are required to contol the size in pixels of the rectangle allocated by the browser to contain the applet.

ALIGN tag controls "flow"

The attribute align controls the way the main body of the text flows around the applet in the same was as the HTML img tag. Some possible values for the attribute are: top, bottom, left, and right.

Applet Tag Details

(See Applet Tag at Sun.) Competing standards Objects, Images, and Applets in the W3C HTML 4.01 standard, MSIE.

<applet CODE="applet.class" WIDTH=200 HEIGTH=200>
text for non-JAVA Browsers 
</applet>

<applet CODE="applet.class" WIDTH=200 HEIGTH=200> 
<PARAM NAME="DBHOME" VALUE="dbdirectory">
<PARAM NAME="LANG" VALUE="US-ENGLISH">
text for non-JAVA Browsers 
< /APPLET>

<applet CODEBASE="http://www.somplace.com/java/classes"
           CODE="applet.class" WIDTH=200 HEIGTH=200> 
'<' 'applet'
    ['ARCHIVE '=' archiveList]
    ['CODEBASE' '=' codebaseURL]
    'CODE' '=' appletFile
    ['ALT' '=' alternateText]
    ['NAME' '=' appletInstanceName]
    'WIDTH' '=' pixels 'HEIGHT' '=' pixels
    ['ALIGN' '=' "LEFT"|"RIGHT"|"TOP"|"ABSMIDDLE"|"ABSBOTTOM"|
                 "TEXTTOP"|"MIDDLE"|"BASELINE"|"BOTTOM"]
    ['VSPACE' '=' pixels] ['HSPACE' '=' pixels]
'>' 
['<' 'PARAM' 'NAME' '=' appletAttribute1 'VALUE' '=' value '>']
['<' 'PARAM' 'NAME' '=' appletAttribute2 'VALUE' '=' value '>']
. . .
[alternateHTML]
'</applet>'
ARCHIVE = archiveList
This optional attribute describes one or more archives containing classes and other resources that will be "preloaded". The classes are loaded using an instance of an AppletClassLoader with the given CODEBASE. The archives in archiveList are separated by ",". NB: in Java Development Kit multiple applet tags with the same CODEBASE share the same instance of a ClassLoader. This is used by some client code to implement inter-applet communication.
CODEBASE = codebaseURL
This optional attribute specifies the base URL of the applet -- the directory that contains the applet's code. If this attribute is not specified, then the document's URL is used.
CODE = appletFile
This required attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute.
ALT = alternateText
This optional attribute specifies any text that should be displayed if the browser understands the APPLET tag but can't run Java applets.
NAME = appletInstanceName
This optional attribute specifies a name for the applet instance, which makes it possible for applets on the same page to find (and communicate with) each other.
WIDTH = pixels & HEIGHT = pixels
These required attributes give the initial width and height (in pixels) of the applet display area, not counting any windows or dialogs that the applet brings up.
ALIGN = alignment
This required attribute specifies the alignment of the applet. The possible values of this attribute are the same as those for the IMG tag: left, right, top, texttop, middle, absmiddle, baseline, bottom, absbottom.
VSPACE = pixels & HSPACE = pixels
These option attributes specify the number of pixels above and below the applet (VSPACE) and on each side of the applet (HSPACE). They're treated the same way as the IMG tag's VSPACE and HSPACE attributes.

Pie Chart Example

You may not be the applet's writer; the applet may not reside on your site even. You can using other applets as long as the parameters of the applet are documented. One example, is scrolling text, another is a pie chart.

Composition of Student Body at Florida Tech
<applet codebase="http://www.cs.fit.edu/~ryan/java/applets/piechart" code="PieChart.class" width=250 height=200>
 <param name=showlabel   value="yes">
 <param name=showpercent value="yes">
 <param name=bgcolor value="white">
 <param name=columns value="2">
 <param name=Plabel1 value="main">
 <param name=Pvalue1 value="2779">
 <param name=Pcolor1 value="red">
 <param name=Plabel2 value="off campus">
 <param name=Pvalue2 value="1399">
 <param name=Pcolor2 value="blue">
</applet>
PieChart.java

Netscape

Applets may write to the standard output or standard error, browswers may collect these output in some window. Additional, the browswer may print diagnostics about the execution of Java applets.
Communicator > Tools > Java Console
To set debug level, type a single digit into Netscape's Java console window.

IE

  1. From the Menu Bar, select Tools.
  2. Select Internet Options.
  3. Select the Advanced tab.
  4. >Make sure you have a check mark next to Java Console Enabled (requires restart), JIT Compiler Enabled, and Java Logging Enabled.

Java Plug-In

Applet lifecycle and methods

An applet is controlled by the browswer; there is no main program.

Usual GUI operation in inherited by from and :

Other methods in class Applet provide the interface to the outside world.

public class Applet extends Panel {
    public boolean isActive()
    public URL getDocumentBase()
    public URL getCodeBase()
    public String getParameter(String name)
    public AppletContext getAppletContext()
    public void showStatus(String msg) {
        getAppletContext().showStatus(msg);
    }
    public Image getImage(URL url) {
        return getAppletContext().getImage(url);
    }
    public AudioClip getAudioClip(URL url) {
        return getAppletContext().getAudioClip(url);
    }
    public void play(URL url)
    public void init() {}
    public void start() {}
    public void stop() {}
    public void destroy() {}
}

These methods are illustrated in the next few sections.

Methods

Audio

Jumping to another URL

Communicating Applets

Applet Security

Denial of service attacks are still possible. Security is in the hands of the broswer, different policies are possible. Java 1.2 plugs-in can run with custom tailored security policies.

Netscape priveledges


Ryan Stansifer <ryan@cs.fit.edu>
Last modified: Tue Feb 24 13:58:53 EST 2004