0% found this document useful (0 votes)
1 views

mod9

The document provides an overview of the Applet class in Java, which is used to create interactive applications that run within web browsers. It details the applet lifecycle, including key methods such as init(), start(), stop(), destroy(), and paint(), as well as how to set colors and display messages. Additionally, it explains how to embed applets in HTML using the <applet> tag and how to pass parameters to applets.

Uploaded by

mandalipsita2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

mod9

The document provides an overview of the Applet class in Java, which is used to create interactive applications that run within web browsers. It details the applet lifecycle, including key methods such as init(), start(), stop(), destroy(), and paint(), as well as how to set colors and display messages. Additionally, it explains how to embed applets in HTML using the <applet> tag and how to pass parameters to applets.

Uploaded by

mandalipsita2002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Web Technology - Module IX

Applet
Applet Class

L
● The Applet class in Java is part of the java.applet package and extends
java.awt.Panel. It provides a framework for creating small programs that are run

A
within a web browser.
● Applets are typically used to create interactive applications that can run inside a
webpage, though they are now less common due to security issues and the rise of
alternative web technologies.

D
● An applet runs in a restricted environment, known as a sandbox, which limits its access
to certain system resources for security reasons.

Applet Architecture

N
● The architecture of an applet revolves around four key lifecycle methods that manage
its initialization, running, pausing, and termination.

● A
When an applet is loaded, the applet's lifecycle methods are called in a specific
sequence, which manages how the applet behaves within a webpage.
Applet execution is managed by the Java Plug-in or Java-enabled browsers, which
handle interactions between the applet and the HTML document hosting it.
M
Applet Skeleton

● The basic structure of an applet includes overriding key lifecycle methods from the
Applet class, which control the applet’s behavior.
B

● Basic Skeleton:
LA
IP
B
L
A
Applet Lifecycle Methods

D
● The lifecycle of an applet is controlled by these methods:
○ init(): Called once when the applet is first loaded. This is where you initialize
variables or set up the applet’s environment.

N
○ start(): Called each time the applet becomes active, such as when the user
navigates back to the page containing the applet. It resumes any activity that was
paused or stopped.
○ stop(): Called when the applet is no longer visible to the user or the user

A
navigates away. Any ongoing activity in the applet should be paused.
○ destroy(): Called when the applet is being permanently unloaded. This is used to
release resources and perform final cleanup.
M
○ paint(Graphics g): Called whenever the applet needs to redraw itself. This
method is often used to render text, shapes, and images.

setForeground() and setBackground() Methods


B

● setForeground(Color color): Sets the foreground color for the applet, which is the color
used for drawing text and shapes.
○ Example: setForeground(Color.RED);
LA

● setBackground(Color color): Sets the background color for the applet, which is the
color of the applet’s background area.
○ Example: setBackground(Color.LIGHT_GRAY);
● These methods are often called in the init() method to set up the appearance of the
applet.
IP

Using the Status Window

● The status window is a small area in the browser (often at the bottom) where the applet
can display text messages to the user.
B

● The Applet class provides the showStatus(String message) method to display


messages in the status bar.
● Example:

L
HTML <applet> Tag

A
● To include an applet in an HTML page, the <applet> tag (now deprecated) was used to
embed the Java applet.
● Basic Syntax:

D
N
● Attributes:

A
○ code: Specifies the class file of the applet.
○ width and height: Set the size of the applet window.
○ param: Passes parameters to the applet (explained further below).
M
Passing Parameters to an Applet

● HTML <param> tags can be used to pass values to an applet.


● In the applet code, these parameters can be retrieved using getParameter(String
B

name).
● Example:
LA
IP

● In the Applet:
B

getCodeBase() and getDocumentBase() Methods


● These methods are used to retrieve the URLs associated with the applet.
● getCodeBase():
○ Returns the URL of the directory from which the applet's code (class file) was
loaded.
○ Useful for accessing resources stored in the same directory as the applet class
files.

L
○ Example:

A
● getDocumentBase():

D
○ Returns the URL of the document (HTML file) that contains the applet.
○ Useful when the applet needs to interact with other files or resources in the
HTML file's location.

N
○ Example:

Summary of Applet Lifecycle and Key Methods A


M
Method Purpose Typical Usage Location
B

init() Initialize applet state Called once, upon loading

start() Start or resume execution Each time the applet


becomes visible
LA

stop() Pause or stop execution When user navigates away

destroy() Final cleanup before applet Called once, before


termination unloading

paint() Renders applet graphics Called when applet needs


IP

redrawing
B

You might also like