Understanding the Applet Class in Java: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-05 04:59:13   9197  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Applet Class

Applet class provides all necessary support for applet execution, such as initializing and destroying of applet. It also provide methods that load and display images and methods that load and play audio clips. Every applet is an extension of the java.applet.Applet class. The base Applet class provides methods that a derived Applet class may call to obtain information and services from the browser context.

An Applet Skeleton

Most applets override these four methods. These four methods forms Applet lifecycle.

  • init() : init() is the first method to be called. This is where variable are initialized. This method is called only once during the runtime of applet.
  • start() : start() method is called after init(). This method is called to restart an applet after it has been stopped.
  • stop() : stop() method is called to suspend thread that does not need to run when applet is not visible.
  • destroy() : destroy() method is called when your applet needs to be removed completely from memory.

Note: The stop() method is always called before destroy() method.

These include methods that do the following ?

  • Get applet parameters
  • Get the network location of the HTML file that contains the applet
  • Get the network location of the applet class directory
  • Print a status message in the browser
  • Fetch an image
  • Fetch an audio clip
  • Play an audio clip
  • Resize the applet

Additionally, the Applet class provides an interface through which the viewer or browser obtains information about the applet and controls the applet's execution. The viewer may ?

  • Request information about the author, version, and copyright of the applet
  • Request a description of the parameters the applet recognizes
  • Initialize the applet
  • Destroy the applet
  • Start the applet's execution
  • Stop the applet's execution

The Applet class provides default implementations of each of these methods. Those implementations may be overridden as necessary.

Example of an Applet Skeleton

Program Skeleton:

import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
 public void init()
 {
  //initialization here
 }
 public void start ()
 {
  //start or resume execution here
 }
 public void stop()
 {
  //suspend execution here
 {
 public void destroy()
 {
  //perform shutdown activity here
 }
 public void paint (Graphics g)
 {
  //display the content of window here
 }
}

Example of an Applet

import java.applet.*;
import java.awt.*;
public class Simple extends Applet
{
 int height, width;
 public void init()
 {
  height = getSize().height;
  width = getSize().width;
  setName("Simple");
 }
 public void paint(Graphics g)
 {
  g.drawRoundRect(20, 40, 130, 150, 2, 3);
 }
}


/*
  <applet code="Simple" width="300" height="300">
  
  </applet>

  */
  

Now Compile the program and run using appletviewer by cmd

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Users\Hello World>I:

I:\>cd Java Programming

I:\Java Programming>javac Simple.java

I:\Java Programming>appletviewer Simple.java
 

Output

Java applet output
MCQ Available

There are 1 MCQs available for this topic.

1 MCQ