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

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

1 month ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

2 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

2 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

3 months ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 months ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 months ago