Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Exception Handling

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

What is Applet?
Java Applet vs Application
Creating a Applet
Displaying Applet on Webpage
Applet Lifecycle
Applet Tag
Passing Parameters to Applet
Setting Color to applet
Update and Repaint method
Using Fonts in Applet

Graphics Programming

Intro. to Graphics class
Drawing - Lines , Rectangles
Drawing - Circles and Ellipse
Drawing - Arcs
Drawing - Polygons
Drawing - Bar Charts

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Applet Lifecycle


When applet is started, it goes from different states from start to end called as lifecycle of applet.

Applet States

Basically, Applet has 4 states as

  • Born
  • Running
  • Idle
  • Dead

Methods of Applet

In order to go from one state to another state, java has following methods

  • init ()
  • start ()
  • paint ()
  • stop ()
  • destroy ()

These methods are called based on the state.

Methods called at begin

  • init ()
  • start ()
  • paint ()

Methods called when applet terminates

  • stop ()
  • destroy ()

Lifecycle diagram

Applet Life Cycle

1. Initialization State

When applet is first loaded, the applet enters in initialization state by calling init () method of applet class.

In this state applet does following tasks

  • Creating objects of applet.
  • Setting the initial values.
  • Setting up the colors,fonts,styles etc.
NOTE - Initialization of an applet occurs only once in the applet's lifecycle.

init () method

public void init ()
{
    // creating objects
    // setting values
    // setting colors,fonts
    ...
}

2. Running state

When java calls start () method of an applet class, the applet enters into running state.

This method is automatically called after the initialization of an object or from idle state.

NOTE - start () method may be called more than once

start () method

public void start()
{
    // assigning values
    // starting threads
    ...
}

3. Idle or Stopped state

When applet is stopped from running state, it comes into idle state. It can be done by calling stop () method by user.

stop () method

public void stop ()
{
    // deallocating memory
    // closing connections
    ...
}

4. Dead state

When applet is removed from memory then we can say applet is dead.

This is done by calling destory () method by java automatically.

This method is also called when we close the applet window from Right corner.

NOTE - Destroy state occurs only once in the lifetime of applet.

destory () method

public void destory ()
{
    // clean up resources
    // closing files
    ...
}

5. Display State

When an applet performs some output operations, it moves to the display state.

Java automatically calls the paint () method when applet enters into the running state.

The paint() method has only one parameter of type Graphics which contains the graphics related methods.

NOTE - paint () method is called everytime whenever applet's output is redrawn into the applet's window.

Display state is not considered as part of applet's lifecycle because of paint() method which belongs from applet class.

paint () method

public void paint (Graphics g)
{
    // drawing shapes
    // displaying images
    // handling events
    ...
}

Applet Life cycle Example

Following example demonstrates the all methods of applet life cycle.

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

public class AppletProgram extends Applet
{
    String status;
    public void init()
    {
	status = "Applet Initialized";
    }
    public void start()
    {
	status = "Applet Started";
    }
    public void paint(Graphics g)
    {
	g.drawString(status, 15, 140);
    }
    public void stop()
    {
	status = "Applet Stopped";
    }
    public void destroy()
    {
	status = "Applet Destroyed";
    }
}
/* < applet code="AppletProgram.class" width=500 height=500> </applet> */

Compile the above program usign javac AppletProgram.java

Run the applet using appletviewer AppletProgram.class

Output

Java Applet Example
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo