Java applets
An applet is a Java program that runs on a webpage within a web browser.
This lesson focuses on:
- The Applet class
- Methods of the Applet class
- Placing an applet on a webpage
- Viewing an applet
The Applet class
The Applet class is used to create applets. This class is located in the java.applet package. Applets are not created by instantiating an Applet object, rather by creating a class which extends the Applet class.
Example:
import java.applet.*;
class anApplet extends Applet{
}
In the above example, a class named anApplet that extends the Applet class is created. The java.applet package is imported into this class.
Methods of the Applet class
The Applet class has several methods:
-
boolean isActive()
This method checks whether an applet is active or not.
-
void resize(int width, int height)
This method resizes an applet to the specified width and height.
-
void play(URL url)
This method plays an audioclip specified by url.
-
start()
This method informs an applet that it should start its execution.
Placing an applet on a webpage
An applet is placed on a webpage using HTML's <object> tag.
Example:
<object title="An applet" code="anApplet.class" width="200" height="200"></object>
In the above example, an applet named anApplet is placed on a webpage using the <applet> tag. The title attribute denotes a title to associate with the applet. The code attribute denotes the name and location of the applet. The height attribute denotes the height of the applet. The width attribute denotes the width of the applet.
Viewing an applet
You can view an applet by viewing the webpage on to which the applet is placed in a web browser or you can use the Java appletviewer command line tool to view it.
Syntax on the command line to view an applet:
appletviewer name_Of_Webpage_Where_Applet_Is_Located
Example:
appletviewer webpage.html
In the above example, the appletviewer utility is used to view a page named webpage.html, and if there is an applet on this page, then the appletviewer window will display it, otherwise nothing will be displayed.




