coderz.py

Keep Coding Keep Cheering!

Java Applet

java thread class

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.
Applet Lifecycle
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

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement