Applet in Java

  • Applets are small Java applications that can be accessed on an Internet server, transported over Internet, and can be automatically installed and run as apart of a web document.
  • After a user receives an applet, the applet can produce a graphical user interface. It has limited access to resources so that it can run complex computations without introducing the risk of viruses or breaching data integrity.
  • Any applet in Java is a class that extends the java.applet.Applet class.
  • An Applet class does not have any main() method. It is viewed using JVM. The JVM can use either a plug-in of the Web browser or a separate runtime environment to run an applet application.
  • JVM creates an instance of the applet class and invokes init() method to initialize an Applet.

A Simple Applet

import java.awt.*;
import java.applet.*;
public class Simple extends Applet
{
 public void paint(Graphics g)
 {
  g.drawString("A simple Applet", 20, 20);
 }
}

creating simple applet


Every Applet application must import two packages - java.awt and java.applet.

java.awt.* imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT. The AWT contains support for a window-based, graphical user interface. java.applet.* imports the applet package, which contains the class Applet. Every applet that you create must be a subclass of Applet class.

The class in the program must be declared as public, because it will be accessed by code that is outside the program.Every Applet application must declare a paint() method. This method is defined by AWT class and must be overridden by the applet. The paint() method is called each time when an applet needs to redisplay its output. Another important thing to notice about applet application is that, execution of an applet does not begin at main() method. In fact an applet application does not have any main() method.


Advantages of Applets

  1. It takes very less response time as it works on the client side.
  2. It can be run on any browser which has JVM running in it.

Applet class

Applet class provides all necessary support for applet execution, such as initializing and destroying of applet. It also provide methods that load and display images and methods that load and play audio clips.

An Applet Skeleton

Most applets override these four methods. These four methods forms Applet lifecycle.

  • init() : init() is the first method to be called. This is where variable are initialized. This method is called only once during the runtime of applet.
  • start() : start() method is called after init(). This method is called to restart an applet after it has been stopped.
  • stop() : stop() method is called to suspend thread that does not need to run when applet is not visible.
  • destroy() : destroy() method is called when your applet needs to be removed completely from memory.

Note: The stop() method is always called before destroy() method.


Example of an Applet Skeleton

import java.awt.*; 
import java.applet.*; 
public class AppletTest extends Applet
{
 public void init()
 {
  //initialization 
 }
 public void start ()
 {
  //start or resume execution 
 }
 public void stop()
 {
  //suspend execution 
 {
 public void destroy()
 {
  //perform shutdown activity
 }
 public void paint (Graphics g)
 {
  //display the content of window
 }
}

Example of an Applet

import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet 
{
 int height, width;
 public void init() 
 {
  height = getSize().height;
  width = getSize().width;
  setName("MyApplet");
 }
 public void paint(Graphics g)
 {
  g.drawRoundRect(10, 30, 120, 120, 2, 3);
 }
}

applet example


How to run an Applet Program

An Applet program is compiled in the same way as you have been compiling your console programs. However there are two ways to run an applet.

  • Executing the Applet within Java-compatible web browser.
  • Using an Applet viewer, such as the standard tool, applet viewer. An applet viewer executes your applet in a window

For executing an Applet in an web browser, create short HTML file in the same directory. Inside body tag of the file, include the following code. (applet tag loads the Applet class)

< applet code = "MyApplet" width=400 height=400 >
< /applet >

Run the HTML file

running applet inside browser


Running Applet using Applet Viewer

To execute an Applet with an applet viewer, write short HTML file as discussed above. If you name it as run.htm, then the following command will run your applet program.

f:/>appletviewer run.htm

running applet using applet viewer