Beruflich Dokumente
Kultur Dokumente
An applet is a program written in the Java programming language that can be included in
an HTML page, much in the same way an image is included in a page. When you use a
Java technology-enabled browser to view a page that contains an applet, the applet's code
is transferred to your system and executed by the browser's Java Virtual Machine (JVM).
For information and examples on how to include an applet in an HTML page, refer to this
description of the <APPLET> tag.
This section tells you most of what you need to know to use the <APPLET> tag. It starts
by showing you the tag's simplest form. It then discusses some of the most common
additions to that simple form: the <PARAM> tag, alternate HTML code and text, the
CODEBASE attribute, and the ARCHIVE attribute.
You should already have seen the simplest form of the <APPLET> tag:
<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
</APPLET>
This tag tells the browser to load the applet whose Applet subclass is named
AppletSubclass, displaying it in an area of the specified width and height.
Specifying Parameters.
Some applets let the user customize the applet's configuration with parameters.
For example, AppletButton (an applet used throughout this tutorial to provide a button
that brings up a window) allows the user to set the button's text by specifying the value of
a parameter named BUTTONTEXT.
The user specifies the value of a parameter using a <PARAM> tag. The <PARAM> tags
should appear just after the <APPLET> tag for the applet they affect:
<APPLET CODE=AppletSubclass.class WIDTH=anInt HEIGHT=anInt>
<PARAM NAME=parameter1Name VALUE=aValue>
<PARAM NAME=parameter2Name VALUE=anotherValue>
</APPLET>
Here's an example of the <PARAM> tag in use.
<APPLET CODE="Animator.class" WIDTH=460 HEIGHT=160>
<PARAM NAME="imageSource" VALUE="images/Beans">
<PARAM NAME="backgroundColor" VALUE="0xc0c0c0">
<PARAM NAME="endImage" VALUE=10>
<PARAM NAME="soundSource" VALUE="audio">
<PARAM NAME="soundtrack" VALUE="spacemusic.au">
<PARAM NAME="sounds"
VALUE="1.au|2.au|3.au|4.au|5.au|6.au|7.au|8au|9.au|0.au">
<PARAM NAME="pause" VALUE=200>
...
</APPLET>
Note the ellipsis points (". . .") in the previous HTML example. What did the example
leave out? It omitted alternate HTML code -- HTML code interpreted only by browsers
that don't understand the <APPLET> tag. Alternate HTML code is any text that appears
between the <APPLET> and </APPLET> tags, after any <PARAM> tags. Java-enabled
browsers ignore alternate HTML code.
To specify alternate text to Java-enabled browsers and other browsers that understand the
<APPLET> tag, use the ALT attribute. If the browser can't display an applet for some
reason, it can display the applet's ALT text.
We use alternate HTML code throughout the online version of this tutorial to tell readers
about the applets they're missing. Often, the alternate HTML code includes one or more
pictures of the applet. Here's the complete HTML code for the Animator example shown
previously:
<APPLET CODE="Animator.class" WIDTH=460 HEIGHT=160
ALT="If you could run this applet, you'd see some animation">
<PARAM NAME="imageSource" VALUE="images/Beans">
<PARAM NAME="backgroundColor" VALUE="0xc0c0c0">
<PARAM NAME="endImage" VALUE=10>
<PARAM NAME="soundSource" VALUE="audio">
<PARAM NAME="soundtrack" VALUE="spacemusic.au">
<PARAM NAME="sounds"
VALUE="1.au|2.au|3.au|4.au|5.au|6.au|7.au|8au|9.au|0.au">
<PARAM NAME="pause" VALUE=200>
Your browser is completely ignoring the <APPLET> tag!
</APPLET>
An applet that doesn't understand the <APPLET> tag ignores everything in the previous
HTML code except the line that starts with "Your". A browser that does understand the
<APPLET> tag ignores everything on that line. If the applet-savvy browser can't run the
applet, it might display the ALT text.
By default, a browser looks for an applet's class and archive files in the same directory as
the HTML file that has the <APPLET> tag. (If the applet's class is in a package, then the
browser uses the package name to construct a directory path underneath the HTML file's
directory.) Sometimes, however, it's useful to put the applet's files somewhere else. You
can use the CODEBASE attribute to tell the browser in which directory the applet's files
are located:
<APPLET CODE=AppletSubclass.class CODEBASE=aURL
WIDTH=anInt HEIGHT=anInt>
</APPLET>
If aURL is a relative URL, then it's interpreted relative to the HTML document's location.
By making aURL an absolute URL, you can load an applet from just about anywhere --
even from another HTTP server.
This tutorial uses CODEBASE="someDirectory/" frequently, since we group the
examples for each lesson in subdirectories. For example, here's the <APPLET> tag that
includes the Simple applet in LIFE CYCLE of An APPLET
<APPLET CODE=Simple.class CODEBASE="example/"
WIDTH=500 HEIGHT=20>
</APPLET>
The following figure shows the location of the class file, relative to the HTML file, when
CODEBASE is set to "example/".
The next figure shows where the applet class can be if you specify an absolute URL for
the value of CODEBASE.
If your applet has more than one file, you should consider providing an archive file that
bundles the applet's files into a single file. Whether archive files make sense for your
applet depends on several factors, including your applet's size, performance
considerations, and the environment you expect your users to have.
Archive files reduce your applet's total download time. Much of the time saved comes
from reducing the number of HTTP connections that the browser must make. Each HTTP
connection can take several seconds to start. This means that for a multifile applet,
connection time can dwarf transfer time. You can further reduce transfer time by
compressing the files in your archive file.
If you specify one or more archive files, then the browser looks for the archive files in the
same directory that it would search for the applet class file. The browser then looks for
the applet's class files in the archive files. If a file isn't in the archive, then the browser
generally tries to load it just as it would if the archive file weren't present.
The standard Java archive format, called JAR, was introduced in JDK 1.1 and is based on
the ZIP file format. You specify JAR files using the ARCHIVE attribute of the
<APPLET> tag. You can specify multiple archive files by separating them with commas:
<APPLET CODE="AppletSubclass.class" ARCHIVE="file1, file2"
WIDTH=anInt HEIGHT=anInt>
</APPLET>
Unfortunately, not all browsers understand the same archive format or use the same
HTML code to specify the applet archive. Watch this page for the latest information about
browser support for archives.
This section didn't discuss every attribute of the <APPLET> tag. Other attributes -- which
might seem familiar, since the <IMG> HTML tag uses them -- include ALIGN,
VSPACE, and HSPACE. The <APPLET> tag also allows you to load a serialized (saved)
applet by specifying the OBJECT attribute instead of specifying a class file with CODE.
Finally, you can name an applet using the NAME attribute.
Below is the source code for the Simple. The Simple applet displays a descriptive string
whenever it encounters a major milestone in its life, such as when the user first visits the
page the applet's on. The pages that follow use the Simple applet and build upon it to
illustrate concepts that are common to many applets.
* Java SE 5 Version
*/
import java.applet.Applet;
import java.awt.Graphics;
StringBuffer buffer;
The Simple applet, like every other applet, features a subclass of the Applet class. The
Simple class overrides four Applet methods so that it can respond to major events:
init
To initialize the applet each time it's loaded (or reloaded).
start
To start the applet's execution, such as when the applet's loaded or when the user revisits
a page that contains the applet.
stop
To stop the applet's execution, such as when the user leaves the applet's page or quits the
browser.
destroy
To perform a final cleanup in preparation for unloading.
Not every applet needs to override every one of these methods. Some very simple applets
override none of them. For example, the HelloWorld applet at the beginning of this
section doesn't override any of these methods, since it doesn't do anything except draw
itself.
The "Hello World" applet just displays a string once, using its paint method. (The paint
method is described on the following page.) Most applets, however, do more.
The applet API lets you take advantage of the close relationship that applets have with
Web browsers. The API is provided by the javax.swing.JApplet class and the
java.applet.AppletContext interface.
Deploying Applets
This section explains to HTML authors how and when to use the applet, object, and
embed tags to add Java applets to Web pages, and provides guidelines for deploying
applets on the Internet and Intranets, and for use with different browsers.
• You use the object tag to deploy applets that are to be used only with Internet
Explorer.
• You use the embed tag to deploy applets that are to be used only with the Mozilla
family of browsers.
Question: How do you cause the applet GUI in the browser to be refreshed when data in
it may have changed?
Answer: You invoke the repaint() method. This method causes the paint method, which
is required in any applet, to be invoked again, updating the display.
Question: True or false: An applet can make network connections to any host on the
Internet.
Answer: False: An applet can only connect to the host that it came from.
Question: How do you get the value of a parameter specified in the APPLET tag from
within the applet's code?
Answer: You use the getParameter("Parameter name") method, which returns the String
value of the parameter.
Question: True of false: An applet can run multiple threads.
Answer: True. The paint and update methods are always called from the AWT drawing
and event handling thread. You can have your applet create additional threads, which is
recommended for performing time-consuming tasks.
Question: Match the following tag names with the descriptions in the following lists:
EMBED tag
APPLET tag
OBJECT tag
Use to deploy applets to a multi-browser environment.
Use to deploy applets that are to be used only with the Mozilla family of browsers
Use to deploy applets that are to be used only with Internet Explorer
Answer:
EMBED tag: b
APPLET tag: a
OBJECT tag: c
Exercise: For an applet using the Exercise class, write the Applet tag that sets the
ButtonName parameter to Save.
Answer:
<APPLET CODE=AppletButton.class>
<PARAM NAME=ButtonName VALUE=Save>
</APPLET>
Exercise: Write the method to display an applet in the browser, so that the contents are
contained in a rectangle around the phrase "Exercise Applet". Have the applet be one
pixal less than the size specified ont he web page, and the phrase starting at the
coordinates 5, 15.
Answer:
public void paint(Graphics g) {
//Draw a Rectangle around the applet's display area.
g.drawRect(0, 0,
getWidth() - 1,
getHeight() - 1);
You do not need to know Java to install applets on your pages. There are thousands of
free applets available on the internet for almost any purpose. Most of them can be
customized without programming.
Most of today's browsers can run applets. The ones that can't, aren't of much importance,
since very few users have such outdated browsers. To be more precise, applets can be
embedded in pages viewed by Netscape 2+ and Internet Explorer 3+.
However, some people have turned off the ability to run applets in their browser. This is
in most cases companies with more or less paranoid ideas of potential hacking.
No matter what their motivation is, it's a fact that there are a minor amount of people out
there, that will not see your applets, even if their browser is capable of showing it. This
should be taken into consideration before deciding to add applets to your pages.
An applet can be embedded into a webpage. Usually the applet has several settings that
will allow you to personalize it. For instance, if you insert an applet that will work as a
menu, you can specify which options should be in the menu, and which pages should be
loaded upon click on an option.
Since Java is a real programming language there aren't many limitations to it. Any
program running on your computer could possibly have been made as an applet.
Spreadsheets, wordprocessors, graphics programs... even entire browsers could be made
with Java. However, most applets used on webpages serve much smaller purposes than
the ones mentioned. The reason is simple: They need to be transferred through the
internet, and therefore can't take up just any amount of space.