Applet Architecture
Definition
Applet Architecture refers to the complete structural design, execution model, and life-cycle mechanism by which a Java applet is loaded, initialized, displayed, controlled, and terminated inside a Java-enabled environment such as a web browser or an applet viewer. It defines how an applet interacts with the Java Virtual Machine (JVM), the browser, the security sandbox, the AWT-based user interface, and the predefined applet life-cycle methods. In simple terms, applet architecture explains how an applet works from loading to unloading and how its components cooperate to produce interactive behavior in a controlled environment.
Main Content
1. Applet Environment and Structure
Applet as a Java program embedded in another application
An applet is not a standalone Java application. It runs inside a container such as a browser or appletviewer. The container is responsible for creating the applet object, giving it a screen area, and managing its execution. Unlike a normal Java program with a main() method, an applet depends on the host environment for startup and display.
Inheritance from java.applet.Applet or javax.swing.JApplet
The architecture is built around a predefined class hierarchy. Traditionally, an applet extends java.applet.Applet, which is an AWT-based component. In Swing-based applets, javax.swing.JApplet is used. This inheritance provides built-in methods for initialization, painting, resizing, and life-cycle management.
Example structure of a basic applet:
import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Hello Applet", 50, 50);
}
}
Basic architectural view:
Browser/AppletViewer
|
v
Applet Container
|
v
Applet Class Instance
|
v
JVM + Security Manager + AWT/Swing Components
2. Life Cycle of an Applet
The applet life cycle controls execution stages
Applet architecture is centered on a set of life-cycle methods that are automatically invoked by the container. These methods determine when the applet is created, started, paused, resumed, and destroyed. The programmer does not manually call these methods in normal use; the browser or viewer does it.
Main life-cycle methods and their roles
init()— called once when the applet is loaded; used for initialization.start()— called afterinit()and whenever the applet becomes active.paint(Graphics g)— called whenever the applet must be redrawn.stop()— called when the applet becomes inactive, such as when the user leaves the page.destroy()— called when the applet is removed from memory or the browser closes.
Life-cycle flow:
Load applet
|
v
init() --> called once
|
v
start() --> called every time applet becomes active
|
v
paint() --> called whenever display needs updating
|
v
stop() --> called when applet is no longer visible/active
|
v
destroy() --> final cleanup
Detailed explanation of life-cycle behavior:
init()is best for setting up variables, UI elements, images, fonts, and event handlers.start()is used to resume animations, threads, or processes that were paused instop().paint()is responsible for rendering text, shapes, images, and graphics on the applet area.stop()should pause background tasks to reduce resource usage.destroy()should release system resources and perform final cleanup.
Example of life-cycle usage:
import java.applet.Applet;
import java.awt.Graphics;
public class LifeCycleApplet extends Applet {
public void init() {
System.out.println("Applet initialized");
}
public void start() {
System.out.println("Applet started");
}
public void paint(Graphics g) {
g.drawString("Applet is running", 20, 20);
}
public void stop() {
System.out.println("Applet stopped");
}
public void destroy() {
System.out.println("Applet destroyed");
}
}
3. Security, Communication, and Rendering Model
Applet sandbox security architecture
Applets are designed to run in a restricted environment called the sandbox. This security model prevents applets from performing dangerous actions such as reading local files, writing to the hard disk, running arbitrary programs, or connecting to unauthorized systems. The sandbox protects the client machine from untrusted code downloaded from the internet.
Rendering and event-driven interaction
Applets use Java’s event-driven model and graphical output mechanisms. The paint() method handles drawing, while mouse, keyboard, and action events allow user interaction. Applet architecture therefore combines GUI rendering with event handling inside the container-managed environment.
Communication and network access limitations
Applets may communicate with the host they were loaded from, but access to other network resources or local system features is restricted unless special permissions are granted. This is part of the security architecture that ensures safe execution.
Security model illustration:
Applet Code
|
v
Restricted Sandbox Environment
/ | \
v v v
No file access No OS access Limited network access
Rendering workflow:
- The browser allocates a rectangular area for the applet.
- The applet draws its content using
Graphics. - If the applet area is covered, resized, or refreshed, the container triggers repainting.
- The applet must be prepared to redraw content at any time.
Example of event-driven rendering:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class ClickApplet extends Applet implements MouseListener {
String msg = "Click inside the applet";
public void init() {
addMouseListener(this);
}
public void paint(Graphics g) {
g.drawString(msg, 30, 30);
}
public void mouseClicked(MouseEvent e) {
msg = "Mouse clicked at: " + e.getX() + ", " + e.getY();
repaint();
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
Working / Process
1. The applet is written and compiled
- The developer creates a Java class that extends
AppletorJApplet. - The code is compiled into bytecode (
.classfile). - Any required resources such as images or audio files are prepared.
2. The applet is loaded by a container
- The browser or
appletviewerreads the HTML file containing the applet tag or equivalent embedding. - The JVM loads the class and creates an object of the applet.
- The security manager applies sandbox restrictions before execution begins.
3. The container manages the applet life cycle and display
init()is called for setup.start()begins or resumes execution.paint()draws the interface.- If the applet becomes hidden or the user navigates away,
stop()is called. - When the applet is closed or unloaded,
destroy()is called for cleanup.
Process diagram:
Compile applet
|
v
Embed in HTML / run in viewer
|
v
Load class into JVM
|
v
Apply sandbox security
|
v
Call init() -> start() -> paint()
|
v
User interacts / repaint occurs
|
v
stop() -> destroy()
Advantages / Applications
Interactive graphical components for web-based learning
Applets were widely used to create dynamic simulations, educational animations, calculators, and graphical demonstrations within web pages. Their architecture made it easy to provide interactive content without requiring a separate desktop application.
Platform independence through Java bytecode
Because applets are written in Java, the same applet could run on any system with a compatible JVM and browser support. This portability was one of the major architectural strengths of the Java applet model.
Built-in security and managed execution
The sandbox architecture allowed code from untrusted sources to run with limited privileges, reducing risk to the user system. The container-controlled life cycle also simplified execution management, memory handling, and display refresh behavior.
Summary
- Applet architecture is the structure that governs how a Java applet is loaded, displayed, controlled, and terminated.
- It depends on a container, a JVM, predefined life-cycle methods, and a secure sandbox environment.
- The main idea is controlled execution through
init(),start(),paint(),stop(), anddestroy(). - Important terms to remember: Applet, Container, JVM, Sandbox, Life Cycle,
init(),start(),paint(),stop(),destroy()