A Java Applet is a small application written in Java that can be embedded in a web page and executed within a web browser. Although Java Applets were widely used in the past to create interactive applications, they have been deprecated in most modern web browsers due to security concerns and the rise of more advanced web technologies like JavaScript and HTML5.
Java Applets run in a Java-enabled browser (or an applet viewer) and are typically used for tasks that require rich graphical user interfaces (GUIs), animations, or game-like interactions.
The life cycle of an applet involves the following phases:
init()
method is
called.start()
method is invoked after
init()
.paint()
method is called
after start()
, which is responsible for drawing the
graphical interface.stop()
method is called to stop execution.destroy()
method is
invoked when the applet is about to be removed from memory.An applet extends the java.applet.Applet
class (or
javax.swing.JApplet
for Swing-based applets) and overrides
certain methods to perform its tasks. A typical applet includes the
following methods:
init()
: Used for initialization. It’s
called when the applet is loaded.start()
: Called after
init()
and when the applet becomes visible.stop()
: Used when the applet is no
longer visible.destroy()
: Called when the applet is
being destroyed.paint(Graphics g)
: Called when the
applet needs to redraw itself.Here’s an example of a basic applet that draws a simple message on the screen.
import java.applet.Applet;
import java.awt.Graphics;
public class SimpleApplet extends Applet {
// Initialization method
public void init() {
// Applet initialization code
}
// Paint method to display content
public void paint(Graphics g) {
// Display "Hello, Applet!" message
.drawString("Hello, Applet!", 50, 60);
g}
// Start method - executed after init
public void start() {
// Applet start code
}
// Stop method - executed when applet is no longer needed
public void stop() {
// Applet stop code
}
// Destroy method - executed before applet is removed from memory
public void destroy() {
// Applet destroy code
}
}
Explanation:
paint()
method draws the string “Hello, Applet!” at
coordinates (50, 60).init()
, start()
, stop()
,
and destroy()
methods are part of the applet life cycle but
are not required for this simple example.To run a Java Applet, you typically need an HTML file that references the applet. Here’s an example of how an HTML page can load and run the applet.
<html>
<body>
<applet code="SimpleApplet.class" width="300" height="300">
</applet>
</body>
</html>
Explanation:
<applet>
tag specifies the applet’s class
(SimpleApplet.class
) and the dimensions of the applet’s
display area.width
and height
attributes define the
size of the applet window in the browser.Java Applets face several security concerns because they run in the context of the browser and can potentially access the local system, perform malicious activities, or breach the security of the host computer. Some security restrictions applied to applets include:
SecurityManager
class is used to manage the access control of applets, specifying what
the applet is allowed to do.As mentioned earlier, applets were once widely used but have been deprecated and are no longer supported in most modern browsers (Chrome, Firefox, etc.) due to:
Java Applets were an essential technology in the past for building interactive web-based applications. They provide a way to run Java code within a web browser and offer functionality for graphical user interfaces, animation, and networking.
However, with the rise of modern web technologies and security concerns, applets are no longer widely used. Java Applets are deprecated and are no longer supported by most browsers, making them largely obsolete for new development. For modern web-based interactive applications, alternatives like JavaScript, HTML5, and CSS3 are now the preferred tools.