/Users/lyon/j4p/src/j2d/filters/ImagePanel.java

1    // ImagePanel.java 
2    // ImagePanel contains an image for display.  The image is 
3    // converted to a BufferedImage for filtering purposes. 
4    package j2d.filters; 
5     
6    // Java core packages 
7     
8    import j2d.ImageUtils; 
9    import ip.transforms.BufferedImageProcessor; 
10    
11   import javax.swing.*; 
12   import java.awt.*; 
13   import java.awt.image.BufferedImage; 
14   import java.io.File; 
15    
16   public class ImagePanel extends JPanel { 
17    
18       private BufferedImage bufferedDisplayImage; // filtered image 
19       private BufferedImage bufferedOriginalImageCopy; // original image copy 
20       private Image image; // image to load 
21    
22       // ImagePanel constructor 
23       public ImagePanel(File f) { 
24           setImage(ImageUtils.getGifJpgPngImage(f)); 
25    
26           ImageUtils.waitForImage(this, getImage()); 
27    
28           // create BufferedImage from Image 
29           setBufferedOriginalImageCopy(makeBlankBufferedImage(getImage().getWidth(null), getImage().getHeight(null))); 
30    
31           //displayImage = new BufferedImage( image.getWidth( null ), 
32           //   image.getHeight( null ), BufferedImage.TYPE_INT_RGB ); 
33    
34           setBufferedDisplayImage(getBufferedOriginalImageCopy()); 
35    
36           // draw Image on Graphics2D object 
37           Graphics2D graphics = getBufferedDisplayImage().createGraphics(); 
38           graphics.drawImage(getImage(), null, null); 
39    
40       } // end ImagePanel constructor 
41    
42       public static BufferedImage makeBlankBufferedImage(int w, int h) { 
43           return new BufferedImage(w, 
44                   h, BufferedImage.TYPE_INT_RGB); 
45       } 
46    
47       // apply Java2DImageFilter to Image 
48       public void applyFilter(BufferedImageProcessor filter) { 
49           // process Image using ImageFilter 
50           setBufferedDisplayImage(filter.process(getBufferedDisplayImage())); 
51           repaint(); 
52       } 
53    
54       // set Image to originalImage 
55       public void displayOriginalImage() { 
56           setBufferedDisplayImage(makeBlankBufferedImage(getImage().getWidth(this), getImage().getHeight(this))); 
57    
58           Graphics2D graphics = getBufferedDisplayImage().createGraphics(); 
59           graphics.drawImage(getBufferedOriginalImageCopy(), null, null); 
60           repaint(); 
61       } 
62    
63       // draw ImagePanel 
64       public void paintComponent(Graphics g) { 
65           super.paintComponent(g); 
66           Graphics2D graphics = (Graphics2D) g; 
67           graphics.drawImage(getBufferedDisplayImage(), 0, 0, null); 
68       } 
69    
70       // get preferred ImagePanel size 
71       public Dimension getPreferredSize() { 
72           return new Dimension(getBufferedDisplayImage().getWidth(), 
73                   getBufferedDisplayImage().getHeight()); 
74       } 
75    
76       // get minimum ImagePanel size 
77       public Dimension getMinimumSize() { 
78           return getPreferredSize(); 
79       } 
80    
81       public BufferedImage getBufferedDisplayImage() { 
82           return bufferedDisplayImage; 
83       } 
84    
85       public void setBufferedDisplayImage(BufferedImage bufferedDisplayImage) { 
86           this.bufferedDisplayImage = bufferedDisplayImage; 
87       } 
88    
89       public BufferedImage getBufferedOriginalImageCopy() { 
90           return bufferedOriginalImageCopy; 
91       } 
92    
93       public void setBufferedOriginalImageCopy(BufferedImage bufferedOriginalImageCopy) { 
94           this.bufferedOriginalImageCopy = bufferedOriginalImageCopy; 
95       } 
96    
97       public Image getImage() { 
98           return image; 
99       } 
100   
101      public void setImage(Image image) { 
102          this.image = image; 
103      } 
104  } 
105   
106   
107