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

Select a Random Element from a Stream

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

1 day ago

Estimate π Using Monte Carlo Method

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

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago