0% found this document useful (0 votes)
17 views8 pages

Applet 2

Uploaded by

rockps12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
17 views8 pages

Applet 2

Uploaded by

rockps12345
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Java Applet

Applet is a special type of program that is embedded in the webpage to generate the
dynamic content. It runs inside the browser and works at client side.

Advantage of Applet
There are many advantages of applet. They are as follows:

o It works at client side so less response time.


o Secured
o It can be executed by browsers running under many plateforms, including Linux,
Windows, Mac Os etc.

Drawback of Applet
o Plugin is required at client browser to execute applet.

Hierarchy of Applet
An applet is a Java program that runs in a Web browser. An applet can be a fully
functional Java application because it has the entire Java API at its disposal.
There are some important differences between an applet and a standalone Java
application, including the following −
• An applet is a Java class that extends the java.applet.Applet class.
• A main() method is not invoked on an applet, and an applet class will not define main().
• Applets are designed to be embedded within an HTML page.
• When a user views an HTML page that contains an applet, the code for the applet is
downloaded to the user's machine.
• A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser
or a separate runtime environment.
• The JVM on the user's machine creates an instance of the applet class and invokes
various methods during the applet's lifetime.
• Applets have strict security rules that are enforced by the Web browser. The security of
an applet is often referred to as sandbox security, comparing the applet to a child playing
in a sandbox with various rules that must be followed.
• Other classes that the applet needs can be downloaded in a single Java Archive (JAR)
file.

Life Cycle of an Applet

o init(): The init() method is the first method to run that initializes the applet. It can be invoked
only once at the time of initialization. The web browser creates the initialized objects, i.e.,
the web browser (after checking the security settings) runs the init() method within the
applet.

o start(): The start() method contains the actual code of the applet and starts the applet. It is
invoked immediately after the init() method is invoked. Every time the browser is loaded or
refreshed, the start() method is invoked. It is also invoked whenever the applet is
maximized, restored, or moving from one tab to another in the browser. It is in an inactive
state until the init() method is invoked.

o stop(): The stop() method stops the execution of the applet. The stop () method is invoked
whenever the applet is stopped, minimized, or moving from one tab to another in the
browser, the stop() method is invoked. When we go back to that page, the start() method is
invoked again.

o destroy(): The destroy() method destroys the applet after its work is done. It is invoked
when the applet window is closed or when the tab containing the webpage is closed. It
removes the applet object from memory and is executed only once. We cannot start the
applet once it is destroyed.

o paint(): The paint() method belongs to the Graphics class in Java. It is used to draw shapes
like circle, square, trapezium, etc., in the applet. It is executed after the start() method and
when the browser or applet windows are resized.

Flow of Applet Life Cycle:


These methods are invoked by the browser automatically. There is no need to call them
explicitly.
class TestAppletLifeCycle extends Applet {

public void init() {

// initialized objects

public void start() {

// code to start the applet

public void paint(Graphics graphics) {

// draw the shapes

public void stop() {

// code to stop the applet

public void destroy() {

// code to destroy the applet

A "Hello, World" Applet


Following is a simple applet named HelloWorldApplet.java −
import java.applet.*;
import java.awt.*;

public class HelloWorldApplet extends Applet {


public void paint (Graphics g) {
g.drawString ("Hello World", 25, 50);
}
}
These import statements bring the classes into the scope of our applet class −

• java.applet.Applet
• java.awt.Graphics
Without those import statements, the Java compiler would not recognize the classes
Applet and Graphics, which the applet class refers to.
How to run an Applet?
There are two ways to run an applet

1. By html file.
2. By appletViewer tool (for testing purpose).

AppletViewer

c:\>javac HelloWorld.java
c:\>appletviewer HelloWorld.java

HTML Webpage

<html>

<body>

<applet code="HelloWorld.class" width="300" height="300">

</applet>

</body>

</html>

The Applet Class


Every applet is an extension of the java.applet.Applet class. The base Applet class
provides methods that a derived Applet class may call to obtain information and
services from the browser context.
These include methods that do the following −

• Get applet parameters


• Get the network location of the HTML file that contains the applet
• Get the network location of the applet class directory
• Print a status message in the browser
• Fetch an image
• Fetch an audio clip
• Play an audio clip
• Resize the applet
Additionally, the Applet class provides an interface by which the viewer or browser
obtains information about the applet and controls the applet's execution. The viewer
may −

• Request information about the author, version, and copyright of the applet
• Request a description of the parameters the applet recognizes
• Initialize the applet
• Destroy the applet
• Start the applet's execution
• Stop the applet's execution
The Applet class provides default implementations of each of these methods. Those
implementations may be overridden as necessary.
The "Hello, World" applet is complete as it stands. The only method overridden is the
paint method.

Invoking an Applet

Getting Applet Parameters


The following example demonstrates how to make an applet respond to setup
parameters specified in the document. This applet displays a checkerboard pattern
of black and a second color.
The second color and the size of each square may be specified as parameters to the
applet within the document.
CheckerApplet gets its parameters in the init() method. It may also get its parameters
in the paint() method. However, getting the values and saving the settings once at
the start of the applet, instead of at every refresh, is convenient and efficient.
The applet viewer or browser calls the init() method of each applet it runs. The viewer
calls init() once, immediately after loading the applet. (Applet.init() is implemented to
do nothing.) Override the default implementation to insert custom initialization code.
The Applet.getParameter() method fetches a parameter given the parameter's name
(the value of a parameter is always a string). If the value is numeric or other non-
character data, the string must be parsed.
The following is a skeleton of CheckerApplet.java −
import java.applet.*;
import java.awt.*;

public class CheckerApplet extends Applet {


int squareSize = 50; // initialized to default size
public void init() {}
private void parseSquareSize (String param) {}
private Color parseColor (String param) {}
public void paint (Graphics g) {}
}
Here are CheckerApplet's init() and private parseSquareSize() methods −
public void init () {
String squareSizeParam = getParameter ("squareSize");
parseSquareSize (squareSizeParam);

String colorParam = getParameter ("color");


Color fg = parseColor (colorParam);

setBackground (Color.black);
setForeground (fg);
}

private void parseSquareSize (String param) {


if (param == null) return;
try {
squareSize = Integer.parseInt (param);
} catch (Exception e) {
// Let default value remain
}
}
The applet calls parseSquareSize() to parse the squareSize parameter.
parseSquareSize() calls the library method Integer.parseInt(), which parses a string
and returns an integer. Integer.parseInt() throws an exception whenever its argument
is invalid.
Therefore, parseSquareSize() catches exceptions, rather than allowing the applet to
fail on bad input.
The applet calls parseColor() to parse the color parameter into a Color value.
parseColor() does a series of string comparisons to match the parameter value to the
name of a predefined color. You need to implement these methods to make this
applet work.

Specifying Applet Parameters


The following is an example of an HTML file with a CheckerApplet embedded in it.
The HTML file specifies both parameters to the applet by means of the <param> tag.
<html>
<title>Checkerboard Applet</title>
<hr>
<applet code = "CheckerApplet.class" width = "480" height =
"320">
<param name = "color" value = "blue">
<param name = "squaresize" value = "30">
</applet>
<hr>
</html>
Note − Parameter names are not case sensitive.

You might also like