Page 720 - Introduction to Programming with Java: A Problem Solving Approach
P. 720
686 Chapter 16 GUI Programming Basics Displaying an Image
It’s now time to see how the SmileyPanel class draws its images. At the top of the class, the SMILEY and SCARED named constants are initialized as follows:
final private ImageIcon SMILEY = new ImageIcon("smiley.gif");
final private ImageIcon SCARED = new ImageIcon("scared.gif");
The ImageIcon constructor creates an image object from its passed-in filename parameter. So in the above code fragment, two image objects are created from the smiley.gif and scared.gif files, respectively.7
In the SmileyPanel constructor, the mousePressed event handler, and the mouseReleased event handler, note how SMILEY and SCARED get assigned into the image instance variable. Those as- signments are what cause the image to change when the user presses the mouse button and releases it.
The JPanel class has a paintComponent method that’s in charge of drawing Swing components (e.g., text boxes and buttons) within the JPanel container. But it doesn’t handle drawing lines, shapes, or images. To draw those things, you need to provide an overriding paintComponent method with calls to graphics methods. For example, here is SmileyPanel’s overriding paintComponent method:
public void paintComponent(Graphics g)
{
super.paintComponent(g);
image.paintIcon(this, g,
(int) imageCorner.getX(), (int) imageCorner.getY());
} // end paintComponeAntpago PDF Enhancer
Note the paintComponent method’s g parameter. It’s a Graphics object, and it’s used to call graph- ics methods within the paintComponent method. For example, the image.paintIcon method call draws image (a smiley face or a scared face), and it requires a Graphics object, g, for its second argu- ment. In calling the paintIcon method, you provide three arguments in addition to the Graphics argu- ment: (1) the window in which the image is displayed (in the above example, the this reference refers to the JPanel’s window, (2) the x coordinate of the image’s top-left corner, and (3) the y coordinate of the image’s top-left corner. Note the super.paintComponent(g) method call. You should always include that call as the first statement within an overriding paintComponent method. Without it, the background for paintComponent’s associated object might be displayed improperly.
Notice that there’s no explicit call to the DragSmiley program’s paintComponent method. You should never call the paintComponent method directly. Instead, you should call the repaint method and let the repaint method call the paintComponent method for you. The repaint method waits until the program’s window is properly prepared to handle the paintComponent method. Note in the SmileyPanel class how repaint is called at the bottom of the three event handlers. That’s where there’s a need to redraw the image. By the way, in addition to calling paintComponent whenever repaint is called, the JVM calls paintComponent automatically when the program starts up and whenever a user does something to alter the program’s window (e.g., when the user resizes the window, or moves another window off of the window).
7
gif stands for Graphics Interchange Format. It’s used for an exact representation of a simple drawn image.