Most of the developers start off their career with Java as their base language. Well, this is because Java provides various intriguing features such as servlets, frameworks, etc. which helps in establishing a stronghold on the programming concepts. One such feature is applets in Java. A Java Applet is a small software program which can be transferred over HTTP. In this Java Applet Tutorial, I will be giving you a complete insight into Java Applets along with examples.
Below are the topics which I will be covering in this Java Applet Tutorial:
- Java Applets
- Java Applications vs Java Applets
- Applet Hierarchy in Java
- Java Applet Program
- Executing Applets in Java
- Applet Life Cycle
- Java Applet Demo
Java Applets
Applets in Java are small and dynamic internet-based programs. A Java Applet can be only executed within the applet framework of Java. For an easy execution of applets, a restricted ‘sandbox’ is provided by the applet framework. Generally, the applet code is embedded within an HTML page. The applet codes are executed when the HTML pages get loaded into the Java-compatible web browsers. Applets are mainly downloaded on remote machines and used on the client side.
A Java applet can be a fully functional Java application as well since it can utilize a complete Java API at its own accord. But still, there is a thin line between an applet and an application in Java.
In the next section of this article on Applets in Java, I will be listing down the differences between a Java Applet and a Java application.
Java Applet vs Java Application
Now that you know, how a Java applet differs from a Java application, let me show you how to create a basic applet in Java through the next section of this Java Applets Tutorial.
Applet Hierarchy in Java
- class java.lang.Object
- class java.awt.Component
- class java.awt.Container
- class java.awt.Panel
- class java.applet.Applet
- class java.awt.Panel
- class java.awt.Container
- class java.awt.Component
As displayed above, the Java Applet class which is a class of applet package extends the Panel class of awt package. The Panel class is a subclass of the Container class of the same package. The Container class is an extension of Component class belonging to the same package. The Component class is an abstract class and derives several useful classes for the components such as Checkbox, List, buttons, etc.
Now that you know about the complete hierarchy of the Java Applet class, let’s now try creating a simple Java Applet.
Java Applet Program
Below I have written a simple Java applet program which will simply display the welcome message.
EduApplet.java
import java.applet.Applet; import java.awt.Graphics; //Extending the Applet class public class EduApplet extends Applet{ public void paint(Graphics g){ g.drawString("Welcome To Edureka's Applet Tutorial",150,150); } }
By now you are familiar with applets and know how to create them. In the next section of this Java Applet Tutorial, I will be showing how to execute an applet in Java.
Executing Applets in Java
By now I have demonstrated how to create an applet, but how do you execute them? Well, unlike Java programs, executing applets is a bit different process. Since applets are net based applications they need a special environment to execute. Java provides two standard way to execute an applet:
Using a Java-Enabled Web Browser
If you are trying to execute your Applet in this way, first you need to compile your Java Applet file. Once done, you have to create a separate HTML file and add the applet code within it with the reference to your .class file within it. Now you can click the HTML file to launch the applet in the browser. Below I have given the code required to create the HTML file:
appletDemo.html
<html> <body> <applet code="EduApplet.class" width="300" height="300"> </applet> </body> </html>
Using appletviewer
In order to execute a Java Applet in this way, all you need to do is, instead of creating a separate file for HTML code, you can directly add comment at the beginning of your Java source code file indicating the presence of APPLET tag within. This helps in documenting your Java code with a prototype of the necessary HTML statements. This been done, now you can execute your applet just by starting the Java Applet Viewer which comes by default with JRE. When using this way of execution, your source code should look like below:
EduApplet.java
import java.applet.Applet; import java.awt.Graphics; /* <applet code="EduApplet" width=200 height=60> </applet> */ public class EduApplet extends Applet{ public void paint(Graphics g){ g.drawString("Welcome To Edureka's Applet Tutorial",150,150); } }
Note: You can also install any IDE such as Eclipse, and execute your codes directly from there itself.
Now that you know what is Java Applet and how it is executed, let’s dive deeper into Java Applets Tutorial and get familiar with the lifecycle of the Java Applets in the next section of this Java Applet Tutorial.
Applet Life Cycle
Every Java Applet needs to go through a series of phases from initialization to destruction in order to complete its execution. For that, the very first step is to inherit the java.applet.Applet class. This class aids with various methods which help in holding up a basic framework for the Java Applets. The various methods involved in the life cycle of Java Applet has been depicted by the below diagram.
- public void init(): This is the very first method to be invoked during the life cycle of an applet. In this method, the variable that will be used further in the applet is initialized. One thing you must note here is that this method can be invoked only once per applet life cycle.
- public void start(): This is the second method that is invoked just after the init() method is called by the browser. Each time a user revisits the web page containing the applet, start() method is invoked and the applet is started.
- public void stop(): This method is invoked whenever a user leaves the web page containing applet. In other words, the stop() method is used to suspend the threads which are not required when the applet is in the background or is not visible on the screen. These can be easily resumed using the start() method.
- public void destroy(): Finally, we have the destroy() method which is invoked in order to completely remove an applet from the memory. This method is invoked only once per applet life cycle and all the engaged resources must be freed up before this method is called.
One more method that is mostly used along with the above four is paint().
- public void paint(Graphics g): This method is invoked whenever an applet needs to be redrawn or repainted in the browser, irrespective of the cause. The paint() method takes one Graphic object as a parameter that contains the graphics context in which the applet is being executed. Also, this method is invoked each time output is expected from the applet.
Below is the basic skeleton of a Java Applet with all the life cycle methods.
AppletLifeCycle.java
import java.applet.*; public class AppletLifeCycle extends Applet{ public void init(){ System.out.println("Applet is Initialized"); } public void start(){ System.out.println("Applet is being Executed"); } public void stop() { System.out.println("Applet execution has Stopped"); } public void paint(Graphics g) { System.out.println("Painting the Applet..."); } public void destroy() { System.out.println("Applet has been Destroyed"); } }
Let’s now put whatever we have learned in this tutorial together and try to create an applet which can respond to user actions.
Java Applet Demo
Below I have included a small and simple Java Applet program where you will see how event handling works while an applet courses through its life cycle.
AppletEventHandling.java
import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; public class AppletEventHandling extends Applet implements MouseListener { StringBuffer strBuf; public void init() { addMouseListener(this); strBuf = new StringBuffer(); addItem(" Initializing the applet"); addItem(" Welcome to the Edureka's Applet Tutorial"); } public void start() { addItem(" Starting the applet "); } public void stop() { addItem(" Stopping the applet "); } public void destroy() { addItem(" Destroying the applet"); addItem(" Good Bye!!"); } void addItem(String word) { System.out.println(word); strBuf.append(word); repaint(); } public void paint(Graphics g) { g.drawString(strBuf.toString(), 10, 20); setForeground(Color.white); setBackground(Color.black); } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseClicked(MouseEvent event) { addItem(" Mouse is Clicked!!"); } }
Next step is to compile the above .java file into .class file. Once done, now you need to create an HTML file and add the reference of the .class file in the applet tag. Below I have demonstrated how to write the code for this HTML file.
eventHandling.html
<html> <title>Event Handling</title> <applet code = "AppletEventHandling.class" width = "300" height = "300"> </applet> </html>
When you execute this code, your applet should look like the below-shown screenshot.
Now that you have understood basics of Java Applet, check out the Java Certification Course by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA Training and Certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Java Applet Tutorial” and we will get back to you as soon as possible.