Monday, 17 February 2014

INITIALIZATION AND TERMINATION OF APPLET IN JAVA

APPLET INITIALIZATION AND TERMINATION

When an applet begins, sequence of methods are:
1. init( )
2. start( )
3. paint( )
When an applet is terminated, the sequence is:
1. stop( )
2. destroy( )

init( ) :

  • The init( ) method is the first method to be called.
  •  Initializing the variables.
  •  This method is called only once during the run time of the applet.



start( ) :

  • Called after init( ).
  • Also called to restart an applet after it has been stopped.
  • Called each time an applet’s HTML document is displayed onscreen

paint( ) :

  • Called each time an applet’s output must be redrawn.
  • Also called when the applet begins execution.
  • The method has one parameter of type Graphics.
  • This parameter will contain the graphics context, which describes the graphics environment in which the applet is running.

stop( ) :

  • Called when a web browser leaves the HTML document containing the applet.
  • Used to suspend threads that don’t need to run when the applet is not visible.
  • We can restart them when start( ) is called.

destroy( ) :

  • called when the environment determines that the applet needs to be removed completely from memory.
  • The stop( ) method is always called before destroy( ).

Overriding update( ) :


  • This method is called when the applet has requested that a portion of its window be redrawn.
  • The default version of update( ) simply calls paint( ).
  • We can override the update( ) method.
Example for nested try Statements:


// Use Parameters
import java.awt.*;
import java.applet.*;
/*
<applet code="ParamDemo" width=300 height=80>
<param name=fontName value=Courier>
<param name=fontSize value=14>
<param name=leading value=2>
<param name=accountEnabled value=true>
</applet>
*/
public class ParamDemo extends Applet{
String fontName;
int fontSize;
float leading;
boolean active;
// Initialize the string to be displayed.
public void start() {
String param;
if(fontName == null)
fontName = "Not Found";
  param = getParameter("fontSize");
try {
      if(param != null) // if not found
fontSize = Integer.parseInt(param);
      else
fontSize = 0; 
} catch(NumberFormatException e) {
fontSize = -1;
}
param = getParameter("leading");
try {
if(param != null) // if not found
          leading =
   Float.valueOf(param).floatValue();
else
leading = 0;
} catch(NumberFormatException e) {
leading = -1;
}
param = getParameter("accountEnabled");
if(param != null)
    active = Boolean.valueOf(param).booleanValue();
}
// Display parameters.
public void paint(Graphics g) {
g.drawString("Font name: " + fontName, 0, 10);
g.drawString("Font size: " + fontSize, 0, 26);
g.drawString("Leading: " + leading, 0, 42);
g.drawString("Account Active: " + active, 0, 58);
}
}


Output:













No comments :

Post a comment