Standard Applet Methods

The first step in the creation of an applet is to make it a subclass of JApplet, a class that's part of the Swing package, javax.swing. An applet is treated as a visual window inside a web page, so JApplet is part of Swing alongside clickable buttons, scrollbars, and other components of a program's user interface. JApplet is a subclass of Applet, a class in the java.applet package. Being part of this hierarchy enables the applets you write to use all the behavior and attributes they need to be run as part of a web page. Before you begin writing any other statements in your applets, they will be able to interact with a web browser, load and unload themselves, redraw their window in response to changes in the browser window, and handle other necessary tasks. In apps, programs begin running with the first statement inside the main() block statement and end with the last closing bracket (}) that closes out the block. There is no main() method in a Java applet, so there is no set starting place for the program. Instead, an applet has a group of standard methods that are handled in response to specific events as the applet runs. The following are the events that could prompt one of the applet methods to be handled:

  • The program is loaded for the first time, which causes the applet's init() and start() methods to be called.
  • Something happens that requires the applet window to be redisplayed, which causes the applet's paint() method to be called.
  • The program is stopped by the browser, which calls the applet's stop() method.
  • The program restarts after a stop, which calls the start() method.
  • The program is unloaded as it finishes running, which calls the destroy() method.

The following is an example of a bare-bones applet:

public class Skeleton extends javax.swing.JApplet {
 // program will go here

Unlike apps, applet class files must be public because the JApplet class is also public. (If your applet uses other class files of your own creation, they do not have to be declared public.) Your applet's class inherits all the methods that are handled automatically when needed: init(), paint(), start(), stop(), and destroy(). However, none of these methods do anything. If you want something to happen in an applet, you have to override these methods with new versions in your applet program.

Painting an Applet Window

The paint() method is used to display text, shapes, and graphics within the applet window. Whenever something needs to be displayed or redisplayed on the applet window, the paint() method handles the task. You also can force paint() to be handled with the following statement in any method of an applet:


Aside from the use of repaint(), the main time the paint() method is handled is when something changes in the browser or the operating system running the browser. For example, if a user minimizes a web page containing an applet, the paint() method will be called to redisplay everything that was onscreen in the applet when the applet is later restored to full size. Unlike the other methods you will be learning about during this hour, paint() takes an argument. The following is an example of a simple paint() method:

public void paint(Graphics screen) {
 Graphics2D screen2D = (Graphics2D)screen;
 // display statements go here

The argument sent to the paint() method is a Graphics object. The Graphics class of objects represents an environment in which something can be displayed, such as an applet window. As you did with Swing programs, you may cast this to a Graphics2D object to employ more sophisticated graphical features. Later this hour, you'll learn about drawString(), a method for the display of text that's available in both the Graphics and Graphics2D classes. If you are using a Graphics or Graphics2D object in your applet, you have to add the following import statements before the class statement at the beginning of the source file:

import java.awt.Graphics;
import java.awt.Graphics2D;

Did you Know?

If you are using several classes that are a part of the java.awt package of classes, you can use the statement import java.awt.* to make all of these classes available for use in your program.

Initializing an Applet

The init() method is handled once—and only once—when the applet is run. As a result, it's an ideal place to set up values for any objects and variables that are needed for the applet to run successfully. This method is also a good place to set up fonts, colors, and the screen's background color. Here's an example:

public void init() {
FlowLayout flo = new FlowLayout();
 JButton run = new JButton("Run");

If you are going to use a variable in other methods, it should not be created inside an init() method because it will only exist within the scope of that method. For example, if you create an integer variable called displayRate inside the init() method and try to use it in the paint() method, you'll get an error when you attempt to compile the program. Create any variables you need to use throughout a class as object variables right after the class statement and before any methods.

Starting and Stopping an Applet

At any point when the applet program starts running, the start() method will be handled. When a program first begins, the init() method is followed by the start() method. After that, in many instances there will never be a cause for the start() method to be handled again. In order for start() to be handled a second time or more, the applet has to stop execution at some point. The stop() method is called when an applet stops execution. This event can occur when a user leaves the web page containing the applet and continues to another page. It also can occur when the stop() method is called directly in a program.

Destroying an Applet

The destroy() method is an opposite of sorts to the init() method. It is handled just before an applet completely closes down and completes running. This method is used when something has been changed during a program that should be restored to its original state. It's another method you'll use more often with animation than with other types of programs.