![](./write-blog/images/21592605-java-applet-introduction.png)
Java Applets: Introduction and Implementation Guide
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.
- Applet is initialized(init).
- Applet is started(start).
- Applet is painted(paint).
- Applet is stopped(stop).
- 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.
- init() method
- start() method
- paint() method
- stop() method
- 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.
Brief Description of Life Cycle Methods
Following is the brief description of the above methods.
- 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.
- 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.
- 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.
- 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.
- 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.
How to run an Applet?
There are two ways to run an applet
- By html file.
- 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.javaimport 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.htmlNow 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]( http://www.atnyla.com/library/images-tutorials/java-applet-output-files.png)
Output
![]( http://www.atnyla.com/library/images-tutorials/java-applet-output.png)
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.javaimport java.awt.*; import java.applet.*; public class Simple extends Applet { public void paint(Graphics g) { g.drawString("A simple Applet", 20, 20); } }/* */
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/
![]( http://www.atnyla.com/library/images-tutorials/java-appletviewer-output-files.png)
Output
![Java applet output]( http://www.atnyla.com/library/images-tutorials/java-applet-output.png)