Life Cycle of Java Applet
An applet is a Java program that can be embedded into a web page. It runs inside the web browser and works on the client-side. An applet is embedded in an HTML page using the APPLET or OBJECT tag and hosted on a web server. The entire life cycle of an applet is managed by the Applet Container. All applets are sub-classes (either directly or indirectly) of java.applet.Applet class. Applets are not stand-alone programs. They run either within a web browser or an applet viewer.
- Applets generate Dynamic content
- Applets work on the client-side
- The response time is fast
We can view our Applet with the help of a standard applet viewer tool called Applet Viewer. Unlike the general executions and outputs of the java programs, applet execution does not begin at main() method, and the output of an applet window is not catered by System.out.println(). Rather it is handled with various Abstract Window Toolkit (AWT) methods, such as drawString().
Let us do see a hierarchy of Applet before landing up on stages in the lifecycle of the java applet that is as follows in the below media:
Stages in the Life Cycle of Java Applet
- Initializing an Applet
- Starting the Applet
- Painting the Applet
- Stopping the Applet
- Destroying the Applet
Note: In order to implement the Applet we need to import awt package :
java.awt.applet.*;
Life Cycle of Applet
Step 1: Initialization
public void init()
There is no main method unlike our normal java programs. Every Applet will start it’s execution from init() method. It is executed only once
Step 2: Start
public void start()
After init() method start() method is invoked. Executed when the browser is maximized
ote>Step 3: Paint
public void paint (Graphics g)Paint method is used to display the content on the applet. We can create the objects or components to the applet or we can directly write a message on the applet. It will take Graphics class as a parameter.
Step 4: Stop
public void stop()stop() method is used to stop the applet. It is executed when the browser is minimized.
Step 5: Destroy
public void destroy()destroy() method is used to completely close the applet. It is executed when the applet is closed.
Implementation:
Implementation of java Applet can be done in two ways as follows:
- Using HTML file
- Applet viewer tool
Way 1: Using HTML file
HTML
<
HTML
>
<
applet
>
code,width,height
</
applet
>
</
HTML
>
Note: Drawbacks of using HTML file is you need a plugi
ava plugin) to run it on your browser.Way 2: Applet viewer tool
Methods of Applet Life Cycle:
There are five methods of an Applet Life Cycle namely;
- init()
- start()
- paint()
- stop()
- destroy()
All these are available in AWT Package java.awt.applet.* and in order ton import paint (Graphics g) we do use java.awt.component package
Let’s understand each method in a detailed manner :
Method 1: init()
- This is the first method to be called
- Variables can be initialized here
- This method can be called only once during the run time of the applet
- It is invoked at the time of Initialization
Syntax:
public void init() { // To initialize objects }Method 2: start()
- This method is called after init() method
- start() method is used for starting the applet
- It is also called to restart an applet after it has been stopped. i.e. to resume the applet
Syntax:
public void start() { // To start the applet code }Note: init() is called once i.e. when the first time an applet is loaded whereas start( ) is called each time an apple
s HTML document is displayed onscreen.Method 3: paint()
void paint(Graphics g){ }
- paint() method is used for painting any shapes like square, rectangle, trapeziums, etc.
- paint() method has one parameter of type Graphics Class, this Graphics class enables the painting features in an applet.
- This parameter will contain the graphics context, which is used whenever output for the applet is required.
Syntax:
public void paint(Graphics graphics) { // Any shape's code }Note: This is the only method among all the method mention above, which is parameterized.
Method 4: stop()
- It is invoked every time the browser is stopped, minimized or when there is an abrupt failure in the application.
- After stop()method called, we can also use start() method whenever we want.
- This method mainly deals with clean up code.
- The stop( ) method is called when a web browser leaves the HTML document containing the applet when it goes to another page, for example, when stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.
Syntax:
public void stop() { // To stop the applet code }Method 5: destroy()
- destroy() method is used to destroy the application once we are done with our applet work. It can be invoked only once.
- Once applet is destroyed we can’t start() the applet (we cannot restore the applet again)
- The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory.
Syntax:
public void destroy() { // To destroy the applet }Note: The stop( ) method is always called before destroy( )
Synta
strong> Entire Applet Life CycleJava
Class AppletLifeCycle
extends
Applet
{
public
void
init()
{
// Initializes objects
}
public
void
start()
{
// Starts the applet code
}
public
void
paint(Graphics graphics)
{
// Any shape's code
}
public
void
stop()
{
// Stops the applet code
}
public
void
destroy()
{
// Destroys the applet code
}
}
Implementation:Example 1: In order to begin with Java Applet, let’s understand a simple code to make the Applet
Java
// Java Program to Make An Applet
// Importing required classes from packages
import
java.awt.*;
import
java.awt.applet.*;
// Class 1
// Helper class extending Applet class
public
class
AppletDemo
extends
Applet
// Note: Every class used here is a derived class of applet,
// Hence we use extends keyword Every applet is public
{
public
void
init()
{
setBackground(Color.black);
setForeground(Color.yellow);
}
public
void
paint(Graphics g)
{
g.drawString(
"Welcome"
,
100
,
100
);
}
}
// Save file as AppletDemo.java in local machine
HTML
<
html
>
<
applet
code
=
AppletDemo
width
=
400
height
=
500
>
</
applet
>
</
html
>
<!-- Save as Applet.html -->
Compilation methods:
Now in order to generate output, do follow below undersigned to compile and run the above file:
Method 1: Using command
Method 2: Include the applet code in our java program.
Methods are as follows:
Method 1: Using the co
dCompilation:
c:> javac.AppletDemo.java
Execution:
Double click on Applet.html This won't work on browser as we don't have the proper plugins.Method 2: Include the applet code in our java program make sure to put this html applet code as comments as it is important evil as demonstrated below as follows:
Example
Java
// Java Program to Illustrate Insertion of HTML File in
// Applet As Commands
// Importing required classes
import
java.applet.*;
import
java.awt.*;
// Note: Insertion of HTM:L file as comments
/* <applet code = AppletDemo width=400 height=500>
</applet>*/
// Java Program
// Class extending Applet
public
class
AppletDemo
extends
Applet {
public
void
init()
{
setBackground(Color.black);
setForeground(Color.yellow);
}
public
void
paint(Graphics g)
{
g.drawString(
"Welcome to Applets"
,
50
,
50
);
}
}
Compilation:c:\> javac AppletDemo.javaExecution:
c:\> appletviewer AppletDemo.javaSo this was all about the Life Cycle of Java Applet and methods used to run the applet ! Hope this helps.