Categories: Java

Java Applet

A Java Applet is a sort of software that is embedded in a webpage to generate dynamic content. It operates on the client side and runs within the browser.

  • All applets are subclasses (either directly or indirectly) of java.applet.Applet class.
  • Applets are not independent programs. They instead run in a web browser or an applet viewer. Applet viewer is a standard applet viewer tool provided by JDK.
  • In general, an applet’s execution does not begin with the main() method.
  • System.out.println does not conduct applet window output (). Rather, it is handled via AWT methods such as drawString ().
  • Java Applet is deprecated since Java 9. It means Applet API is no longer considered important.
Java Applet Lifecycle:

The phases in Applet are as follows:

  • The applet has been launched.
  • The applet has begun.
  • Applet has been painted.
  • Applet has been terminated.
  • Finally, Applet has been destroyed.
Example:
import java.awt.*;
import java.applet.*;
public class Coderz extends Applet
{
  public void paint(Graphics g)
    {
      g.drawString("A simple Applet", 20, 20);
    }
}
Benefits of Applet:

Applets have numerous advantages.

  • It operates on the client side, resulting in a faster response time.
  • Secured
  • It can be executed by browsers operating on a variety of platforms, including Linux, Windows, and macOS.
Disadvantage of Applet:

The disadvantage of the Applet Plugin is that the client browser must execute the applet.

Class java.applet.Applet:

To create an applet, the java.applet.Applet class must be inherited. It includes four applet life cycle techniques.

  • The Applet is initialized using public void init(). It is only used once.
  • public void start(): is called when the init() function has been called or the browser has been maximized. It’s used to launch the Applet.
  • The function public void stop() is used to terminate the Applet. It is called when the applet is stopped or the browser is minimized.
  • The Applet is destroyed with public void destroy(). It is only used once.
Launching an Applet:

There are two methods for running an applet.

  • Using an HTML file.
<html>  
<body>  
<applet code="Coderz.class" width="300" height="300">  
</applet>  
</body>  
</html>  
  • Using the applet viewer tool (for testing purposes).
c:\>javac Coderz.java
c:\>appletviewer Coderz.java

Note: also read about the Legacy Classes in Java

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

2 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

1 year ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

1 year ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

1 year ago