by Dinesh Thakur Category: AWT and Applets

One of the main features of Java is the applet. Applets are dynamic and interactive programs. Applets are usually small in size and facilitate event-driven applications that can be transported over the web.

An applet is a Java code that must be executed within another program. It mostly executes in a Java-enabled web browser. An applet can be embedded in an HTML document using the tag <APPLET> and can be invoked by any web browser. Parameters can be passed to an applet using the tag <PARAM> in the HTML document. Applets can also be executed using the appletviewer utility provided in the J2SDK kit.

 Java applets are downloaded automatically and run by the web browser. Any web-browser that supports Java, such as Applet Viewer, can be used to transport applets to the Internet. An applet can perform many functions such as graphics, play sound, animation, arithmetic operations, play games etc.

Applets differ from application programs in the sense that they do not use main() program for initiating the execution of code. They do not run independently but from inside a web page that makes an efficient use of HTML tags. To build an applet code, we need to include certain packages like java.awt and java.applet that contains the Graphics class that provides methods where the output of all applet operations are performed. A simple program using applet looks like:

A Java applet must be a public class. Thus, the Applet class can be accessed when the applet is run in a web browser or in the Appletviewer. The applet code will be compiled even if the class is not declared as public, but the applet will not run. An applet code looks as shown below:


import java.applet.*;

public class SampleApplet extends Applet


   //applet code 


Like any other Java program, the applet's class name must match with the name of the file in which it is written.

Applets do not contain the main () that is required to run a Java application and thus they cannot run on its own; however, usually applets run in a container that provides the missing code, The main features of applet include the following:

• It provides a GUI

• facilitates graphics, animation and multimedia

• provides a facility for frames

• enables a event handling

• avoids a risk and provides a secure two-way interaction between web pages.

Note that it is possible to execute applets using the Java interpreter (using the main () method) if the class is extended with Frame.

An applet can reside anywhere on the web and can be downloaded on any computer. Applets have limited access to the resources of the client into which they are installed from an Internet server. For this reason, applets avoid the risk of viruses or breaking the data integrity problems at the client. Applets provide security by reverifying the generated byte code and no memory space is allocated till the reverification process completes. Generally, a virus cannot be hidden in uninitialized memory. If the reverification process is not complete, the applet will be rejected. Further, applets cannot do the following:

• Read or write client file system (disks) unless specified explicitly; in such cases, the Applet is allowed only in certain directories

• Access the source code of the applets; the applet's source code can be accessed only from the original server

• Execute local programs/libraries/ DLLs on the client

• Opening network connections other than to a HTTP server: applets only talk to the server that they live on

• Finding information about the user such as user name, directories and applications installed

Applets are very useful for the presentation and demonstration of a concept in an effective manner. For example, an applet designed for explaining the bubble sort algorithm shows visually how the bubble is moved during each pass of the algorithm, Usually, applets interact with the AWT controls and it is necessary to import the awt package (java.awt*) and also the event package (java.awt.event.*) for event handling. Input/Output streams are not commonly used for applets for the purpose of I/O. Figure illustrates the byte code of an applet running at a client.

             Byte code of an applet running at a client.

                                       In Table the main methods of Applet class.




destroy ( )

Method triggered by the browser or applet viewer to indicate that the applet should release all the resources used .

getAppletContext ( )

Returns the execution context of the applet allowing interaction with the browser.

getAppletInfo ( )

Returns information associated with the applet .

getCodeBase ( )

Returns the source URL of the applet .

getDocumentBase ( )

Returns the source document for the applet.

getImage ( URL )

obtained an image at the specified URL .

getParameter ( String )

Gets the value of the specified parameter.

getParameterInfo ( )

Returns parameters associated with the applet information.

init ( )

Method triggered by the browser or applet viewer to indicate that the applet was loaded.

resize ( int , int )

Resizes the applet for the given values ​​of width and time.

showStatus ( String )

Displays the given message to the window or the status line.

start ()

Method triggered by the browser or applet viewer to indicate that the applet should start its execution.

stop ()

Method triggered by the browser or applet viewer to indicate that the applet should stop its execution.


import java.awt.*;
import java.applet.*;
/* <applet code="NewApplet" width=500 height=200> </applet>*/
public class NewApplet extends Applet
public void paint (Graphics g)
g.drawString("New Applet Program", 20, 100);

The statement below draws the string New Applet Program at the pixel position (20, 100 


To execute Java Applet, appletviewer is used. We pass the source filename as an argument to appletviewer because it will read the <applet> tag given in comment part of the source file. From there, it will find which class has to execute from the attribute code, what will be the width and the height of applet window, read from width and height attribute of <applet> tag respectively. The screenshot below explains the compilation and execution of a Java Applet.
                          What is Applet

The screenshot below shows the starting of an Applet.
                          NewApplet Appletviewer

About Dinesh Thakur


Related Articles