Program to display life cycle of an Applet.

This program demonstartes the life cycle methods of an Applet. During execution of an applet it goes through different stages these are init( ), start( ), paint( ), stop( ) and destroy( ). When you run this program it will call init( ), start( ) and paint( ) method as shown in output. The remaining two methods stop( ) and destroy( ) are called when applet stops or when it is destroyed respectively.


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

public class AppletLifeCycleDemo extends Applet {
 
 String msg = "";
 
 public void init() {
  
  msg += "init( ) -> ";
 }
 
 public void start() {
  
  msg += "start( ) -> ";
 }
 
 public void paint(Graphics g) {
  
  msg += "paint( )";
  g.drawString(msg, 20, 20);
 }
 
 public void stop() {
  
  msg += " -> stop( )";
 }
 
 public void destroy() {
  
  msg += "-> destroy( )";
 }
}

/*
<APPLET CODE="AppletLifeCycleDemo.class" HEIGHT="200" WIDTH="200" >
</APPLET>
*/
OUTPUT
C:\>javac AppletLifeCycleDemo.java 
C:\>appletviewer AppletLifeCycleDemo.java



Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative