What is Applet and its Life Cycle methods, How to run Applet Program?

APPLET:

Generally, in java, we can develop two types of applications. They are-

  • Stand-alone Application
  • Distributed Application

A Standalone application is one which runs in the context of local disk. Every Standalone application contains main() method and System.out.println() statements. The result of Standalone application is not sharable.

A Distributed application is one which runs in the context of Browser or www or it can be accessed across the globe. Every Distributed application contains Life-cycle methods or Loop-back methods. The Result of each and every Distributed application is sharable.

  • Life-Cycle methods are those which are called by browser automatically, whenever we load the distributed application.
  • Sun Micro Systems initially developed a concept of APPLET for developing distributed application.
  • Since, applets are running in the context of Browser, it must have Life-cycle methods.
    Each and every method of java belongs to a class. Life-cycle methods of applet belong to a predefined class called Applet class.

    • Applet class belongs java.applet.* package

 

DEFINITION OF APPLET: An Applet is a java program which runs in the context of www or Browser.

LIFE-CYCLE METHODS OF LOOP-BACK METHODS:

In java, we have a class called java.applet.Applet. This class contains 4 Life-Cycle methods. They are:

  • public void init() {}
  • public void start() {}
  • public void stop() {}
  • public void destroy() {}

public void init() {}:-

  • This is the method which will be called by the Browser only one time when the applet is loaded in the Browser first time.
  • In this method we must write the block of statements which will obtain the “resources” such as opening the files, getting the database connection, Initialization of parameters.

public void start() {} :-

  • This method is called after completion of init () when we make very first request. From second request to further subsequent request only start () method will be called i.e. start () will be called each & every time as and when we make a request.
  • In this method we write the block of statement which provides business logic or they perform repeated operations.

public void stop() {} :-

  • This is the method which will be called by the browser in the following circumstances.
    • When the applet is stopped.
    • When the applet window is minimized.
  • In this method, we write the block of statement which will release the resources (files, database connection etc) temporary which we had obtained in init ().

public void destroy() {} :-

  • This is the method which will be called by the browser when we terminate the applet window or when we reload the applet.
  • In this method, we write the block of statement which are releasing the resources permanently which we had obtained in init ().

 

All the above four methods are defined with null body. As an applet programmer we must overwrite the above four methods depending upon our requirement.

 

public void paint(Graphics g) :-

  • This is method which is called by the browser after start () automatically. This method is used for displaying the data onto the browser or applet window.
  • Graphics: It is a class which belongs to a package called java.awt*.
  • An object of Graphihcs will be created by the browser with the help of JVM, when the control comes to paint ().
  • Graphics class contains the following method.
    • drawString (String, int , int)
    • drawRect (int, int, int,int)
    • drawCircle (……..)
    • drawLine (……….)
    • fillRect(……………)
    • fillCircle(………….)
  • public void drawString(String,int,int):The parameter string represents the result of the applet to be displayed on the browser at the specified x co-ordinate & y co-ordinate. First int parameter represents the x co-ordinate of the browser in terms of pixels. Second int parameter represents the y co-ordinate of the browser in terms of pixels.Example:drawString (“Hello Applet ”,100,100);

 STEPS OF DEVELOPING AN APPLET PROGRAM:

  • Import the appropriate package called java.applet.Applet.
  • Choose the user-defined class.
  • Every user defined class which is chosen in step2 must extend a predefined class called Applet class and its modifier must be public.
  • Overwrite the life cycle methods depending upon our requirement.
  • Save the applet program on that class name which extends the applet class with an extension “.java”.

HOW TO RUN THE APPLET PROGRAM:

In order to run the applet we have 2 ways. They are:

  • By writing HTML program.
  • By using appletviewer tool.

 

  1. By writing HTML program:- 

Since Applets come under the development of distributed applications & they must run in the browser. Browser always understands HTML programs only.

  • Syntax:

                                <html>

                                                <head>

                                                                <title>(……………)</title>

                                                </head>

                                                <body>

<applet code=”Name of the Applet Class” width= ”width of applet window” height=”height of Applet window”>

</applet>

                                                   </body>

                                                    </html>

  • <applet>: This is tag which is used for calling the applet program which is developed in java environment.
  • The attribute code is used to specify the class name which extends java.applet.Applet class.
  • The attribute width represents the width of the browser window in terms of pixels.
  • The attribute height represents the height of the browser window in terms of pixels.
  • </applet> tag represents the end of the applet tag.
  • Save the HTML file with any name but the extension must be either”.html” or “.htm”.

2. By using a appletViewer Tool:-

  • In some of the browsers, applets may not be executing. To execute applets in any situation, Sun Micro System has developed a tool called “appletviewer” which is present in “bin” directory of “jdk” software.
  • Weather we are running our applet program using appletviewer tool, our java application must contain the following applet tag:
  • <applet code=”Name of the Applet Class” width= ”width of applet window” height=”height of Applet window”></applet>
  • When we use the above applet tag as a part of our java application, it must be present as a commented statement.

 

import java.applet.Applet;
import java.awt.*;
public class AppletLifeCycleDemo extends Applet
{
String s =””;
public void init()
{
setBackground(Color.red);
setForeground(Color.yellow);
Font f = new Font(“Arial”,Font.Bold,24);
setFont(f);
s=s+” int ()”;
}
public void start()
{
s=s+”start()”;
}
public void stop()
{
s=s+”stop()”;
}
public void destroy()
{
s=s+”destroy()”;
}
public void paint(Graphics g)
{
g.drawString(s,10,10);
}
}

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *