HTML HTML Tutorial HTML Forms HTML Graphics HTML Media HTML APIs HTML Tags



HTML Tag: applet

The HTML Tag: applet is used to embed Java applets into web pages. Java applets are small programs written in the Java programming language that can be executed within a web browser. The applet tag was introduced in HTML 3.2 and is now deprecated in HTML5.

Brief Explanation of HTML Tag: applet

The applet tag has several attributes that can be used to customize the appearance and behavior of the Java applet. The most commonly used attributes are:

  • code: Specifies the name of the Java applet class file.
  • width: Specifies the width of the applet in pixels.
  • height: Specifies the height of the applet in pixels.
  • archive: Specifies a comma-separated list of URLs for the Java archive files that contain the applet class files.
  • align: Specifies the alignment of the applet within the surrounding text.

Here is an example of how to use the applet tag:

<applet code="MyApplet.class" width="300" height="200">
  <param name="param1" value="value1">
  <param name="param2" value="value2">
  Your browser does not support Java applets.
</applet>

The <param> tags are used to pass parameters to the Java applet. These parameters can be accessed by the applet using the getParameter() method.

It is important to note that Java applets require the Java Runtime Environment (JRE) to be installed on the user's computer. If the JRE is not installed, the applet will not be able to run.

Code Examples in HTML Tag: applet

Here are some examples of how to use the applet tag:

Example 1: Simple Java Applet

This example shows a simple Java applet that displays a message:

<applet code="HelloWorld.class" width="200" height="100">
  Your browser does not support Java applets.
</applet>

The HelloWorld.class file contains the following Java code:

import java.applet.*;
import java.awt.*;

public class HelloWorld extends Applet {
  public void paint(Graphics g) {
    g.drawString("Hello, world!", 20, 20);
  }
}

When the applet is loaded, it will display the message "Hello, world!" in the top-left corner of the applet area.

Example 2: Passing Parameters to a Java Applet

This example shows how to pass parameters to a Java applet using the <param> tag:

<applet code="MyApplet.class" width="300" height="200">
  <param name="param1" value="value1">
  <param name="param2" value="value2">
  Your browser does not support Java applets.
</applet>

The MyApplet.class file contains the following Java code:

import java.applet.*;
import java.awt.*;

public class MyApplet extends Applet {
  private String param1;
  private String param2;

  public void init() {
    param1 = getParameter("param1");
    param2 = getParameter("param2");
  }

  public void paint(Graphics g) {
    g.drawString("Parameter 1: " + param1, 20, 20);
    g.drawString("Parameter 2: " + param2, 20, 40);
  }
}

When the applet is loaded, it will display the values of the param1 and param2 parameters passed to it using the <param> tags.

Example 3: Using the Archive Attribute

This example shows how to use the archive attribute to specify a Java archive file that contains the applet class file:

<applet code="MyApplet.class" width="300" height="200" archive="MyApplet.jar">
  Your browser does not support Java applets.
</applet>

The MyApplet.jar file contains the MyApplet.class file and any other class files required by the applet.

References

Activity