/Users/lyon/j4p/src/j2d/examples/DisplayImage.java

1    package j2d.examples; 
2     
3    import futils.Futil; 
4    import graphics.NumImage; 
5    import gui.ClosableJFrame; 
6     
7    import javax.swing.*; 
8    import java.awt.*; 
9    import java.io.File; 
10   import java.io.FileNotFoundException; 
11    
12   import j2d.ImageUtils; 
13    
14   /** 
15    * DocJava, Inc. 
16    * http://www.docjava.com 
17    * Programmer: dlyon 
18    * Date: Mar 22, 2004 
19    * Time: 2:22:10 PM 
20    */ 
21   public class DisplayImage { 
22       public static void main(String[] args) { 
23           Image img = NumImage.getImage(); 
24           displayImage(img); 
25       } 
26    
27       public static void displayImage(Image img) { 
28           ClosableJFrame cf = new ClosableJFrame(); 
29           Container c = cf.getContentPane(); 
30           c.add(getImagePanel(img)); 
31           c.setLayout(new GridLayout(1, 0)); 
32           cf.setSize(200, 200); 
33           cf.show(); 
34       } 
35    
36       public static JPanel getImagePanel(final Image img) { 
37           return new JPanel() { 
38               public Dimension getPreferredSize() { 
39                   return new Dimension(img.getHeight(this), 
40                           img.getWidth(this)); 
41               } 
42    
43               public void paint(Graphics g) { 
44                   Dimension d = getSize(); 
45                   int w = d.width; 
46                   int h = d.height; 
47                   g.drawImage(img, 0, 0, w, h, this); 
48               } 
49           }; 
50       } 
51    
52       public static Image getImage() throws FileNotFoundException { 
53           File f = Futil.getReadFile("enter a gif or jpeg"); 
54           return ImageUtils.getImage(f); 
55       } 
56    
57   } 
58