08 April 2002
This lecture covers the reading in Day 7: Writing Java Applets |
What is an applet?
http://java.sun.com/j2se/1.3/docs/api/java/applet/Applet.html
An applet is a small program that is
intended not to be run on its own, but rather to be embedded inside another
application.
The Applet
class must be the superclass of any applet that is to be
embedded in a Web page or viewed by the Java Applet Viewer. The Applet
class provides a standard interface between applets and their environment.
Back in 1995-96, web applets were all the rage, and did much to make Java famous.
Applet | Application |
Browser that supports a Java version loads a web page that includes the applet | Java interpreter loads the main() class file, typically from a command-line prompt |
Java 2 Software Development Kit includes the appletviewer tool for testing applets. | |
The web browser provides part of the user interface, graphics, and event structure, so you get some functionality for free. | |
"sandbox" security restrictions apply | your application has the full power of Java |
takes parameters through an HTML file | takes command-line arguments |
Can a single Java program include both an application and an applet? Why or why not?
no access to the client's file system, clipboard, print queue, or executables
no access to or from web sites except the site that provided the html page with the current applet
no ability to create a window that "spoofs" the appearance of another application
The web browser enforces this restrictions with its SecurityManager object:
http://java.sun.com/j2se/1.3/docs/api/java/lang/SecurityManager.html
Java 1.1 added the concept of a trusted (or signed) applet.
A trusted applet is an applet that the user has taken action to put in the
category of "trusted" applet. A trusted applet can do things outside
of the security sandbox.
http://java.sun.com/docs/books/tutorial/jar/sign/intro.html
[explains signing, verification, and certificates]
http://java.sun.com/j2se/1.3/docs/api/java/security/package-frame.html
[details of the java.security package]
Swing | AWT |
A set of components in Java 2 that tend to work the same on all platforms. The default "look and feel" is called "metal". | "advanced windowing toolkit" of Java 1.0 and Java 1.1; does NOT work the same on all platforms |
Note: The browser needs the Java Plug-in to support the additional functionality of Swing components. | Note: The browser has the virtual machine for running the applet |
Whereas an Java 1.1 applet extends the java.applet.Applet class, a Java 2 Swing applet extends a subclass of java.applet.Applet: javax.swing.JApplet.
http://java.sun.com/j2se/1.3/docs/api/javax/swing/JApplet.html
The JApplet class is an extended version of java.applet.Applet that adds
support for the JFC/Swing component architecture.
Sun's Tutorial on the Swing extension (javax):
http://java.sun.com/docs/books/tutorial/uiswing/index.html
includes this visual presentation of
the Swing components:
http://java.sun.com/docs/books/tutorial/uiswing/components/components.html
Sun's Tutorial on AWT:
http://java.sun.com/docs/books/tutorial/information/download.html#OLDui
Details of the Swing package:
http://java.sun.com/j2se/1.3/docs/api/javax/swing/package-frame.html
Details of the AWT package:
http://java.sun.com/j2se/1.3/docs/api/java/awt/package-frame.html
The applets in the Day 7 chapter of Teach Yourself Java are Swing applets. That is, they extend javax.swing.applet. However, these applets show how to make an <applet> tag, which won't work in most browsers because the browser's virtual machine will not have the javax.swing.applet class.
The applet viewer that the Java 2 SDK provides does have the javax.swing.applet class and does allow you do to testing for Java 2 applets. However, if you are creating Swing applets for real world users, your users will need a browser with the Java plug-in.
What browser runs what kind of applet?
Java 1.0 applets | Java 1.1 applets | Java 2.0 applets |
all web browsers that can run applets | most web browsers. including Internet Explorer 4.x | browsers with the Java Plug-in http://java.sun.com/products/plugin/ |
Web programmers whose audience is all web users make sure that their applets are for Java 1.0 or Java 1.1. | The audience for applets that require that the browser have the Java 2 Plug-in is mostly inside a controlled environment, such as a corporate intranet. |
http://java.sun.com/docs/books/tutorial/applet/appletsonly/html.html
<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
</APPLET>
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.html#plugin
If your applet's users don't have a Swing-enabled browser, then they'll need
to use Java Plug-in to run the applet. Java Plug-in requires that an applet be
included in an HTML page with an
<OBJECT> tag or <EMBED> tag. You can download a free tool
that automatically generates the necessary tags from an <APPLET> tag.
Look at this simple applet posted on this web page:
http://java.sun.com/docs/books/tutorial/uiswing/start/HelloSwingApplet.html
If you right-click to view source, you will discover that the HTML is longer than the source code for the applet.
This tool edits a web page so that its applets run with the Java Plug-in.
http://java.sun.com/products/plugin/1.3/docs/htmlconv.html
A basic template for an applet:
import java.awt.* // for 1.1 applets import javax.swing.* // for Swing applets public class myApplet extends javax.swing.JApplet { // Applet code // public void init() { // setBackground(Color.black); // you might set the background color here // or use an HTML page to pass a parameter // public void start() { // restarts the applet if the browser returns to page } // public void stop() { // you can stop threads the applet started } } }
A screenshot of an applet that keeps time.
Code with color:
Line 27 calls the paint() method because this applet does not have any Swing components.
Line 30 takes advantage of calendar functionality imported from the java.util package.
Line 31 converts
the the time of day into a string.
http://java.sun.com/j2se/1.3/docs/api/java/util/Calendar.html#getTime()
public final Date getTime()
Line 36 calls the Graphics2D method drawString(), which takes three arguments, a string to render, and the x- and y-coordinates on which to render.
Line 38 specifies
the updating of the day every 1000 milliseconds (every second).
Line 43 repaints the screen so that the new time appears.
Note: The book on page 171 says that the paint()
method is called automatically whenever the applet window must be redrawn.
The book might be referring to a Java 1.1 method of the Canvas class:
http://java.sun.com/products/jdk/1.1/docs/api/java.awt.Canvas.html#paint(java.awt.Graphics)
but this would be for AWT rather than a Swing.
In your Swing applets, you call repaint() if
you want to specify it is time to refresh the graphic.
Swing components already have a paint() implementation.
Sun's Swing Tutorial explains the concept of painting: http://java.sun.com/docs/books/tutorial/uiswing/overview/draw.html
To make your applet available to a web browser, you need:
Example HTML code for a Java 1.1 applet:
The same example as output of the HTML converter for the Java Plug-in, which creates and populates the <OBJECT> tag.
The <OBJECT> tag informs Netscape Navigator 4.0 and higher and Microsoft Internet Explorer 4.0 and higher to look for an object reference in the HTML. The reference can be to an applet class file, or to some other object a web browser might support, such as an Active X control.
Note: The <APPLET> tag has a CODE attribute that indicates the full name of the applet.class file. If you applet class file is on another machine, use the CODEBASE attribute to give the complete URL.
Network latency to establish a connection from the web client to the web server is a significant portion of the "world wide wait". A Java archive (JAR) allows the client to perform a single download operation for an active that might include many files.
http://java.sun.com/products/jdk/1.1/docs/guide/jar/
JAR (Java Archive) is a platform-independent file format that aggregates
many files into one. Multiple Java applets and their requisite components
(.class files, images and sounds) can be bundled in a JAR file and subsequently
downloaded to a browser in a single HTTP transaction, greatly improving the
download speed. The JAR format also supports compression, which reduces the file
size, further improving the download time.
If you use a JAR, the applet tag in your HTML file must specify a value for the archive attribute so that the browser's VM can find the archive.
<applet code="MyClass.class" archive="MyClass.jar" ...
The HTML for our example applet (line 8) does pass a parameter for the background color.
The applet uses this parameter (line 15), which it parses with the getParameter() method in the applet's init() method.
You could also pass parameters for other values.
______________
course
homepage course
calendar