Skip to content

Applet Programming

  • An applet is a Java program that runs in a web browser.
  • An applet is a Java class that extends the java.applet.Applet.
  • No main() method
  • Applet was designed to be embedded with a HTML page.
  • JVM is required to view an applet.
  • Applet have strict security rules that are enforced by the web browser.

Two types of Applet

  1. Based on Applet class
  2. These Applets use the Abstract Window Toolkit (AWT) to provide the graphical user interface (GUI).
  3. The style of applet has been available since Java was first created.
  4. Based on JApplet class
    • They are based on the Swing class JApplet which inherits Applet.
    • Swing applet use the swing classes to provide the GUI.

Life Cycle of Applet

Life Cycle of Applet

Applet Initialization and Termination

  • When an applet begins ,the following methods are called in this sequence :
  • init()
  • start()
  • paint() Whenever an applet is terminated, the following sequence of methods calls takes place.
  • stop()
  • destroy()

Description :

  1. init()
  2. The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet.

  3. start()

  4. The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

  5. paint()

  6. The paint( ) method is called each time an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. Whatever the applet must redraw its output, paint( ) method is called. The paint( ) method has one parameter of type Graphics.

  7. stop()

  8. The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

  9. destroy()

  10. The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).

The HTML APPLET Tag

Syntax :

    <applet
    [codebase = codebaseURL]
    code = appletFile
    [alt = alternateText]
    [name = appletInstanceName]
    width = pixels
    height = pixels
    [align = alignment]
    [vspace = pixels]
    [hspace = pixels]
    >

    [< PARAM NAME = attributeName VALUE = AttributeValue>]  >> Important for Exam 💯
    [HTML Displayed in the absence of Java]

    </applet>

Note : [ ] ⏩ Optional

CODEBASE

  • It is an optional attribute that specifies the base URL of the applet code, which is the directory that will be searched for the applets executable class file.
  • The HTML Document URL Directory is used as the CODEBASE if this attribute is not specified.

CODE

  • Code is the required attribute that gives the name of file containing compiled .class file. This file is related to the code base URL of the applet.

ALT

  • The ALT tag is an alternate tag used to specify a short text message that should be displayed if the browser recognized the applet tag but can't currently run JAVA applet.

NAME

  • NAME is an optional attribute used to specify a name for the applet instance. NAME attribute is used in order to communicate between applets on the same page.

WIDTH and HEIGHT

  • WIDTH and HEIGHT are the required attributes that gives the size of the applet display area.

ALIGN

  • ALIGN is an optional attribute that specifies the alignment of the applet. The possible values are LEFT, RIGHT, TOP, BOTTOM, MIDDLE, BASELINE, TEXTTOP, ABSMIDDLE (Absolute Middle) and ABSBOTTOM (Absolute Bottom).

VSPACE and HSPACE

  • These attributes are optional.
  • VSPACE specifies the space in pixel and below the applet.
  • HSPACE specifies the space in pixel on each sides of the applet.

PARAM NAME and VALUE

  • PARAMNAME is a tag used to specify applet specific arguments.
  • Applets access their attributes with the getParameter( )

Life Cycle of Applet

import java.applet.*;
import java.awt.*;

/*
    <applet code = "LifeCycle.class" height = "300" width = "300">

    </applet>
*/

public class LifeCycle extends Applet{
    String message = "";

    public void init(Graphics graphicsObject){
        message += "Inside Init()     ";
        graphicsObject.drawString(message, 100, 120);
    }

    public void start() {
        message += "Inside Start()     ";
    }

    public void paint(Graphics graphicsObject) {
        message += "Inside Paint()     ";
        graphicsObject.drawString(message, 100, 120);
    }

    public void stop() {
        message += "Inside Stop()     ";
    }

    public void destroy() {
        message += "Inside Destroy()     ";
    }
}