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
- API Documentation for class
- API Documentation for class
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
- From the Menu Bar, select Tools.
- Select Internet Options.
- Select the Advanced tab.
- >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.
-
--
called first and called only once
-
--
called everytime applet is displayed on screen
-
--
called when browser no longer is displaying applet
-
--
called when browser decides that the applet is no longer needed at all
Usual GUI operation in inherited by
from
and
:
-
--
called when applet display is damaged
-
--
called by
Default value in
:
public void update(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, width, height);
g.setColor(getForeground());
paint(g);
}
Don't change background in paint or you will get a flash of color.
-
--
force the applet to repaint the whole display as soon as possible
Other methods in class Applet provide the interface to the
outside world.
-
- getAudioClip(URL url)
- getCodeBase()
- getDocumentBase()
- getImage(URL url)
- getLocale() -- for internationalization
-
-- named parameters in HTML file (already discussed)
- showStatus()
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
- Can't access, read or write files
- Can't create, delete or rename files or directory
- Can't list directory contents
- Can't make a network connection to any other computer
except the one form which the applet itself was loaded
- A warning on top-level window to prevent spoofing
- Can't access most of the system properities
- Can't run Reuntime.exec() methods
- Can't execute System.exit()
- Can't manipulate any thread that is not part of the same
ThreadGroup as its own.
- Can't create its own class loader
- Can't create its own SecurityManager
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