Applet in Java

 Java applets are small programs that run on a web browser to perform a specific task. They were introduced with the release of the first version of Java in 1995 and quickly became popular for their ability to add dynamic content to web pages. Applets are essentially mini-programs that are written in Java and run within the web browser using a Java Virtual Machine (JVM). 

What are Applets?

Java applets are programs written in Java that can be embedded in a web page and run on a user’s browser. Applets were created as a way to bring dynamic content to web pages and provide interactivity to the user. Applets can be used for a wide range of tasks, from creating interactive games to displaying real-time data.

Applets are similar to Java applications in that they are written in the same programming language and share many of the same features. However, applets are designed to be run within a web browser, while Java applications are designed to be run as standalone programs.

How do Applets work?

Applets are loaded into a web page using a <applet> tag, which specifies the location of the applet’s code and any necessary parameters. When the web page is loaded, the browser downloads the applet’s code and runs it within the JVM. The applet’s code has access to the browser’s Document Object Model (DOM), which allows it to interact with the web page and modify its contents.

Unlike Java applications, applets run within a sandboxed environment, which restricts their access to the user’s system. This is done for security reasons, as applets can potentially be used to execute malicious code. The sandboxed environment ensures that applets cannot access the user’s files or network resources without their explicit permission.

  • Applets are small Java applications that can be accessed on an Internet server. 
  • it can be transported over the Internet, and it can be installed and run automatically as part of a web document. 
  • Java applet is a class that extends the class of java.applet.Applet.
  •  There is no main() method in any Applet class. 
  • Using JVM it is regarded. 
  • The JVM can operate an applet application in two ways using a Web browser plug-in or a distinct runtime environment. 
  • JVM(Java Virtual Machine) generates an applet class instant and invokes an init method (init()) to initialize an applet. 

 

Example of Applet –

import java.awt.*; 

import java.applet.*; 

public class Simple extends Applet 

public void paint(Graphics g) 

 { 

  g.drawString(“A simple Applet”, 20, 20); 

 } 

 

Let’s go over each line of code and what it does:

import java.awt.*; – This imports the java.awt package, which contains the classes for creating graphical user interfaces.

import java.applet.*; – This imports the java.applet package, which contains the classes for creating applets.

public class MyFirstApplet extends Applet { – This defines a class called MyFirstApplet that extends the Applet class. This means that MyFirstApplet is an applet.

public void paint(Graphics g) { – This defines a method called paint that takes a Graphics object as its argument. The paint method is called by the applet when it needs to be displayed on the screen.

g.drawString(“Hello, World!”, 20, 20); – This draws the string “Hello, World!” on the applet’s canvas at the coordinates (20, 20).

We need to create an HTML file to run this applet and that HTML file embeds the applet in a web page.

These 2 packages which every Applet application must import- java.applet. & java.awt – 

  • Abstract Window Toolkit (AWT) classes are those classes that are imported by java.awt.*. Applets communicate via the AWT with the client directly or indirectly. 
  • These AWT classes include support for a graphical user interface based on a window. Java.applet.* imports the Applet package and contains the Applet class. 

Any applet which we generate must be an Applet class subclass. 

The class in the program must be declared public because the code which is outside of the program will be accessed to it. Every request in Applet must declare a method for paint. AWT class defines this method and the applet must override this method. At every moment an applet requires to redisplay its output, for which the paint method (paint()) is called. Another significant thing to notice about the applet which is important is that an applet execution does not start with the main method. In reality, there is no primary or any main method in an applet implementation. 

Benefits of Applets –

  • As it operates on the client side, it requires much less response time. 
  • Any browser that has JVM operating in it only can operate it. 
  • The life cycle of an applet can be defined as the process of how the object is created, started, stopped, and destroyed during the entire execution of its application. 
  • Along with the browser, the applet works on the client side also, thus having less processing time.

Methods of Applet Life Cycle-

It basically has five core methods which are – init(), start(), stop(), paint(), and destroy().

These five methods are invoked by the browser to execute.

Applet Life Cycle in Java-

Below are those five methods-

  1. init(): This method is the first method to run that initializes the applet. It can be invoked only once at the time of initialization only. The web browser creates the initialized objects, i.e., runs init() method within the applet but after checking the security settings the web browser.
  2. start(): This method contains the actual code of the applet and this method’s responsibility is to start the applet also. The init() method is invoked after that this method is invoked immediately. Every time the start() method is invoked when the browser is loaded or refreshed. Whenever the applet is maximized, restored, or moved from one tab to another in the browser, the start() method is invoked. It is in an inactive state until the init() method is invoked, after the invocation of init() method it is in an active state.
  3. stop(): This stop() method is used to stop the execution of the applet. It is invoked when Applet is stopped or the browser is minimized in both cases. The start() method is invoked when we go back to that page.
  4. destroy(): When the work is done, the destroy() method destroys the applet. It is invoked when either the applet window is closed or the tab containing the webpage which is closed. It removes the applet object from the memory and executed only once. After the applet is destroyed, we can’t start the applet.
  5. paint(): The paint() method belongs to the Graphics class in Java. This method is used to draw shapes like circles, squares, etc., in the applet. This method is executed after the start() method and when the browser or applet windows are resized then also this method is executed.

Below is the sequence of method execution when an applet is executed:

init()

start()

paint()

stop()

destroy()

Applet Life Cycle Working-

The software named Java plug-in software is responsible for managing the life cycle of an applet. An applet is a Java application that is executed in any web browser and it works on the client side. It doesn’t have the main() method because it runs in the browser. It is thus created to be placed on an HTML page.

The init(), start(), stop(), and destroy() all these methods belong to the applet.Applet class.

The paint() method belongs to the awt.Component class.

In Java, if we want to make a class an Applet class, we need to extend the Applet

Whenever we create an applet, it means we are creating the instance of the existing Applet class. And thus, we can use all the methods of that class.

These methods are invoked by the browser automatically. There is no need to call them explicitly.

 

Syntax of the entire Applet Life Cycle in Java-

class TestAppletLifeCycle extends Applet {  

public void init() {  

// initialized objects  

}   

public void start() {  

// code to start the applet   

}  

public void paint(Graphics graphics) {  

// draw the shapes  

}  

public void stop() {  

// code to stop the applet   

}  

public void destroy() {  

// code to destroy the applet   

}  

}  

 

Leave a Reply