The first step in the creation of an applet is to make it a subclass of JApplet
, a class in the Swing package javax.swing
. An applet is treated as a visual window inside a web page, so JApplet
is part of Swing alongside buttons, scrollbars, and other components of a program’s user interface.
The applets you write inherit 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 are able to interact with a web browser, load and unload themselves, redraw their windows in response to changes in the browser window, and handle other necessary tasks.
Applications begin running with the first statement inside the main()
block. There is no main()
method in a Java applet, so there’s 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 start()
.
• The program is unloaded as it finishes running, which calls destroy()
.
The following is an example of a bare-bones applet:
public class Skeleton extends javax.swing.JApplet {
// program will go here
}
Applet class files must be public
because the JApplet
class also is 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, you must override these methods with new versions in your applet.
The paint()
method is used to display text and graphics within the applet window. Whenever something needs to be displayed or redisplayed in the applet, the paint()
method handles the task. You also can force paint()
to be called by using the following statement in an applet:
repaint();
The main time the paint()
method is called is when something changes in the browser or the operating system running the browser. For example, if a user closes a window of another program that was in front of the browser, the paint()
method is called to redisplay everything in the applet.
The paint()
method takes a single argument, a Graphics
object from the java.awt
package:
public void paint(Graphics screen) {
Graphics2D screen2D = (Graphics2D) screen;
// display statements go here
}
The Graphics
class represents a graphical context, an environment in which something can be displayed. As you did with Swing applications, you should cast this to a Graphics2D
object from the java.awt
package to use Swing’s graphical capabilities.
Later this hour, you learn about drawString()
, a method for displaying text in the Graphics2D
classes.
If you are using a Graphics
or Graphics2D
object in your applet, you should add the following import
statements before the class
statement at the beginning of the source file:
import java.awt.Graphics;
import java.awt.Graphics2D;
Alternatively, you can make all java.awt
classes available by using the wildcard character “*”:
import java.awt.*;
The init()
method is called once—and only once—when an applet is run. It’s an ideal place to set up values for objects and variables that are needed for the applet to run successfully. This method also is a good place to set up fonts, colors, and the applet window’s background color. Here’s an example:
public void init() {
FlowLayout flo = new FlowLayout();
setLayout(flo);
JButton run = new JButton("Run");
add(run);
}
If you are going to use a variable in other methods, it should not be created inside an init()
method because it only exists within the scope of that method. Create any variables you need to use throughout a class as instance variables.
When the applet program starts running, the start()
method is called. When a program first begins, the init()
method is followed by the start()
method. After that, the start()
method only is called again if the applet stops execution at some point and is later restarted.
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.
The destroy()
method is the opposite of the init()
method. It is handled just before an applet completely closes down and completes running.
3.15.1.22