13.14. (Optional) Case Study: The ImageViewer Class |
Displaying an image is a common task in Java programming. This case study develops a reusable component named ImageViewer that displays an image on a panel. The class contains the properties image , stretched , xCoordinate , and yCoordinate , with associated accessor and mutator methods , as shown in Figure 13.24.
You can use images in Swing components like JLabel and JButton , but these images are not stretchable. The image in an ImageViewer can be stretched.
Let us write a test program in Listing 13.14 that displays six images using the ImageViewer class. Figure 13.25 shows a sample run of the program.
1 import javax.swing.*; 2 import java.awt.*; 3 4 public class SixFlags extends JFrame { 5 public SixFlags() { 6 Image image1 = new ImageIcon( "image/us.gif" ).getImage(); 7 Image image2 = new ImageIcon( "image/ca.gif" ).getImage(); 8 Image image3 = new ImageIcon( "image/india.gif" ).getImage(); 9 Image image4 = new ImageIcon( "image/uk.gif" ).getImage(); 10 Image image5 = new ImageIcon( "image/china.gif" ).getImage(); 11 Image image6 = new ImageIcon( "image/norway.gif" ).getImage(); 12 13 setLayout( new GridLayout( 2 , , 5 , 5 )); 14 add( new ImageViewer(image1)); 15 add( new ImageViewer(image2)); 16 add( new ImageViewer(image3)); 17 add( new ImageViewer(image4)); 18 add( new ImageViewer(image5)); 19 add( new ImageViewer(image6)); 20 } 21 22 public static void main(String[] args) { 23 SixFlags frame = new SixFlags(); 24 frame.setTitle( "SixFlags" ); 25 frame.setLocationRelativeTo( null ); // Center the frame 26 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 27 frame.setSize( 400 , 320 ); 28 frame.setVisible( true ); 29 } 30 } |
The ImageViewer class is implemented in Listing 13.15. ( Note: You may skip the implementation. ) The accessor and mutator methods for the properties image , stretched , xCoordinate , and yCoordinate are easy to implement. The paintComponent method (lines 26 “35) displays the image on the panel. Line 29 ensures that the image is not null before displaying it. Line 30 checks whether the image is stretched or not.
1 import java.awt.*; 2 import javax.swing.*; 3 4 public class ImageViewer extends JPanel { 5 /** Hold value of property image. */ 6 private java.awt.Image image; 7 8 /** Hold value of property stretched. */ 9 private boolean stretched = true ; 10 11 /** Hold value of property xCoordinate. */ 12 private int xCoordinate; 13 14 /** Hold value of property yCoordinate. */ 15 private int yCoordinate; 16 17 /** Construct an empty image viewer */ 18 public ImageViewer() { 19 } 20 21 /** Construct an image viewer for a specified Image object */ 22 public ImageViewer(Image image) { 23 this .image = image; 24 } 25 26 protected void paintComponent(Graphics g) { 27 super .paintComponent(g); 28 29 if (image != null ) 30 if ( isStretched() ) 31 g.drawImage(image, xCoordinate, yCoordinate, 32 getSize().width, getSize().height, this ); 33 else 34 g.drawImage(image, xCoordinate, yCoordinate, this ); 35 } 36 37 /** Return value of property image */ 38 public java.awt.Image getImage() { 39 return image; 40 } 41 42 /** Set a new value for property image */ 43 public void setImage(java.awt.Image image) { 44 this .image = image; 45 repaint(); 46 } 47 48 /** Return value of property stretched */ 49 public boolean isStretched() { 50 return stretched; 51 } 52 53 /** Set a new value for property stretched */ 54 public void setStretched( boolean stretched) { 55 this .stretched = stretched; 56 repaint(); 57 } 58 59 /** Return value of property xCoordinate */ 60 public int getXCoordinate() { 61 return xCoordinate; 62 } 63 64 /** Set a new value for property xCoordinate */ 65 public void setXCoordinate( int xCoordinate) { 66 this .xCoordinate = xCoordinate; 67 repaint(); 68 } 69 70 /** Return value of property yCoordinate */ 71 public int getYCoordinate() { 72 return yCoordinate; 73 } 74 75 /** Set a new value for property yCoordinate */ 76 public void setYCoordinate( int yCoordinate) { 77 this .yCoordinate = yCoordinate; 78 repaint(); 79 } 80 } |