Sams Teach Yourself Java 2 in 24 Hours

Sams Teach Yourself Java 2 in 24 Hours

By Rogers Cadenhead

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 World Wide 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 applications, 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 following is an example of a bare-bones applet:

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

Unlike applications, 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 can also force paint() to be handled with the following statement in any method of an applet:

repaint(); 

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. There's also another version of this class, Graphics2D, which supports more sophisticated graphical features.

In the preceding example, a Graphics2D object called screen2D is created. Instead of calling a constructor method to create the object, a technique called casting is used.

Casting is the process of creating a Java object based on an existing object. In this example, a Graphics object called screen is cast to a Graphics2D object called screen2D. To take advantage of the features of Graphics2D in an applet window, you must cast the Graphics object sent to the paint() method into a Graphics2D object.

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; 

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() { 
    Container pane = getContentPane(); 
    FlowLayout flo = new FlowLayout(); 
    pane.setLayout(flo); 
    JButton run = new JButton("Run"); 
    pane.add(run); 
    setContentPane(pane); 
} 

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 can also 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 in rare instances when something has been changed during a program and it should be restored to its original state. It's another method you'll use more often with animation than with other types of programs.

Share ThisShare This

Informit Network