mod9
mod9
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(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
● 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
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
name).
● Example:
LA
IP
● In the Applet:
B
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:
redrawing
B