Applet Life Cycle: Applet runs in the browser and its lifecycle-methods are called by JVM at its birth, its death and when it is running or idle. When an applet is made to work, it undergoes a series of changes as discussed below. Every Applet can be said to be in any of the following states
1. New Born state
2. Running state
3. Idle state
4. Dead or Destroyed state
New Born State
· An apllet enters into initialization state when the applet is first loaded into the browser by calling the init() method.
· The init() method is called only one time in the life cycle on an applet.
· The init() method retrieves the parameters through the PARAM tag of html file.
· At this stage, we may create Objects needed by applets, setup initial values, load images or fonts and setup colors.
· After the initialization of the init() method user can interact with the Applet.
· We can override this method.
· Syntax:
public void init()
{
Statements
}
Running State:
· After initialization, this state will automatically occur by invoking the start method of applet.
· The running state can be achieved from idle state when the applet is reloaded.
· This method may be called multiple times when the Applet needs to be started or restarted.
· If the user leaves the applet and returns back, the applet may restart its running.
· We can override this method.
· Syntax:
public void start()
{
Statements
}
Idle State:
· The idle state will make the execution of the applet to be halted temporarily.
· Applet moves to this state when the currently executed applet is minimized or when the user switches over to another page. At this point the stop method is invoked.
· From the idle state the applet can move to the running state.
· The stop() method can be called multiple times in the life cycle of applet.
· We can override this method.
· Syntax:
public void stop()
{
Statements
}
Dead State:
· When the applet programs terminate, the destroy function is invoked which brings an applet to its dead state.
· The destroy() method is called only one time in the life cycle of Applet like init() method.
· In this state, the applet is removed from memory.
· We can override this to cleanup resources.
· Syntax
public void destroy()
{
Statements
}
Display State:
· The applet is said to be in display state when the paint or update method is called.
· This method can be used when we want to display output in the screen.
· This method can be called any number of times.
· Overriding paint() method is a must when we want to draw something on the applet window.
· paint() method takes Graphics object as argument
· paint() method is automatically called each time the applet window is redrawn i.e. when it is maximized from minimized state or resized or when other windows uncover overlapped portions.
· It can also be invoked by calling “repaint()” method.
· Syntax:
public void paint(Graphics g)
{
Statements
}
0 comments:
Post a Comment