Page 716 - Introduction to Programming with Java: A Problem Solving Approach
P. 716
682 Chapter 16 GUI Programming Basics
classes because they are descendants of the Component class. There’s another component class that’s a bit different. It doesn’t feel like a component in the normal sense of the word, but it’s a Java component none- theless (because it’s a descendant of the Component class), and it works great for handling mouse events. So what is the mystery component? JPanel!
Think of a JPanel object as a generic storage area for other objects. More formally, the JPanel class is a descendant of the Container class, and as such, it’s a container and you can add objects to it. In the next chapter, you’ll add Swing components (JLabel, JTextField, and so on) to JPanel containers. In the upcoming program example, you add an image object to a JPanel container. By surrounding the im- age with a JPanel container, you provide a platform that mouse listeners can attach to. In the upcoming program example, the JPanel listeners allow you to detect mouse events on the image object.
The DragSmiley Program
See Figure 16.16. It contains a driver class and a sample session for a DragSmiley program. As indicated in the sample session, the program initially displays a smiley face in the top-left corner of the program’s win- dow. If the user presses the mouse button, the smiley image changes to a scared image (presumably because the smiley is apprehensive of what the user might do to it). When the user releases the mouse button, the scared image changes back to the smiley image. If the mouse cursor resides on the image and the user drags the mouse, the image follows the mouse cursor.
Study Figure 16.16’s DragSmiley constructor. In it, the following two statements instantiate a JPanel container named smileyPanel and add the JPanel container to DragSmiley’s window.
Apago PDF Enhancer
smileyPanel = new SmileyPanel();
add(smileyPanel);
See the SmileyPanel class in Figures 16.17a and 16.17b. The SmileyPanel class is where the bulk of the program’s logic is. We’ll describe the SmileyPanel class by first focusing on the listeners. Note how the SmileyPanel constructor creates the mouse listeners and adds them to the JPanel container. Note the mouse listener class headings, repeated here for your convenience:
private class ClickListener extends MouseAdapter
private class DragListener extends MouseMotionAdapter
The extends clauses indicate inheritance from the MouseAdapter and MouseMotionAdapter classes. For each event handling interface with more than one method, Sun provides an associated class that already implements the interface’s methods for you. Those classes are called adapter classes. The MouseAdapter class implements the MouseListener interface’s methods, and the MouseMotionAdapter class im- plements the MouseMotionListener interface’s methods. Adapter classes don’t do much. They simply implement their associated interface’s methods as dummy methods, like this:
public void mousePressed(MouseEvent event)
{}
To implement a listener that detects the mouse being pressed, you extend the MouseAdapter class and provide an overriding mousePressed method. For an example, see Figure 16.17a. As an alternative, you can implement a listener using an interface rather than an adapter. But remember that an interface is a con- tract, and when you implement an interface, you’re required to provide methods for all of the interface’s methods. So if you wanted to replace the SmileyPanel class’s adapters with interfaces, you’d have to provide dummy methods for the methods that you don’t use.