Pages

Applets in Java

Applets are a kind of program that Java supports which are used in the internet computing. These are the programs that are downloaded from a foreign machine (server) through a network and executed in a local machine (client's computer). Applets are not controlled by the local operating system. Applets are downloaded from the server and executed by the browsers in local computer. These programs cannot access the local resources like hard disk and cannot executed directly. It can only be the part of the HTML pages. Since applets are only executed by the browsers we can execute it separately while developing applets program by appletviewer provided in the JDK.
Applets program do not use main() method and System.out.println() method for executing and displaying outputs. It use only graphics methods for output and generate window-based output. Therefore, it need graphics support. It uses Java's Abstract Window Toolkit(AWT) for graphics output. Applets are defined in Applet class. Therefore we must extend Applet class to write applet program and include java.applet and java.awt packages.


Example of an applet.

import java.applet.*; //imports applet package
import java.awt.*; //imports AWT package for supporting Graphics

/*<applet code=AppletPro width = 250 height = 250></applet>*/

public AppletPro extends Applet{

    public static void paint(Graphics g){
        g.drawString("Let's start practicing Applet in Java", 20, 22); //display given string at specified co-ordinate
   }
}
}

To compile and execute the program, first compile the program using javac and execute using appletviewer.

Life Cycle of an Applet
Applets are created, executed, stopped and destroyed by appropriate methods provided in Applet class and we can override the available methods to fulfill our requirements. The methods available in Applet class are automatically called by AWT automatically. During the entire life cycle of an applet the following methods are used:

init()
This method is used to initialize the variables of the applet and is called first. This method is called only once during the life cycle of an applet.

  • create objects needed by the applet
  • set up initial values
  • load images or fonts
  • setup colors


start()
This method is called automatically after the init() method. It is also called when a user come to the page from different page. This method can be called repeatedly but the init() method is called only once.

paint()
This method is called automatically when after start() method. The user screen is drawn using this method. This method accepts an argument of type Graphics defined in AWT. It is called when the applet start executing.

stop()
This method is called automatically when a user leaves the page containing the applet. It is also called repeatedly during the life cycle of an applet.

destroy()
this method is called automatically when the user exit the applet program or browser end the activity. This method helps to release the resources used by the applet. The stop() method is called automatically befor the destroy() method.

Example of using all the above methods.

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

/*<applet cod=LifeCycleDemo width = 300 height = 300></applet>*/

class LifeCycleDemo extends Applet{
    String initMsg, startMsg, stopMsg, destroyMsg;

    public void init(){
        initMsg = "inside init method";
    }
    public void start(){
        startMsg = "inside start() method";
    }
    public void stop(){
        stopMsg = "inside stop() method";
    }
    public void destroy(){
       destroyMsg = "inside destroy() method";
   }
   public void paint(Graphics g){
      g.drawString("Demo of an Applet Life Cycle !", 10, 10);
      if(initMsg != null)
         g.drawString(initMsg, 10, 20);
     if(startMsg != null)
         g.drawString(startMsg, 10, 25);
    if(stopMsg != null)
        g.drawString(stopMsg, 10, 30);
    if(destroyMsg != null)
        g.drawString(destroyMsg, 10, 35);
  }
}

@msucil

Phasellus facilisis convallis metus, ut imperdiet augue auctor nec. Duis at velit id augue lobortis porta. Sed varius, enim accumsan aliquam tincidunt, tortor urna vulputate quam, eget finibus urna est in augue.

No comments:

Post a Comment