Applet Life Cycle in Java
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.
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).
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
Method 2: start()
Syntax
Method 3: paint()
Syntax
• 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
Method 5: destroy()
Syntax
• 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.
<html>
<applet code = AppletDemo
width = 400
height = 500>
</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.*;
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.