Trail: Deployment
Lesson: Applets
Section: Getting Started with Applets
Defining an Applet Subclass
Home Page > Deployment > Applets
Defining an Applet Subclass
The first bold line of the following listing begins a block that defines the HelloWorld class.
import javax.swing.JApplet;
import java.awt.Graphics;

public class HelloWorld extends JApplet {
    public void paint(Graphics g) {
	g.drawRect(0, 0, 
		   getSize().width - 1,
		   getSize().height - 1);
        g.drawString("Hello world!", 5, 15);

Applets inherit a great deal of functionality from the Applet or JApplet class, including abilities to communicate with the browser and present a graphical user interface (GUI) to the user.

An applet which will be using GUI components from Swing (Java's GUI toolkit) should extend the javax.swing.JApplet base class, which provides the best integration with Swing's GUI facilities.

JApplet provides the same "RootPane" top-level component structure as Swing's JFrame and JDialog components, whereas Applet provides just a simple panel. See How to Use Root Panesfor more details on how to utilize this feature.

An applet may extend java.applet.Applet when it makes no use of Swing's GUI components. This may be the case if the applet does all its own rendering using the Java graphics libraries (such as with graphing or gaming) and/or uses only AWT components.

Previous page: Importing Classes and Packages for Applets
Next page: The Life Cycle of an Applet