Applet in java with example is to develop web application in java programming.
In this applet in java tutorial we will see applet in java, Applet class in java, Applet life cycle, and program for applet in java.
What is applet in java ?
Applet is a program which runs within Web browser.
Applets are not stand alone program, instead they can be able written within Web browser or applet viewer.
Applet in java definition : Applet is a special type of program that is embedded in the web page to generate the dynamic content.
It runs within web browser and works at client side.
The Appletviewer is provided by JDK.
All Applets are the sub class of applet class.
As we write programs in java , the applet doesn’t begin with main() method the execution of applet uses different structure.
There are different methods provided by AWT class in java such as drawstring() which is used to print string to output window using such methods we handle the output of our output.
There are also different methods provided to handle input also.
How to use applet in java ?
To use an applet , we specify one tag in HTML file.
Using HTML statement you can code your document.
Syntax to declare applet tag
Applet tag in java
<applet code=”filename” width=value height=value>
Applet tag in java contains attributes
code is file name
Width is width of applet
Height is height of applet
Applet class in java
The applet class provides necessary support for executing applets in Web browser such as starting and stopping applet.
It also provides various methods to handle applet such as to load images , display videos etc.
The applet class extends AWT class panel.
Panel extends container which extends component.
These classes provides support for Windows based GUI (graphical user interface) .
It provides all the methods to handle windows activities.
Applet Life cycle
Implementation and execution of applet goes through various states which is its life cycle.
The diagram shows the entire life cycle of the applet from start to end.
Whenever we create a applet using init() the applet life cycle starts this is applets first state which is initialization state. Next when we write start(), the applet actually starts execution. While execution if applet encounters interrupt then it stop its execution and move to idle state otherwise applet executed successfully and Applet destroyed.
Methods of Applet
The init() method is first method to be called in applet. In this method you can initialize variables.
This method is called only once in entire life cycle of applet.
We override the init() of applet class.
Public void init()
// initialization code
After init() method execution start() method is called.
When the first time applet is loaded start() is called.
It is also called to restart an applet it has been stopped, whereas init() is called once the first time and applet is loaded.
Start() stage can occurs several times over the life cycle.
To write the start code we override the start() method of applet class.
public void start()
3. Paint ()
This method is called each time your applets output must be redrawn.
This stage executed after start stage or whenever applet is changed.
This situation can occur for several reasons
The window in which applet is running may be overwritten by another change , in this case the paint stage is executed to redraw the changes.
The paint() is called while execution begins and whenever the applets must redrawn it’s output.
Every applet has paint method which is overriden to provides the display.
public void paint(Graphics g)
Paint () accepts one parameter which is type of graphics
Graphics class provides the methods to handle windows activities.
4. Stop ()
This method is called when the browser leaves the document containing applet.
Java excuses this method when applet is no longer visible.
While exciting the applet we clicked close button or minimise the window or switch to another window then stop() is called
If we again returned to applet then applets start() is called.
To handle stop activity stop() is overriden from applet class.
5. Destroy ()
The destroy method is called when the environment determines that your applet needs to be removed completely from memory.
At this method you should free up any resources the applet may be using.
Similar to init method the destroy() is called only once.
To write destroy method override destroy method from applet class.
The stop() is always called before destroy () method.
public void destroy()
Passing parameters to applet
To pass parameters we use param tag in applet tag
The applet tag in HTML document allows to pass arguments to applet using param tag
<applet code=”.class_file_name” height=value width= value>
<param name=var value=”val”>
To retrieve these arguments we use getParameter() method of applet class
Example to pass parameters to applet
<applet code=”A.class” height=500 width= 500>
<param name=str value=”parameter value”>
String msg= getParameter(str);
How to run applet program
There are two different way time run an applet
- Through Web browser
- Through Appletviewer
1. Through Web browser
To run an applet, we write one file with .HTML extension and in this file we write HTML tag along with the applet tag. And we can open this file by any java compatible web browser.
The html tag is given below
<applet code=”classfile.class” height=200 width=200>
In above Syntax, code us assigned the name of .class file of applet to be viewed and height and width of applet in pixel.
2. Through Appletviewer
Applet can run without the java compatible Web browser using Appletviewer tool which is part of JDK.
What is AppletViewer : It is tool in JDK.
Applet is windows application that us run from command line.
When Appletviewer appears, the applet appears in viewers main window. The command to view applet through the Appletviewer is as below.
First write java file including aplet tag in it.
Compile the code then run the file as given below.
Where c:\jdk1.2\bin> is path where appletviewer.exe is present and next to path we write file name which contains applet tag (which we want to execute)
We can write separate html file which contains applet tag
Or we can write applet tag in program itself.
Applet program in java
To accept a string using param tag and display it on applet
public class AppletDemo extends Applet
public void init()
public void paint(Graphics g)
/* <applet code=AppletDemo.class height=500 width=500>
<param name="str" value="Welcome to applet programming">
Steps to run applet
1. Save File with AppletDemo.java
2. Compile file javac AppletDemo.java
3. Run the file appletviewer AppletDemo.java