13.7. Case Study - The FigurePanel Class

 
[Page 418 ( continued )]

12.10. Image Icons

Java uses the javax.swing.ImageIcon class to represent an icon. An icon is a fixed- size picture; typically it is small and used to decorate components . Images are normally stored in image files. You can use new ImageIcon(filename) to construct an image icon. For example, the following statement creates an icon from an image file us.gif in the image directory under the current class path :

 ImageIcon icon =   new   ImageIcon(   "image/us.gif"   ); 

Note

" image/us.gif " is located in " c:\book\image\us.gif. " The back slash ( \ ) is the Windows file path notation. On Unix, the forward slash ( / ) should be used. In Java, the forward slash ( / ) is used to denote a relative file path under the Java classpath (e.g., image/left.gif , as in this example).


Note

Java currently supports three image formats: GIF (Graphics Interchange Format), JPEG (Joint Photographic Experts Group), and PNG (Portable Network Graphics). The image filenames for these types end with .gif, .jpg, and .png, respectively. If you have a bitmap file or image files in other formats, you can use image-processing utilities to convert them into GIF, JPEG, or PNG format for use in Java.


Note

File names are not case sensitive on Windows, but are case sensitive on Unix. To enable your programs to run on all platforms, name all the image files consistently, using lowercase.


An image icon can be displayed in a label or a button using new JLabel(imageIcon) or new JButton(imageIcon) . Listing 12.8 demonstrates displaying icons in a label and a button. The example creates two labels and two buttons with icons, as shown in Figure 12.15.

Figure 12.15. The image icons are displayed in labels and buttons.


Listing 12.8. TestImageIcon.java
(This item is displayed on pages 418 - 419 in the print version)
 1   import   javax.swing.*; 2   import   java.awt.*; 3 4   public class   TestImageIcon   extends   JFrame { 5    private   ImageIcon usIcon =   new   ImageIcon(   "image/us.gif"   );  6   private   ImageIcon myIcon =   new   ImageIcon(   "image/my.jpg"   ); 

[Page 419]
 7   private   ImageIcon frIcon =   new   ImageIcon(   "image/fr.gif"   ); 8   private   ImageIcon ukIcon =   new   ImageIcon(   "image/uk.gif"   ); 9 10   public   TestImageIcon() { 11 setLayout(   new   GridLayout(   1   ,   4   ,   5   ,   5   )); 12  add(   new   JLabel(usIcon));  13 add(   new   JLabel(myIcon)); 14  add(   new   JButton(frIcon));  15 add(   new   JButton(ukIcon)); 16 } 17 18  /** Main method */  19   public static void   main(String[] args) { 20 TestImageIcon frame =   new   TestImageIcon(); 21 frame.setTitle(   "TestImageIcon"   ); 22 frame.setLocationRelativeTo(   null   );  // Center the frame  23 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 24 frame.setSize(   200   ,   200   ); 25 frame.setVisible(   true   ); 26 } 27 } 

Note

GUI components cannot be shared by containers because a GUI component can appear in only one container at a time. Therefore, the relationship between a component and a container is the composition denoted by a solid diamond, as shown in Figure 12.2.


Note

Borders and icons can be shared. Thus you can create a border or icon and use it to set the border or icon property for any GUI component. For example, the following statements set a border b for two panels p1 and p2 :

 p1.setBorder(b); p2.setBorder(b); 

The following statements set an icon in two buttons jbt1 and jbt2 :

 jbt1.setIcon(icon); jbt2.setIcon(icon); 


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net