Skip to content

Introduction to Applets in Java

An applet is a small Java program that is designed to run within a web browser or an applet viewer. It is primarily used for creating dynamic and interactive web applications. Applets are part of the java.applet package and are now considered a legacy technology due to the evolution of more modern web development frameworks.


Key Features of Applets

  1. Platform-Independent: Applets are written in Java, making them platform-independent.
  2. GUI Support: They can create graphical user interfaces (GUIs) using the Abstract Window Toolkit (AWT).
  3. Embedded in Web Pages: Applets can be embedded in HTML pages and executed using a compatible browser.
  4. Secure Execution: Applets run in a restricted environment (sandbox) to ensure security.
  5. Dynamic and Interactive: Applets are often used for animations, games, and real-time data processing on websites.

Life Cycle of an Applet

An applet has a well-defined life cycle with the following methods:

  1. init():
    1. Called once when the applet is first loaded.
    1. Used for initializing resources like variables or starting background threads.
  2. start():
    1. Called after init() or when the applet becomes visible again (e.g., the user returns to the webpage).
    1. Used to resume operations or start animations.
  3. stop():
    1. Called when the applet is no longer visible (e.g., the user navigates away from the webpage).
    1. Used to pause animations or stop background threads.
  4. destroy():
    1. Called when the applet is removed from memory.
    1. Used to release resources like threads or file handles.
  5. paint(Graphics g):
    1. Called to draw the applet’s content on the screen.
    1. The Graphics object provides methods for rendering shapes, text, and images.

Creating a Simple Applet

Example:

import java.applet.Applet;

import java.awt.Graphics;

public class SimpleApplet extends Applet {

    // Called when the applet is first loaded

    public void init() {

        System.out.println(“Applet initialized”);

    }

    // Called each time the applet is started

    public void start() {

        System.out.println(“Applet started”);

    }

    // Called to repaint the applet

    public void paint(Graphics g) {

        g.drawString(“Welcome to Java Applet!”, 50, 50);

    }

    // Called when the applet is stopped

    public void stop() {

        System.out.println(“Applet stopped”);

    }

    // Called when the applet is destroyed

    public void destroy() {

        System.out.println(“Applet destroyed”);

    }

}


Running an Applet

  1. Create an HTML File: Applets are embedded in an HTML page using the <applet> tag (deprecated). For example:

<html>

<body>

    <applet code=”SimpleApplet.class” width=”300″ height=”150″></applet>

</body>

</html>

  • Use an Applet Viewer: Java provides the appletviewer tool to run applets for development purposes:

appletviewer SimpleApplet.html


Advantages of Applets

  1. Easy to embed dynamic content in web pages.
  2. Secure execution due to the sandbox model.
  3. Platform-independent and portable.

Disadvantages of Applets

  1. Browser Support: Most modern browsers have removed support for Java Applets due to security concerns.
  2. Performance: Applets may not be as fast as native applications or modern web technologies.
  3. Obsolete Technology: Applets are now deprecated in favor of newer frameworks like JavaFX, HTML5, and JavaScript-based libraries.

Alternatives to Applets

Due to the decline in applet usage, alternatives include:

  • JavaFX: A modern framework for creating rich desktop and web applications.
  • HTML5 and JavaScript: Popular for interactive web content.
  • React.js and Angular: Frameworks for dynamic and responsive web applications.