Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Applet Class

  • Last Updated : 22 Apr, 2022

Java Applet is a special type of small Java program embedded in the webpage to generate dynamic content. The specialty of the Java applet is it runs inside the browser and works on the Client side (User interface side).

Java Applet Class

For Creating any applet in Java, we use the java.applet.Applet class. It has four Methods in its Life Cycle of Java Applet. The applet can be executed using the applet viewer utility provided by JDK. A Java Applet was created using the Applet class, i.e., part of the java.applet package.

The Applet class provides a standard interface between applets and their environment. The Applet class is the superclass of an applet that is embedded in a Web page or viewed by the Java Applet Viewer. The Java applet class gives several useful methods to give you complete control over the running of an Applet. Like initializing and destroying an applet, It also provides ways that load and display Web Colourful images and methods that load and play audio and Videos Clips and Cinematic Videos.

In Java, there are two types of Applet

  1. Java Applets based on the AWT(Abstract Window Toolkit) packages by extending its Applet class
  2. Java Applets is based on the Swing package by extending its JApplet Class in it.

Now We See The Life Cycle of an Applet and its Methods-

How to run an Applet?

There are two ways to execute a Java Applet:

  • By using an HTML file
  • By using the appletviewer tool

Life Cycle Of java Applet Class

Life Cycle Of java Applet Class has four main methods in it-

  1. init()
  2. Start()
  3. Stop()
  4. Destroy()

Explanation:

1. void init():  This init() method is the first method of a java applet. This is used to initialize the applet when the applet begins to execute

2. void start(): void start() this method is called automatically after the init() method, and it is used to start the Applet and to implementation of an applet

3. void stop(): void stop() is used to stop the Applet or to stop the running applet

4. void destroy(): void destroy() is used to destroy the Applet / to Terminate the applet.

Other Methods in Applet Class:

5. System.out.println(String): This Method Works from appletviewer, as not from browsers, and it Automatically opens an Output window.

6. ShowStatus(String): This Method Displays the String in the Applet’s status line, and each call overwrites the previous call, and You have to allow time to read the line.

7. String getParameter(String ParameterName): It Returns the value of a parameter defined in a Current Applet.

8. Image getImage(URL url): This method returns an Image object which contains an image specified at its location, url.

9. void play(URL url): This method can play an audio clip found at the specified location, url.

10. setStub: It sets this applet’s stub, which is done automatically by the system.

11. isActive: This method Determines if this current applet is active. Then Applet is marked active just before its start method is invoked. Then It becomes inactive immediately after its stop method when it is initialized.

Constructor Of Applet Class 

The Applet class is just like any other class as an Applet Constructor is simply the subclass constructor of the Applet class. Therefore, Because the applet constructor is just like or any other constructor, it cannot be overridden, so Constructors perform any Necessary initialization for the new object or Create the new Object for it.

Applet() – It Constructs a new Applet.

Example of Applet: The Applet program using appletviewer- 

Java




// This is a Simple Java Applet
// program using appletviewer
 
import java.applet.*;
import java.awt.*;
 
/*
<applet  code="AppletExp1" width=600 height=300>
</applet>
*/
 
public class AppletExp1 extends Applet {
    public void init()
    {
        System.out.println("Initializing an applet");
    }
 
    public void start()
    {
        System.out.println("Starting an applet");
    }
    public void stop()
 
    {
        System.out.println("Stopping an applet");
    }
    public void destroy()
 
    {
        System.out.println("Destroying an applet");
    }
}

By using Appletviewer, type the following command at the command prompt-

Output:

Then after An Window is Opening the Final applet Output Applet Window-

Advantages of Applet

  • It runs inside the browser and works on the Client-side, so it takes less time to respond.
  • It is more Secured
  • It can be Executed By multi-platforms with any Browsers, i.e., Windows, Mac Os, Linux Os.

Disadvantages of Applet

  • A plugin is required at the client browser(User Side) to execute an Applet.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!