Applet Basics

Comprehensive study notes, diagrams, and exam preparation for Applet Basics.

Applet Basics

Definition

An applet is a small Java program that is designed to be embedded inside a web page or executed within an applet viewer, and it runs under the control of a browser or a special applet container rather than as a standalone desktop application. In Java, applets are created by extending the Applet class or, more commonly in modern Java GUI programming, the JApplet class (from Swing), and they follow a special life cycle managed by the container. Applets were historically used to add interactivity, graphics, animation, and user input handling to web pages.


Main Content

1. Applet Concept and Characteristics

Applet as a GUI-based Java program

An applet is not executed like a normal Java application with a main() method. Instead, it is loaded by a browser or applet viewer, and its execution is managed by predefined life-cycle methods such as init(), start(), stop(), and destroy(). This makes applets suitable for event-driven and graphical tasks.

Key characteristics of applets

Applets are typically platform-independent because they are written in Java and run on the Java Virtual Machine. They are also small in size, secure by design due to sandbox restrictions, and able to respond to user events such as mouse clicks, keyboard input, and button presses. However, they have limited system access for security reasons.

Example:
An applet can display a moving shape, a chart, or a simple interactive form directly inside a web page area.

import java.applet.Applet;
import java.awt.Graphics;

public class HelloApplet extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello, Applet!", 50, 50);
    }
}

2. Applet Structure and Life Cycle

Applet class hierarchy and structure

An applet usually extends java.applet.Applet, which provides the basic framework for rendering and interaction. The applet can override specific methods to control initialization, display, and cleanup. It uses AWT components and graphics for output.

Life cycle methods

The applet life cycle is the sequence of stages the applet passes through:

  • init() is called once when the applet is first loaded.
  • start() is called after init() and every time the applet becomes active again.
  • paint(Graphics g) is called whenever the applet must be redrawn.
  • stop() is called when the applet is no longer visible or active.
  • destroy() is called before the applet is removed from memory.

Example life cycle flow:

Loading applet
     |
     v
   init()
     |
     v
   start()
     |
     v
  paint()
     |
     v
   stop()
     |
     v
 destroy()

Explanation:
This sequence shows that the browser controls when the applet starts, stops, and is destroyed. The programmer does not manually call these methods in ordinary use.


3. Applet Execution, Display, and User Interaction

How an applet runs

The applet is embedded in an HTML page and loaded by an applet-enabled browser or an applet viewer. The browser creates an applet object, initializes it, and displays it within a rectangular area called the applet area. The applet then paints its content and responds to events.

User interaction and event handling

Applets can handle user interaction through event listeners and GUI components. For example, buttons, text fields, and mouse events can be used to make the applet interactive. The paint() method is used to draw on the screen, while event-handling methods respond to user actions.

Example of simple interactive applet:

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class ClickApplet extends Applet implements MouseListener {
    String message = "Click inside the applet";

    public void init() {
        addMouseListener(this);
    }

    public void paint(Graphics g) {
        g.drawString(message, 20, 20);
    }

    public void mouseClicked(MouseEvent e) {
        message = "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) {}
}

What happens here:
The applet listens for mouse clicks. When the user clicks, it updates the message and calls repaint(), which triggers paint() again to display the new text.


Working / Process

1. Write the applet class

  • by extending Applet or JApplet, and override the needed life-cycle methods such as init() and paint().

2. Embed the applet in an HTML file

  • using an appropriate applet tag or viewer-supported configuration, so the container knows where and how to load it.

3. Load and run the applet in a container

  • ; the container calls the life-cycle methods, displays the applet, and processes user interactions and repaint requests.

Advantages / Applications

Platform independence

Because applets are written in Java, they can run on any system with a compatible Java runtime and applet-supporting container.

Rich interactivity in web pages

Applets were used to add interactive content such as animations, games, calculators, scientific simulations, and graphical demonstrations to web pages.

Useful for educational demonstrations

Applets were especially popular for teaching programming, mathematics, physics, and engineering because they could visually demonstrate concepts with live interaction.


Summary

  • Applets are small Java programs that run inside a browser or applet viewer.
  • They work through a managed life cycle instead of a main() method.
  • Applets can display graphics and respond to user events in an embedded area.
  • Important terms to remember: Applet, init(), start(), paint(), stop(), destroy(), repaint()