Trail: Deployment
Lesson: Applets
Getting Started with Applets
Home Page > Deployment > 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.


Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the Java(TM) SE JRE or JDK. This applet requires JDK 1.4 or later. You can find more information on the Java Plug-in home page.

We strongly recommend that you install the latest version; release 1.4 or later is required for all our applets. You can find more information in Troubleshooting Applet Problems

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(TM) SE Runtime Environment (JRE), extends the functionality of a web browser, allowing applets to be run under Sun's Java(TM) SE 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.

Converting Applications to Applets

An application is a standalone program consisting of at least one class with a main method. Applets differ significantly from applications. First, applets do not have a main method that is automatically called to begin the program. Instead, several methods are called at different points in the execution of an applet. The difference between Java applets and applications lies in how they are run. Applications are usually run by loading the application's main class file with a Java interpreter, such as the java tool in the JDK(TM) 6.

Basic steps to follow to convert an application program into an applet program:

  • You need to create a subclass of java.applet.Applet in which you override the init method to initialize your applet's resources the same way the main method initializes the application's resources.
  • init might be called more than once and should be designed accordingly. Moreover, the top-level Panel needs to be added to the applet in init; usually it was added to a Frame in main. That's it!

    You may understand clearly as to how you can convert an application program into an applet, by going through a sample application program SwingUI.java here and its corresponding applet program ApptoAppl.java

    When you compare these two programs, you may come up with the following major differences between the two:

  • The applet class is declared public so appletviewer can access it.
  • The applet class descends from Applet/JApplet and the application class descends from Frame/JFrame.
  • The applet version has no main method.
  • The application constructor is replaced in the applet by start and init methods.
  • GUI components are added directly to the Applet; whereas, in the case of an application, GUI components are added to the content pane of its JFrame object.

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

  • Extending Applet or JApplet

    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, including abilities to communicate with the browser and present a graphical user interface (GUI) to the user.

    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: Applets
    Next page: Importing Classes and Packages for Applets