The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Deployment
Lesson: Applets

Getting Started with Applets

Below is the HelloWorld applet, which is a simple Java class that prints the string "Hello World" in small rectangle.

If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the JRE or JDK (outside of the tutorial).

We strongly recommend that you install the latest version; at least 1.4.2 is required for all our applets. You can find more information in Troubleshooting Applet Problems (in the Deployment trail)

Following is the source code for the HelloWorld applet:

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);
    }
}

An applet such as this is typically managed and run by Java Plug-in. Java Plug-in, which is automatically included when you download the Java Runtime Environment (JRE), extends the functionality of a web browser, allowing applets to be run under Sun's Java 2 runtime environment (JRE) rather than the Java runtime environment that comes with the web browser. It works with the Mozilla family of browsers and with Internet Explorer.

The rest of this section contains the following to help you get started with applets:

Importing Classes and Packages for Applets

The code above starts off with two import statements, so you can reference the classes needed to build the applet.

Defining an Applet Subclass

Every applet must define a subclass of the Applet or JApplet class. In the "Hello World" applet, this subclass is called HelloWorld. Applets inherit a great deal of functionality from the Applet or JApplet class, ranging from communication with the browser to the ability to present a graphical user interface (GUI).

The Life Cycle of an Applet

The HelloWorld applet implements just one method, the paint method. Every applet must implement at least one of the following methods: init, start, or paint. This section introduces a new applet, Simple, that uses all of these methods. Unlike Java applications, applets do not need to implement a main method.

Methods for Milestones

The JApplet class provides a framework for applet execution, defining methods that the system calls when milestones -- major events in an applet's life cycle -- occur. Most applets override some or all of these methods to respond appropriately to milestones.

Methods for Drawing and Event Handling

Applets inherit the drawing and event handling methods of the AWT Component class. Drawing refers to anything related to representing an applet on-screen -- drawing images, presenting user interface components such as buttons, or using graphics primitives. Event handling refers to detecting and processing user input such as mouse clicks and key presses, as well as more abstract events such as saving files and iconifying windows.

Methods for Adding UI Components

Applets inherit from the AWT Container class. This means that they are designed to hold Components -- user interface objects such as buttons, labels, pop-up lists, and scrollbars. Like other Containers, applets use layout managers to control the positioning of Components.

What Applets Can and Can't Do

For security reasons, applets that are loaded over the network have several restrictions. One is that an applet can't ordinarily read or write files on the computer that it's executing on. Another is that an applet can't make network connections except to the host that it came from. Despite these restrictions, applets can do some things that you might not expect. For example, applets can invoke the public methods of other applets on the same page.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.