☰ Topics


An applet is a special type of Java program that runs in a Java enabled web browser (i.e. Internet Explorer, Firefox). Applets are intended to be small programs and can be downloaded on demand. It can be a fully functional Java application because it has the entire Java API at its disposal.
An applet is a subclass of java.applet.Applet package. It does not consist of a main method unlike a Java application. Different states that an applet experiences between its object creation and object removal is known as Applet Life Cycle. Applet are designed to be embedded with in HTML page. When the user views an html page that contains applet, the code for the applet is downloaded to the users machine. The JVM on the user's machine creates an instance of the applet class and invokes various methods during the applet life cycle.

Applet life cycle

  1. public void init(): This method is intended for whatever initialization is needed for applet. This is called only once in the life cycle. It instantiate the objects and load the GUI of the applet. It is the born state of a thread. It is called after the param tags inside the applet tag have been processed.

  2. public void start(): Even though applet object is created by the init() method, it remains in inactive state. The init() method calls start() method & it starts the execution of applet. It is also called whenever the user returns to the page containing the applet after having gone off to the other page.

  3. public void stop(): This method is automatically called when the users moves off the page on which the applet reside. In this method, the applet becomes temporarily inactive. It is the equivalent to the blocked state of the thread.

  4. public void paint(): This method takes a object as parameter of java.awt.Graphics. It helps to create Applet's GUI. It is invoked immediately after the start() and also at any time the applet needs to repaint itself in the browser. This is equivalent to runnable state of thread.

  5. public void destroy(): This destroys the Applet and is also invoked only once when the active browser which contains the page is closed not minimized. It is equivalent to the dead state of the thread.

For example

 import java.applet.*;
 import java.awt.*;
 /* <applet code="applet1" width="400" height="400">
    <applet> */
 public class Hello extends Applet{
    public void paint(Graphics g){
        g.drawString("Hello Java",27,50);

Note: To run an applet the HTML code must be embedded in the Java program itself within comments.