JAVA Life Cycle of an Applet

Four methods in the Applet class give you the framework on which you build any serious applet:

  • init: This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
  • start: This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
  • stop: This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
  • destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
  • paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.

The Applet Class

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.

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
  • ? Previous Next ?

    JAVA Life Cycle of an Applet

    Four methods in the Applet class give you the framework on which you build any serious applet:

  • init: This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.
  • start: This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.
  • stop: This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.
  • destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.
  • paint: Invoked immediately after the start() method, and also any time the applet needs to repaint itself in the browser. The paint() method is actually inherited from the java.awt.
  • The Applet Class

    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.

    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 by 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
  • A simple Applet

    Let's see an applet that will display "Dhanpat" in the applet frame. For this we use the paint() method of Component class of java.awt package. It is noted that all the methods of applet and applet class itself should be declared as public, otherwise they are not available to the browser to execute.

    // the following program creates an applet having white background color and a message "SILAN SOFTWARE".

    import java.awt.*;
    import java.applet.*;
    public class AppletDemo extends Applet
    {
    public void init()
    {
    setBackground(Color.white);
    }
    public void paint(Graphics g)
    {
    g.drawString("SILAN SOFTWARE",100,100);
    }
    }

    Output

    D:\app>javac AppletDemo.java

    Now AppletDemo.class is created which is called as byte code. Now we will embedded this byte code into a HTML page usingtag which is as follows :

     

    <html>
    <applet code="AppletDemo.class" height=250 width=350>
    </applet>
    </html>

    Now we will save the above HTML code in Silan.HTML file. This HTML page contains the applet which can be opened in the web browser or by giving the command at system prompt as :

    D:\app>appletviewer Silan.html

    applet

    Passing parameters to applets

    It is possible to pass user-defined parameters to an applet using <PARAM?> tags. Each <PARAM?> tag has a name attributes such as color, and a value attribute such as red. Inside the applet code, the applet can refer to that parameter by name to find it's value. For example, we can change the color of the text displayed to yellow by an applet by using a <PARAM?> tag as follows :

    <applet>
    <PARAM=color VALUE="yellow"<
    </applet>

    Similarly we can change the text to be displayed by an applet by supplying new text to the applet through a <PARAM?> tag as follows :

    <PARAM NAME=text VALUE="Silan Software" >

    To setup and handle parameters, we have to do two things :

  • Include appropriate tags in the HTML document.
  • Provide code in the applet to parse these parameters.
  • Parameters are passes on an applet when it is loaded. We can define the init() method in the applet to get hold of the parameters defined in the <PARAM> tags.

    This is done using getParameter() method which takes one string argument representing the name of the parameter and returns a string containing the value of that parameter.

    Let's see a program to understand the concept of passing parameters to applets:

    import java.awt.*;
    import java.applet.*;
    public class Himalaya extends Applet
    {
    String msg;
    public void init()
    {
    msg=getParameter("string");
    if(msg==null)
    msg="java";
    msg="welcome to"+msg;
    }
    public void paint(Graphics g)
    {
    g.drawString(msg,10,100);
    }
    }

    Output

    C:\>javac Himalaya.java
    Now Himalaya.class is created. Then this byte code is embedded into a HTML page which is as follows:

    <html>
    <head>
    <title>welcome 2 Himalaya Publication</title>
    <body>
    <applet code=Himalaya.class height=200 width=300>
    <PARAM NAME="string" VALUE="Trilochan">
    </applet>
    </body>
    </html>

    Output:

    C:\> appletviewer app1.html

    Let's see another program to display the numerical values:

    import java.awt.*;
    import java.applet.*;
    public class Numeric extends Applet
    {
    public void paint(Graphics g)
    {
    int x=15;
    int y=25;
    int z=x+y;
    String s="sum of x and y:"+String.valueOf(z);
    g.drawString(s,150,250);
    }
    }

    Now we compile this code as :

    C:\>javac Numeric.java
    Now Numeric.class created. Then this byte code will embed into a HTML page as:

    <html>
    <applet code=Numeric.class height=200 width=200>
    </applet>
    </html>

    Output

    Appletviewer app2.html

     

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment