![]() |
![]() |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Intro. to Graphics class | |
Drawing - Lines , Rectangles | |
Drawing - Circles and Ellipse | |
Drawing - Arcs | |
Drawing - Polygons | |
Drawing - Bar Charts |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
When applet is started, it goes from different states from start to end called as lifecycle of applet.
Basically, Applet has 4 states as
In order to go from one state to another state, java has following methods
These methods are called based on the state.
Methods called at begin
Methods called when applet terminates
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
NOTE - Initialization of an applet occurs only once in the applet's lifecycle. |
public void init () { // creating objects // setting values // setting colors,fonts ... }
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 |
public void start() { // assigning values // starting threads ... }
When applet is stopped from running state, it comes into idle state. It can be done by calling stop () method by user.
public void stop () { // deallocating memory // closing connections ... }
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. |
public void destory () { // clean up resources // closing files ... }
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.
public void paint (Graphics g) { // drawing shapes // displaying images // handling events ... }
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