0% found this document useful (0 votes)
222 views6 pages

Applet Life Cycle in Java

An applet in Java is a program embedded in a web page to generate dynamic content. It extends the Applet class. The applet lifecycle involves five core methods - init(), start(), stop(), paint(), and destroy() - that are automatically called to create, initialize, end, and destroy the applet as it runs. There are two types of applets: local applets that are stored locally, and remote applets stored on remote computers accessed via URLs.

Uploaded by

msukumarbtech
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
222 views6 pages

Applet Life Cycle in Java

An applet in Java is a program embedded in a web page to generate dynamic content. It extends the Applet class. The applet lifecycle involves five core methods - init(), start(), stop(), paint(), and destroy() - that are automatically called to create, initialize, end, and destroy the applet as it runs. There are two types of applets: local applets that are stored locally, and remote applets stored on remote computers accessed via URLs.

Uploaded by

msukumarbtech
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

What is the Applet Life Cycle in Java?

An applet in java is a special type of program which is embedded into the web page in order
to generate dynamic content. It is also a class in java that extends
the java.applet.Applet class. Applet life cycle is a process of how an object in java is created,
initiated, ended, and destroyed during its entire cycle of execution in an application. It has
five core methods which are, init(), start(), stop(), paint() and destroy(). In java, their
methods are invoked for execution. Apart from the browser, applet also works on the client
side thereby having less process time.

What are the Types of Applets in Java?

There are two types of applet in java:

1. Local applet
2. Remote applet

Local Applet

It is written on its own and then embedded into the web pages. It is developed locally and
is stored in the local system/machine. In the local applet, a web page doesn’t get the
information from the internet, instead, it is specified by the local pathname or filename. In
java applet, there are two attributes specifically used in defining an applet. The first
attribute is the codebase which specifies the path name and the second attribute is the
code that defines the file name that contains the applet code.
Remote Applet

It is developed and designed by developer another developer and not the java team. It
resides on a remote computer that is connected to the internet. For running the applet
which is stored in remote computer, the system must be connected to the internet for able
to download and run the applet program. The remote applet can be loaded only if the
address of applet is known on the web which is also known as URL (Uniform Resource
Locator).

How Do Applet Life-Cycle Methods Work in Java?


The plugin software in java is responsible for managing the life cycle of the applet. It is
executed in any browser and works on the client side of an application. Because it runs on
the browser, there is no main() method involved. Thus it is created for HTML pages. The
methods, init(), start(), stop() and destroy() belongs to the class, applet.Applet The
method paint() belongs to the class awt.Component.

For making a normal class in java into applet class, the Applet needs to be extended in order
to be used.
Whenever an applet class is created, an instance of it gets created thereby allowing us to
use all the methods of the class. In applet, there is no need of calling a method explicitly,
these are automatically invoked by the browser.

Method 1: init()
Syntax

public void init()


{
// To initialize objects
}

• This is the first method that is being called


• All the variables are initialized here
• This method is only called once during the run time
• It is generally invoked during the time of initialization

Method 2: start()
Syntax

public void start()


{
// To start the applet code
}

• It is called after the init method


• Used for starting an applet
• It is also used for restarting an applet in case it is stopped

Method 3: paint()
Syntax

public void paint(Graphics graphics)


{
// Any shape's code
}

• This method is used for painting various shapes like squares, rectangles, etc.
• It has parameter of type graphic class, which enable the feature of painting in the
applet.
• The graphics class parameter contains graphics context which is used for displaying
the output of the applet.

Method 4: stop()
Syntax

public void stop()


{
// To stop the applet code
}

• This method is invoked whenever the browser is stopped, minimized, or because of


abrupt failure within an application.
• Generally after the stop method, the start method can be used.
• It deals with the cleaning of the code, the method is called when the browser leaves
the HTML document when the applet is running.

Method 5: destroy()
Syntax

public void destroy()


{
// To destroy the applet
}

• Once we are done with the applet work, this method destroys the application and is
invoked only once.
• Once the applet is destroyed, it can’t be restored.
• It is called when the environment determines that the applet needs to be completely
removed from the memory.

Examples of an Applet Life Cycle in Java


Example 1
A simple code for understanding the implementation of applet in java.
import java.awt.*;
import java.awt.applet.*;

// This is the class definition for AppletDemo, which extends Applet


public class AppletDemo extends Applet {

// This method sets the background and foreground colors of the


applet
public void init() {
setBackground(Color.black); // Set the background color to
black
setForeground(Color.yellow); // Set the foreground color
to yellow
}

// This method draws the "Welcome" message on the applet


public void paint(Graphics g) {
g.drawString("Welcome", 100, 100); // Draw the string
"Welcome" at coordinates (100, 100)
}
}

Save the given file as AppletDemo.java in local machine

<html>
<applet code = AppletDemo
width = 400
height = 500>
</applet>
</html>

Save the given file as Applet.html

For compilation and execution of the program, use the commands given below: javac
AppletDemo.java appletviewer AppletDemo.java

Explanation: As soon as the program is executed, a window pops up with the background
color black. On it, the void paint method is executed, drawing a string "welcome" on the
window.

Example 2
Program for understanding java applet life cycle.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.*;

/*<applet code="AppletLifeCycle.class" width="350" height="150">


</applet>*/

public class AppletLifeCycle extends Applet {

// This method is called when the applet is initialized


public void init() {
setBackground(Color.BLUE); // Set the background color to
blue
System.out.println("init() is invoked"); // Print a
message to the console
}

// This method is called when the applet is started


public void start() {
System.out.println("Start() is invoked"); // Print a
message to the console
}

// This method is called when the applet needs to be painted


public void paint(Graphics g) {
System.out.println("Paint() is invoked"); // Print a
message to the console
}

// This method is called when the applet is stopped


public void stop() {
System.out.println("Stop() is invoked"); // Print a
message to the console
}

// This method is called when the applet is destroyed


public void destroy() {
System.out.println("Destroy() is invoked"); // Print a
message to the console
}
}

Explanation: As soon as the program is executed, the window pops up and instance of each
applet class is created. As a result of this, the method init() is called which displays
command in the window. After this, the start method is invoked, and the message is
displayed followed by paint() method. Now when the user clicks on the minimize button
on window, the stop() method is triggered and for restoring the window, start() method is
invoked again. When the window is closed, the destroy() method is invoked, closing the
window.

Conclusion
Here are a few key takeaways from this blog.

• An applet in java is a special type of program which is embedded into the web page
in order to generate dynamic content. It is also a class in java.
• There are two types of applet in java: 1. Local applet 2. Remote applet
• The plugin software in java is responsible for managing the life cycle of the applet. It
is executed in any browser and work on the client-side of an application.

You might also like