Java Applets: Introduction and Implementation Guide

Rumman Ansari   Software Engineer   2024-07-05 04:58:46   9414  Share
Subject Syllabus DetailsSubject Details 28 Questions
☰ TContent
☰Fullscreen

Table of Content:

What is java applet?

An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java-compatible web browser. or in another way we can say Applet is a special type of program that is embedded in the webpage to generate the dynamic content. It runs inside the browser and works at client side.

The creation of the applet changed Internet programming because it expanded the universe of objects that can move about freely in cyberspace. In general, there are two very broad categories of objects that are transmitted between the server and the client: passive information and dynamic, active programs. For example, when you read your e-mail, you are viewing passive data. Even when you download a program, the program’s code is still only passive data until you execute it. By contrast, the applet is a dynamic, self-executing program. Such a program is an active agent on the client computer, yet it is initiated by the server.

Why applet is important

There are many importance of applet. They are as follows:

  • Security and portability : The same applet must be able to be downloaded and executed by the wide variety of CPUs, operating systems, and browsers connected to the Internet. It is not practical to have different versions of the applet for different computers. The same code must work on all computers. Therefore, some means of generating portable executable code was needed. As you will soon see, the same mechanism that helps ensure security also helps create portability
  • It works at client side so less response time.
  • It can be executed by browsers running under many plateforms, including Linux, Windows, Mac Os etc.
  • An applet can be a fully functional Java application because it has the entire Java API at its disposal.

Drawback of Applet

  • Plugin is required at client browser to execute applet.

Life Cycle of an Applet

Various states, an applet, undergoes between its object creation and object removal (when the job is over) is known as Applet Life Cycle. Each state is represented by a method. There exists 5 states represented by 5 methods. That is, in its life of execution, the applet exists (lives) in one of these 5 states.

  1. Applet is initialized(init).
  2. Applet is started(start).
  3. Applet is painted(paint).
  4. Applet is stopped(stop).
  5. Applet is destroyed(destroy).

These methods are known as "callback methods" as they are called automatically by the browser whenever required for the smooth execution of the applet. Programmer just write the methods with some code but never calls.

Following are the methods.

  1. init() method
  2. start() method
  3. paint() method
  4. stop() method
  5. destroy() method

These methods are known as Applet Life Cycle methods. These methods are defined in java.applet.Applet  class except paint() method. The paint() method is defined in java.awt.Component class, an indirect super class of Applet.

Description of Applet Life Cycle methods

Even though the methods are called automatically by the browser, the programmer should know well when they are called and what he can do with the methods. Following is the schematic representation of the methods.

Applet Life Cycle

Brief Description of Life Cycle Methods

Following is the brief description of the above methods.

  1. init(): The applet's voyage starts here. In this method, the applet object is created by the browser. Because this method is called before all the other methods, the programmer can utilize this method to instantiate objects, initialize variables, setting background and foreground colors in GUI etc.; the place of a constructor in an application. It is equivalent to born state of a thread.
  2. start(): In init() method, even through applet object is created, it is in inactive state. An inactive applet is not eligible for microprocessor time even though the microprocessor is idle. To make the applet active, the init() method calls start() method. In start() method, applet becomes active and thereby eligible for processor time.
  3. paint(): This method takes a java.awt.Graphics object as a parameter. This class includes many methods of drawing necessary to draw on the applet window. This is the place where the programmer can write his code of what he expects from applet like animation etc. This is equivalent to runnable state of thread.
  4. stop(): In this method, the applet becomes temporarily inactive. An applet can come any number of times into this method in its life cycle and can go back to the active state (paint() method) whenever would like. It is the best place to have a cleanup code. It is equivalent to the blocked state of the thread.
  5. destroy(): This method is called just before an applet object is a garbage collected. This is the end of the life cycle of an applet. It is the best place to have cleanup code. It is equivalent to the dead state of the thread.

After knowing the methods, let us know when they are called by the browser.

  • init() method is called at the time of starting the execution. This is called only once in the life cycle.
  • start() method is called by the init() method. This method is called a number of times in the life cycle; whenever the applet is deiconifed , to make the applet active.
  • paint() method is called by the start() method. This is called a number of times in the execution.
  • stop() method is called whenever the applet window is iconified to inactivate the applet. This method is called a number of times in the execution.
  • destroy() method is called when the applet is closed. This method is called only once in the life cycle.

Observe, the init() and destroy() methods are called only once in the life cycle. But, start(), paint() and stop() methods are called a number of times.

Hierarchy of Applet

As displayed in the below diagram, Applet class extends Panel. Panel class extends Container which is the subclass of Component.

Hierarchy of Applet

How to run an Applet?

There are two ways to run an applet

  1. By html file.
  2. By appletViewer tool (for testing purpose).

Simple example of Applet by html file:

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

Program

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

Html file

file name: mySimpleApplet.html

 
 <html>  
<body>  
  <applet code="Simple" width="300" height="300">  </applet>  
</body>  
</html>  
 

Now Compile the program and open the html file using cmd

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Users\Hello World>I:

I:\>cd Java Programming

I:\Java Programming>javac Simple.java

I:\Java Programming>appletviewer mySimpleApplet.html

After Compilation: folder contains

My Location (present working directory): I:/Java Programming/

Java applet output files

Output

Littlebit Explanation

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.

Simple example of Applet by appletviewer tool

Program

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

/*
 <applet code="Simple" width="300" height="300">  </applet>  
 
 */
 

Now Compile the program and run using appletviewer by cmd

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Users\Hello World>I:

I:\>cd Java Programming

I:\Java Programming>javac Simple.java

I:\Java Programming>appletviewer Simple.java

I:\Java Programming>

After Compilation: folder contains

My Location (present working directory): I:/Java Programming/

Output

Java applet output
MCQ Available

There are 1 MCQs available for this topic.

1 MCQ