6 Aug 2012

applet in java with examples

An Applet is a java program that can be included in an HTML page which always runs in the context of browser such as mozilla and internet explorer, etc. Applet is designed to run remotely on the client browser, so there are some restrictions on it. Applet can't access system resources on the local computer.
In order to develop any distributed applications using applets concept,we must use a predefined class called java.applet.Applet.
All the life cycle methods of applet concept are present in a predefined class called java.applet.Applet.
This class contains four life cycle methods .They are
1. public void init()
2. public void start()
3. public void stop()
4. public void destroy()
1. public void init(): This is one of the predefined method available in Applet class.This method will be called by browser only once when an applet is loaded in the browser window.
In this method we write the block of statements which will perform only one time operations such as obtaining database connection, initializing parameters, etc.
2. public void start(): This is one of the predefined method available in Applet class.After executing init() first time, the start() is called immediately by the browser window. From the second request to further subsequent request start() only will be called bt the browser. 
In this method , we write the block of statements which will perform repeated operations such as reading records from the files, etc. In general, start() always contains business logic.
3. public void stop(): This is one of the predefined method available in Applet class. This method will be called by browser automatically, when we minimize the applet window or applet-> stop.
.4. public void destroy(): This is one of the predefined method available in Applet class. This method will be called by browser automatically, when we terminate the applet window.
All life cycle methods are defined in java.applet.Applet class with null body. In order to develop our own applet applications, we must override the required life cycle methods of applet class into our class by inheriting from Applet class and that class modifier must be public.
ex:
import java.applet.Applet;
public class myapplet extends Applet
{
 //override required life cycle methods of Applet class.
}
In order to display the result of an applet we use the following method 
public void paint(Graphics)
{
}
paint() is the predefined method available in Applet class and it is defined with null body. This method will be called by the browser automatically after executing start() since the result is ready. paint() is taking Graphics class object as a parameter and this class present in java.awt package. Graphics class contains the following instance method which is used in the body of paint() for displaying the result of an applet.
public void drawString(String,int,int).
Here, the String parameter represents result of an applet.
first int parameter represents x-coordinate of the browser. second int parameter represents y-coordinate of the browser. Finally the result of an applet must be displaying in the browser window at the intersection point of x and y coordinates.
ex:


import java.applet.Applet;
import java.awt.Graphics;
public class myapplet extends Applet
{
 //override required life cycle methods of Applet class.
 public void paint(Graphics g)
 {
  g.drawString("Hello Applet",100,100);
 }

}
In order to to run applet programs in the browser Sun micro system has developed a tag
<applet code="name of the class which extends Applet class" height=" x-coordinate of the browser" width="y-coordinate of the browser">
</applet>
ex:

<applet code="myapplet" height="100" width="100">
</applet>
The above tag must be written in the <body> of HTML program or in the java program within the comments(/* ....*/ or //).

ex:

/* <applet code="myapplet" height="100" width="100">
    </applet> */

Running Applet program:

C:\javac myapplet.java
C:\appletviewer myapplet.java



0 comments:

Post a Comment