Applet Programming in Java

Introduction

An Applet is the special type of Java program that is run on web browser. The Applet class provides the standard interface between applet and browser.  

An applet class does not have main method and it extends the java.applet.Applet class. An applet program runs through applet viewer.

Advantages of Applets

  • Applets are supported by most web browser.
  • Applets have very less response time, because it works at client side.
  • Applets are quite secure because of their access of resources.
  • An untrusted applet has no access to local machine.
  • The size of applets is small, making it easier to transfer over network.

Limitations of Applets

  • Applets require a Java plug-in to execute.
  • Applets do not support file system.
  • Applet itself cannot run or modify any application on the local system.
  • It cannot work with native methods of system.
  • An applet cannot access the client-side resources.

Applet Life Cycle

It is derived from the Applet class. When an Applet is created, it goes through different stages; it is known as applet life cycle.

1. Born or initialization state
2. Running state
3. Stopped  state
4. Destroyed state
5. Display state

Applet Life Cycle Methods

1. init ( )
The init( ) method is responsible for applet initialization when it is loaded. It is called by browser or applet viewer only once.

Syntax

public void init( )
{
      //actions
}


2. start ( )
It is invoked after the initialization state and applet entered into running state. It starts the execution of Applet and the applet becomes an active state.

Syntax

public void start()
{
      //actions
}


3. paint( )
This method is used to display output on screen. It takes java.awt.Graphics object as a parameter. It helps to create Applet GUI such as a colored background, drawing etc.

Syntax

public void paint(Graphics g )
{
       //Display statement
}


4. stop( )
This method is responsible to stop the execution and applet becomes temporarily inactive state. It is invoked when Applet is stopped.

Syntax

public void stop( )
{
      //actions
}


5. destroy( )
It destroys and removes the applet from the memory. It is invoked only once. This is end of the life cycle of applet and it becomes dead.

Syntax

public void destroy()
{
      //actions
}

Developing an Applet Program

Steps to develop an applet program
1. Create an applet (java) code i.e. .java file.
2. Compile to generate .class file.
3. Design a HTML file with <APPLET> tag.

Running the Applet Program

There are two ways to run the applet program
1. By HTML
2. Appletviewer tool

Example : Getting a message printed through Applet

import java.awt.*;
import java.applet.*;
public class AppletDemo
{
      public void paint(Graphics g)
      {
           g.drawString("Welcome to TutorialRide", 50, 50);
      }
}

1. Save the above program: AppletDemo.java
2. Compile: > javac AppletDemo.java

//AppletDemo.html

<html>
      <head>
            <title> AppletDemo </title>
      </head>
      <body>
            <applet code="AppletDemo.class" width="200" height="250">
            </applet>
      </body>
</html>


Run the complete program:
> appletviewer AppletDemo.html

NOTE: To execute the applet program through appletviewer tool, write it on command prompt.
C :\> javac AppletDemo.java
C :\> appletviewer AppletDemo.java

Difference between Applications and Applets

ApplicationsApplets
An application runs stand-alone.An applet program runs under the control of browser.
It can access any data or software available on the system.It cannot access anything on the system except browser’s service.
Execution start from the main() method.Execution start from the init() method because main() is not available.
It can read and write to the file system.Cannot read and write to the file systems.
Does not provide any security.An applet provides the security features.
Application run using Java interpreter.It runs using applet viewer or on web browser.