Java Applets
Intro
• Java applets are special kind of programs (given by Java) which can be
embedded into HTML codes.
• . HTML codes are static codes to bring some dynamicity into these
codes applets can be used.
• Applets are java codes thus translated to class file when compiled
• A big condition for running applet code on client machine is
availability of JVM (Java Virtual Machine).
• If JVM not installed on client side an empty box will appear instead of
actual applet. Width & Height parameters mention the window size
where your applet will appear in your HTML page.
intro
• An applet is a Java program that runs in a • A JVM is required to view an
Web browser.
applet. The JVM can be either a
• An applet can be a fully functional Java
application because it has the entire Java API
plug-in of the Web browser or a
at its disposal. separate runtime environment.
• key differences between an applet and a • The JVM on the user's machine
standalone Java application include: creates an instance of the applet
• An applet is a Java class that extends the
[Link] class. class and invokes various methods
• A main() method is not invoked on an applet, during the applet's lifetime.
and an applet class will not define main().
• Applets are designed to be embedded within • Applets have strict security rules
an HTML page. When a user views an HTML
page that contains an applet, the code for the
that are enforced by the Web
applet is downloaded to the user's machine. browser.
Key Applet methods
• init: This method is intended for whatever • destroy: This method is only called
initialization is needed for your applet. It is when the browser shuts down
called after the param tags inside the normally. Because applets are meant
applet tag have been processed.
to live on an HTML page, you should
• start: This method is automatically called not normally leave resources behind
after the browser calls the init method. It after a user leaves the page that
is also called whenever the user returns to
the page containing the applet after contains the applet.
having gone off to other pages. • paint: Invoked immediately after the
• stop: This method is automatically called start() method, and also any time the
when the user moves off the page on applet needs to repaint itself in the
which the applet sits. It can, therefore, be browser. The paint() method is
called repeatedly in the same applet. actually inherited from the [Link].
Example
import [Link].*;
These import statements bring the
import [Link].*;
classes into the scope of our
public class JamboApplet extends Applet
applet class:
{
public void paint (Graphics salamu) • [Link].
{ [Link] (“Karibu Sana ", 25, 50); • [Link].
} Without those import statements,
}//end of applet the Java compiler would not
recognize the classes Applet and
Graphics, which the applet class
refers to
The Applet CLASS:
• Every applet is an extension of the • Get the network location of the
[Link] class. applet class directory Print a
• The base Applet class provides status message in the browser
methods that a derived Applet class • Fetch an image
may call to obtain information and
services from the browser context. • Fetch an audio clip
• These include methods that do the • Play an audio clip Resize the
following: apple
• Get applet parameters
• Get the network location of the HTML
file that contains the applet
The Hello, World Applet
Invoking an Applet:
• An applet may be invoked by <html>
embedding directives in an <title>The welcome Applet for
HTML file and viewing the file Kenya</title>
through an applet viewer or <hr>
Java-enabled browser. <applet code=“[Link]"
• <Applet> tag is the basis of width="320" height="120"> If your
embedding and applet in an browser was Java-enabled, a “Karibu
HTML file. It has three Kenya" message would appear here.
mandatory attributes: code, </applet>
width and height. <hr>
</html>
• The code attribute of the <applet> tag is • The viewer or browser looks for the compiled
required. It specifies the Applet class to run. Java code at the location of the document. To
• Width and height are also required to specify specify otherwise, use the codebase attribute
the initial size of the panel in which an applet of the <applet> tag as shown:
runs. The applet directive must be closed with a • <applet codebase="[Link]
</applet> tag. code=“[Link]" width="320"
• If an applet takes parameters, values may be height="120">
passed for the parameters by adding <param> • If an applet resides in a package other than
tags between <applet> and </applet>. The
the default, the holding package must be
browser ignores text and other tags between
the applet tags. specified in the code attribute using the
period character (.) to separate package/class
• Non-Java-enabled browsers do not process components. For example:
<applet> and </applet>. Therefore, anything
that appears between the tags, not related to • <applet
the applet, is visible in nonJava-enabled code="[Link]"
browsers. width="320" height="120">
Getting Applet Parameters
Skeleton Example:
import [Link].*; private void parseSquareSize (String param)
import [Link].*; {if (param == null) return;
public class TestApplet extends Applet try {
{ int squareSize = 50;// initialized to default size public squareSize = [Link] (param);
void init ()
}
{
catch (Exception e) {
String squareSizeParam = getParameter ("squareSize");
parseSquareSize (squareSizeParam); // Let default value remain
String colorParam = getParameter ("color"); }
Color fg = parseColor (colorParam); }
setBackground ([Link]); Color parseColor (String param) {} //incomplete
setForeground (fg); public void paint (Graphics g) {} //empty body
} }//end of TestApplet
• The applet calls parseSquareSize() • The applet calls parseColor() to
to parse the squareSize parameter. parse the color parameter into a
• parseSquareSize() calls the library Color value.
method [Link](), which • parseColor() does a series of string
parses a string and returns an comparisons to match the
integer. [Link]() throws an parameter value to the name of a
exception whenever its argument is predefined color. You need to
invalid. implement these methods to make
• Therefore, parseSquareSize() this applet works.
catches exceptions, rather than
allowing the applet to fail on bad
input.
Specifying Applet Parameters:
<html>
<title>TestApplet</title>
<hr>
<applet code=“[Link]"
width="480" height="320">
<param name="color" value="blue">
<param name="squaresize" value="30">
</applet>
<hr>
</html>
Event Handling:
• Applets inherit a group of event- • An applet can display images of
handling methods from the the format GIF, JPEG, BMP, and
Container class. others. To display an image
• The Container class defines within the applet, you use the
several methods, such as drawImage() method found in
processKeyEvent and the [Link] class.
processMouseEvent, for
handling particular types of
events, and then one catch-all
method called processEvent.
Playing Audio:
• An applet can play an audio file • To obtain an AudioClip object,
represented by the AudioClip you must invoke the
interface in the [Link] getAudioClip() method of the
package. The AudioClip interface Applet class.
has three methods, including: • The getAudioClip() method
• public void play(): Plays the audio returns immediately, whether or
clip one time, from the beginning. not the URL resolves to an actual
public void loop(): Causes the audio file.
audio clip to replay continually
• public void stop(): Stops playing • The audio file is not downloaded
the audio clip until an attempt is made to play
the audio clip