APPLET PROGRAMMING

APPLET PROGRAMMING:-

Applet:-

 Ø Applet is the program that comes from the server, downloaded on the client computer and executed on the client computer.
 Ø Applet is a web based program.
 Ø Applet does not have main method.
 Ø In Applet we have to write applet tag as shown below


 Ø Here code, width and height are known as attributes.
 Ø Here the attribute will give some extra to the tag.
 Ø To execute Applet there are two ways. 
          They are:
1.     In Command prompt using appletviewer command
2.     Using web browser

 Ø In java.applet package there is only class known as Applet.
 Ø In java.awt package there are some set of classes to create GUI components.

Life Cycle of Applet:-

 Ø Life cycle consists of different stages like:
1.     init
2.     start
3.     stop
4.     destroy

NOTE:-
 Ø Before destroy () method, stop () method is executed.

PROGRAM:-

import java.applet.*;
import java.awt.*;
public class Appletdemo extends Applet
{
  public void paint(Graphics g)
  {
     g.drawString("Applet Programming",100,150);
  }
}
/*
*/

File Name: -    Appletdemo.java    

Commands:-
                       javac    Appletdemo.java
  appletviewer Appletdemo.java    

OUTPUT:-    
Applet is displayed with Applet Programming output.

NOTE:-

 Ø To see the output for the applet in the browser we have to write applet tag in separate file without comments and save that file with .html extension.

PROGRAM:-


File Name: -    Appletdemo.html    

OUTPUT:-    
Double click the html thenApplet is displayed with Applet Programming output.

PROGRAM:- To display some message in status bar

import java.applet.*;
import java.awt.*;
public class Appletdemo1 extends Applet
{
  public void paint(Graphics g)
  {
     showStatus("Information in status bar");
  }
}
/*
*/

File Name: -    Appletdemo1.java    

Commands:-
                       javac    Appletdemo1.java
  appletviewer Appletdemo1.java    

OUTPUT:-    
Applet is displayed with Information in status bar output in status bar

Passing Parameters to Applet:-

 Ø To pass parameters to applet we have to use param tag as shown below



PROGRAM:- To pass parameters to applet.

import java.applet.*;
import java.awt.*;
public class Appletdemo2 extends Applet
{
   public void paint(Graphics g)
   {
     String str=getParameter("STR");
     int x=Integer.parseInt(getParameter("X"));
     int y=Integer.parseInt(getParameter("Y"));
     g.drawString(str,x,y);
   }
}
/*
*/

File Name: -    Appletdemo2.java    

Commands:-
                       javac    Appletdemo2.java
  appletviewer Appletdemo2.java    

OUTPUT:-    
                      Applet is displayed with hello applets output

PROGRAM:- To show the life cycle of applet

import java.applet.*;
public class Appletdemo3 extends Applet
{
   public void init()
   {
     System.out.println("In init method");
   }
   public void start()
   {
      System.out.println("In start method");
   }
   public void stop()
   {
      System.out.println("In stop method");
   }
   public void destroy()
  {
     System.out.println("In destroy method");
  }
}
/*
*/

File Name: -    Appletdemo3.java    

Commands:-
                        javac    Appletdemo3.java
   appletviewer Appletdemo3.java    

OUTPUT:-    
                       In init method
                       In start method
                       In stop method
                       In destroy method
RESULT:-
                     The output is displayed in command prompt.