Skip to content

Applet Class in Java

The Applet class in Java is part of the java.applet package and is the base class for creating applets. Applets are small Java programs that run within a web browser or an applet viewer. The Applet class provides the foundational methods and functionality required to develop applet-based applications.


Inheritance Hierarchy

The Applet class extends the Panel class from the AWT package, which in turn extends the Container class. The hierarchy is as follows:

java.lang.Object

   ↳ java.awt.Component

       ↳ java.awt.Container

           ↳ java.awt.Panel

               ↳ java.applet.Applet


Key Features

  1. Event Handling: The Applet class can handle user input events such as mouse clicks and keyboard interactions using the AWT event-handling model.
  2. Graphics Support: It provides methods to render graphical components using the Graphics class.
  3. Lifecycle Management: The Applet class defines the lifecycle of an applet through methods like init(), start(), stop(), and destroy().
  4. HTML Integration: Applets can interact with HTML parameters passed via the <applet> tag using the getParameter() method.

Important Methods in the Applet Class

MethodDescription
init()Initializes the applet. Called once after the applet is loaded.
start()Starts or resumes the execution of the applet. Called after init() or when the applet becomes visible.
stop()Pauses the execution of the applet when it is no longer visible.
destroy()Cleans up resources before the applet is terminated.
paint(Graphics g)Used to draw graphics or text on the applet.
getParameter(String name)Retrieves a parameter passed to the applet from the HTML page.
getAppletContext()Returns the context of the applet, allowing communication with other applets or the environment.
resize(int width, int height)Resizes the applet window.

Lifecycle of an Applet

The Applet class lifecycle has four main phases, each represented by a method:

  1. Initialization (init()):
    1. Executed once when the applet is loaded.
    1. Used for initializing variables, setting up the UI, or allocating resources.
  2. Starting (start()):
    1. Executed each time the applet becomes visible or active.
    1. Used for starting animations or resuming activities.
  3. Stopping (stop()):
    1. Executed when the applet is no longer visible or the user navigates away from the page.
    1. Used for pausing threads or stopping animations.
  4. Destruction (destroy()):
    1. Executed once before the applet is removed from memory.
    1. Used for releasing resources like files, threads, or sockets.

Example of an Applet

import java.applet.Applet;

import java.awt.Graphics;

public class AppletExample extends Applet {

    // Initialization method

    @Override

    public void init() {

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

    }

    // Start method

    @Override

    public void start() {

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

    }

    // Stop method

    @Override

    public void stop() {

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

    }

    // Destroy method

    @Override

    public void destroy() {

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

    }

    // Paint method

    @Override

    public void paint(Graphics g) {

        g.drawString(“Hello, Applet!”, 50, 50);

    }

}


HTML to Run the Applet

html

Copy code

<html>

<body>

    <applet code=”AppletExample.class” width=”300″ height=”200″>

    </applet>

</body>

</html>

  • Save this HTML file in the same directory as the compiled AppletExample.class file.

Running the Applet

  1. Compile the Java file:

javac AppletExample.java

  • Use the appletviewer tool to view the applet:

appletviewer AppletExample.html


Advantages of Applet Class

  1. Simplifies the creation of graphical and interactive web-based applications.
  2. Provides secure execution in a sandboxed environment.
  3. Allows interaction with HTML content using parameters.

Limitations of the Applet Class

  1. Deprecated Technology: Browsers have phased out support for applets due to security and compatibility concerns.
  2. Performance Issues: Applets can be slower compared to modern alternatives like JavaFX or JavaScript.
  3. Dependency on Java Plugin: Requires a compatible Java plugin, which is no longer supported in most modern browsers.