The best programs are written so that computing machines can perform them quickly and so that human beings can understand them clearly. A programmer is ideally an essayist who works with traditional aesthetic and literary forms as
well as mathematical concepts, to communicate the way that an algorithm works and to convince a reader that the results will be correct. Donald E. Knuth

Applets

An applet is Java program which is embedded in a web page and runs in a browser on the client side. A JVM is required either as a browser plug-in or separate runtime environment to execute the applet. Let's create a very basic applet. See the program below :



import java.applet.*;
import java.awt.*;
public class BasicAppletDemo extends Applet {
   public void paint(Graphics g) {
      g.drawString("This is my first applet", 100, 100);
   }
}

In the above program, we extended the Applet class to create an applet. It simply prints a message on the applet screen at the coordinate ( 100, 100 ). The drawString( ) method of java.awt.Component class is used to draw the string ( message ). This method is invoked inside the paint( ) method. The paint( ) method is present in every applet program to draw or present anything on the applet screen. Please note that applets do not define any
main( ) method.

How to execute an applet ?
We can see the applet in action using two methods :
1 ) By using AppletViewer tool ( used for testing the applet )
2 ) By using Html file ( applet is embedded in the file and executed on the browser )

AppletViewer comes with JDK and is plugged in as an utility if you are using an IDE.
Let us write an html file to execute the applet on the browser.

<!DOCTYPE html>
<html>
<title>My First Applet</title>
<body>
<applet code="BasicAppletDemo.class" width="500" height="500">
</applet>
</body>
</html>


Life Cycle of an Applet
The Applet goes through following states during its life-cycle :
Initialized : The init( ) method is called to initialize the applet. It is called automatically after processing the param tag in html file.
Started : The start( ) method is called automatically after the init( ) method.
Painted : The paint( ) method is called after the start( ) method.
Stopped : The stop( ) method is called automatically when the user moves to another page.
Destroyed : The destroy( ) method is called automatically when the browser exits.

Displaying image in Applet
Following program illustrates how to display an image in an Applet.

import java.applet.*;
import java.awt.*;
import java.net.*;
public class DisplayImageDemo extends Applet {
   Image pic;
   public void init() {
      String imageURL = "Penguins.jpg"; // path to the image
      // get the URL of the document in which this applet is embedded
      URL docURL = getDocumentBase();
      pic = getImage(docURL, imageURL);
   }
   public void paint(Graphics g) {
      g.drawImage(pic, 0, 0, 400, 400, this);
   }
}

If you are running this program on your system, make sure that imageURL contains appropriate path to any image stored on your system.

Animation in Applet
In the following program, we create a simple animation in which a car moves from left to right on the Applet screen.

import java.applet.*;
import java.awt.*;
import java.net.*;
public class AnimationDemo extends Applet {
   Image pic;
   public void init() {
      String imageURL = "car.jpg"; // path to the image
      // get the URL of the document in which this applet is embedded
      URL docURL = getDocumentBase();
      pic = getImage(docURL, imageURL);
   }
   public void paint(Graphics g) {
      int i = 0, j = 0;
      boolean moveCar = true;
      while(moveCar) { // move the car from left to right
         g.drawImage(pic, i, 30, 200, 150, this);
         i++;
         if ((i % 400) == 0) {
            i = 0; j++;
         }
         if ( j > 3) {
            moveCar = false; // stop the moving car
         }
         try {
            Thread.sleep(7);
         } catch(Exception e) { }
      }
   }
}


Passing Parameter to Applet
The Html file is used to pass parameters to the Applet and getParameter( ) method of Applet class is used to receive the parameters. See the program below :

Applet which accepts parameter
import java.applet.*;
import java.awt.*;
public class ParameterDemo extends Applet {
   String str;
   public void init() {
      str = getParameter("args");
   }
   public void paint(Graphics g) {
      g.drawString(str, 200, 200);
   }
}

HTML file
<!DOCTYPE html>
<html>
<title>Applet taking arguments</title>
<body>
<applet code="ParameterDemo.class" width="500" height="500">
<param name="args" value="Hello World! This is an Applet.">
</applet>
</body>
</html>

Back