Unit 1: The Applet Class

5th Semester

Applet

  • 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.

Types of Applet: There are two types of applet. The applet type varies based on how the applet is embedded into web page. Two types of applet are:

  • Local Applet: Local applets are applets types that are developed and stored in local system. The web page will search the local system directories, find the local applet and execute it. Execution of local applet does not require Internet connection
  • Remote Applet: A remote applet is that which is developed by someone else and stored on remote computer connected with the internet. The web page requires Internet connection to locate and load the remote applet from the remote computer.

Creating an Applet
 An applet must be subclass of Applet class or JApplet class. We can create an applet by extending the

  • Applet class of java.applet package
  • JApplet class of javax.swing package

Application vs. Applet : Comparison Table

Application Applet
Applications are stand-alone programs that can be run independently without having to use a web browser. Applets are small Java programs that are designed to be included in a HTML web document. They require a Java-enabled browser for execution.
Java applications have full access to local file system and network. Applets have no disk and network access.
It requires a main method() for its execution. It does not require a main method() for its execution.
Applications can run programs from the local system. Applets cannot run programs from the local machine.
An application program is used to perform some task directly for the user. An applet program is used to perform small tasks or part of it.
It can access all kinds of resources available on the system. It can only access the browser specific services.

A Simple Applet

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.

1. 3 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

Example of an Applet

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 bodytag of the file, include the following code. (applet tag loads the Applet class)

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.

running applet using applet viewer