Open In App

Life Cycle of Java Applet

Last Updated : 11 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Note:
Java applet is deprecated because it’s no longer widely used on the web. The popularity of applets  has decreased over the years as browser support for applets has declined, and more advanced  technologies such as web-based applications and JavaScript have become more prevalent.  Additionally, applets are considered a security risk as they can execute arbitrary code on the  client machine, and many browsers now disable them by default. As a result, Java’s applet  technology is no longer seen as a valuable feature for Java developers and is removed from the  newer versions of Java.

  • 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

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:

  1. Using HTML file
  2. 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 plugin (java 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; 

  1. init()
  2. start()
  3. paint()
  4. stop()
  5. 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 applet鈥檚 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鈥檛 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鈥檛 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( )

Syntax: Entire Applet Life Cycle 

Java




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 command

Compilation:

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.java

Execution: 

c:\> appletviewer AppletDemo.java

So this was all about the Life Cycle of Java Applet and methods used to run the applet ! Hope this helps.

 



Like Article
Suggest improvement
Next
Java Applet Class
Share your thoughts in the comments

Similar Reads

Difference between a Java Application and a Java Applet
Bean life cycle in Java Spring
Draw a Polygon in Java Applet
Java Code for Moving Text | Applet | Thread
Draw a ellipse and a rectangle in Java Applet
Java Applet | Implementing Flood Fill algorithm
How to Install Java Applet Viewer in Windows?
Java Applet | Digital Stopwatch
Draw a Chessboard in Java Applet
Draw a Smiley in Java Applet

代做工资流水公司莆田工资银行流水查询衡阳代办流水账单包头打工作收入证明宿迁工资银行流水样本潍坊流水单打印武汉打签证流水济宁背调银行流水模板打印工资流水账单信阳工资流水单公司株洲打印个人银行流水合肥银行流水电子版报价南京制作车贷流水漳州流水账单制作包头打印个人工资流水菏泽收入证明样本德阳购房银行流水模板北京企业贷流水查询合肥办车贷工资流水惠州背调工资流水查询淮安对公流水模板宁波贷款工资流水 查询沧州企业对私流水模板阜阳贷款银行流水样本海口打印银行流水PS柳州办理工资流水app截图柳州工资流水模板潍坊银行流水PS模板菏泽离职证明模板厦门办入职工资流水曲靖办房贷银行流水香港通过《维护国家安全条例》两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”19岁小伙救下5人后溺亡 多方发声卫健委通报少年有偿捐血浆16次猝死汪小菲曝离婚始末何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言男子被猫抓伤后确诊“猫抓病”周杰伦一审败诉网易中国拥有亿元资产的家庭达13.3万户315晚会后胖东来又人满为患了高校汽车撞人致3死16伤 司机系学生张家界的山上“长”满了韩国人?张立群任西安交通大学校长手机成瘾是影响睡眠质量重要因素网友洛杉矶偶遇贾玲“重生之我在北大当嫡校长”单亲妈妈陷入热恋 14岁儿子报警倪萍分享减重40斤方法杨倩无缘巴黎奥运考生莫言也上北大硕士复试名单了许家印被限制高消费奥巴马现身唐宁街 黑色着装引猜测专访95后高颜值猪保姆男孩8年未见母亲被告知被遗忘七年后宇文玥被薅头发捞上岸郑州一火锅店爆改成麻辣烫店西双版纳热带植物园回应蜉蝣大爆发沉迷短剧的人就像掉进了杀猪盘当地回应沈阳致3死车祸车主疑毒驾开除党籍5年后 原水城县长再被查凯特王妃现身!外出购物视频曝光初中生遭15人围殴自卫刺伤3人判无罪事业单位女子向同事水杯投不明物质男子被流浪猫绊倒 投喂者赔24万外国人感慨凌晨的中国很安全路边卖淀粉肠阿姨主动出示声明书胖东来员工每周单休无小长假王树国卸任西安交大校长 师生送别小米汽车超级工厂正式揭幕黑马情侣提车了妈妈回应孩子在校撞护栏坠楼校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变老人退休金被冒领16年 金额超20万西藏招商引资投资者子女可当地高考特朗普无法缴纳4.54亿美元罚金浙江一高校内汽车冲撞行人 多人受伤

代做工资流水公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化