Saturday 31 August 2019

Java Applet Life Cycle Program | Java program to demonstrate applet life cycle | Example

                 In this post, we will see Java Applet Life Cycle Program | Java program to demonstrate applet life cycle | Example 


Find details of Life Cycle of Applet in this link: https://www.comrevo.com/2018/10/life-cycle-of-applet.html.

Explanation: 
                  Applet program does not have main() method. Hence, it can not be used for creating stand-alone application. It is to be used with other applications like webpages created by JSP, Servlet.
                  We need to extend class Applet given by package java.applet.
                  We can override init(), start(), stop(), destroy() methods given by Applet class. We may also need paint() method to redraw applet. paint() method is given by Component class which is given by java.awt package. paint() method accepts Graphics class object as parameter.

                  Go through the following programs:

Program (AppletLifeCycle.java)

import java.applet.*;
import java.awt.*;

public class AppletLifeCycle extends Applet
{
  public void init()
  {
    System.out.println("Applet is initialized");
  }
  public void start()
  {
    System.out.println("Applet is started");
  }
  public void stop()
  {
    System.out.println("Applet execution is stopped");
  }
  public void paint(Graphics g)
  {
    System.out.println("Painting the Applet");
  }
  public void destroy()
  {
    System.out.println("Applet is Destroyed");
  }
}

Program (AppletLifeCycle.html)
<html>
<head><title>Applet Life Cycle</title></head>
<body>
<applet code = "AppletLifeCycle.class" width = "300" height = "300">
If your browser is Java enabled, then only you can see Applet in your browser. If it is not Java enabled,then you need to add Java plug-ins in your browser or use appletviewer to view applet.
</applet>
</body>  
</html>
  


 Output:
parag@parag-Inspiron-N4010:~/Desktop/programs/applet$ javac AppletLifeCycle.java
parag@parag-Inspiron-N4010:~/Desktop/programs/applet$ appletviewer AppletLifeCycle.html
Applet is initialized
Applet is started
Painting the Applet
Applet execution is stopped
Applet is started
Painting the Applet
Applet execution is stopped
Applet is started
Painting the Applet
Applet execution is stopped
Applet is Destroyed

   

Explanation:
                  If you look at the output in terminal, you can find init() method is called only once. After init() method start() and paint() methods are called.
                   If applet is minimized, then stop() method is called. 
                   If you reopen it, start() and paint() methods are called.
                   If you close the applet, destroy() method is called.

Find Life Cycle of Applet in following link:
https://www.comrevo.com/2018/10/life-cycle-of-applet.html .

Find Applet Hello World Program Example in following link:
https://www.comrevo.com/2019/08/Java-Applet-Hello-World-Example-Java-Applet-Hello-World-Program.html .

No comments:

Post a Comment