Java Applet is a Java code that can be run in web browser. Java Applet uses complete Java API to create Java Applications that can perform any task at client side. These applets work as web pages inside browsers that can create dynamic data.
An applet is similar to a java class. It extends from the built in java.applet.Applet class. The hierarchy of Applet class is given below:
- The Applet class extends Panel class.
- The Panel class extends Container class.
- The Container class extends Component class.
Life Cycle of an Applet
An Applet goes through the following stages:
- init: An Applet is initialized. The method init() performs initialization of the applet. It is called only once after the param tags are processed. These <applet> tags are used to embed an applet in an HTML file.
- start: An Applet is started. The method start() starts the applet. It is called right after the init() method is called by the browser.
- paint: An Applet is painted. The method paint() is used to paint the applet. It provides Graphics to the applet. It is the method of component class java.awt.Component.
- stop: An Applet is stopped. The method stop() stops the applet. It is called when applet stops or when browser is minimized. It can be invoked multiple times.
- destroy: An Applet is destroyed. The method destroy() is called only once when the browser shuts down. It destroys the applet to kill the occupied resources.
Run Java Applet
In Java, there are two ways to run an Applet code. These methods are described in detail in the below sections:
- By html file
- By Applet viewer tool
Here is a simple example of Java Applet code that prints the text “Hello World!”.
//File name is Example.java import java.applet.*; import java.awt.*; public class Example extends Applet { public void paint (Graphics g) { // Paints the Applet g.drawString ("Hello World!", 50, 50); // Prints Hello World! text } }
The import statements used in the above code includes the following classes that are used in the program:
- java.applet.Applet: Every Applet extends from this class. This class provides methods and interfaces by which the browser actions are controlled and an Applet is executed.
- java.awt.Graphics: This class is used to adjust size of the text. Its Graphic class object can be used fro drawing shapes. This class also provides methods to display images of different formats like JPG, JPEG, PNG, GIF, etc.
Run by html file:
To execute the above applet code by using html file, following steps must be followed.
- Create an applet file
- Compile the applet file
- Create an html file. For example, the html file for above applet is applet.html:
<html> <body> <applet code="Example.class" width="300" height="300"> </applet> </body> </html>
- Add the applet code in the html file as shown in the html file above.
- Click on the html file
Run by appletviewer tool
The tool named AppletViewer allows the programmer to run one or more java applets. The AppletViewer tool can be downloaded from the official Java website. Following steps must be followed to execute applet using the appletviewer tool:
- Create an applet code
- Add applet tag in the comments of applet code as shown in the example below
- Compile the applet code
- Go to command prompt
- Run the applet using the command appletviewer followed by filename.
//File name is Example.java import java.applet.*; import java.awt.*; public class Example extends Applet { public void paint (Graphics g) { g.drawString ("Hello World!", 50, 50); } } /* <applet code="Example.class" width="300" height="300"> </applet> */
The commands to compile and run the above applet will be:
c:\>javac Example.java c:\>appletviewer Example.java
Advantages of Java Applet
Java Applet have multiple advantages. One of them is that it is has less response time as it works at client side. Applet is more secured. It supports various browsers on multiple platforms like Windows, Linux, Mac etc.