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

1    package j2d.filters; 
2     
3    import ip.transforms.BufferedImageProcessor; 
4     
5    import javax.swing.*; 
6    import java.awt.*; 
7    import java.awt.event.ActionEvent; 
8    import java.awt.event.ActionListener; 
9     
10   import j2d.filters.BlurFilter; 
11   import j2d.filters.ColorFilter; 
12   import j2d.filters.ImagePanel; 
13   import j2d.filters.InvertFilter; 
14    
15   public class Main extends JFrame { 
16    
17       private JMenu filterMenu; 
18       private ImagePanel imagePanel = new ImagePanel( 
19               futils.Futil.getReadFile("select an image")); 
20    
21       // image filters 
22       private BufferedImageProcessor invertFilter = new InvertFilter(); 
23       private BufferedImageProcessor sharpenFilter = new SharpenFilter(); 
24       private BufferedImageProcessor blurFilter = new BlurFilter(); 
25       private BufferedImageProcessor colorFilter = new ColorFilter(); 
26    
27    
28       // initialize JMenuItems 
29       public Main() { 
30           super("Java 2D Image Processing Demo"); 
31    
32           // initialize ImagePanel 
33           initGui(); 
34    
35       } // end Java2DExample constructor 
36    
37       private void initGui() { 
38    
39           // create JMenuBar 
40           JMenuBar menuBar = new JMenuBar(); 
41           setJMenuBar(menuBar); 
42    
43           // create JMenu 
44           filterMenu = new JMenu("Image Filters"); 
45           filterMenu.setMnemonic('I'); 
46    
47           // create JMenuItem for displaying orginal Image 
48           JMenuItem originalMenuItem = 
49                   new JMenuItem("Display Original"); 
50           originalMenuItem.setMnemonic('O'); 
51    
52           originalMenuItem.addActionListener( 
53                   new ActionListener() { 
54    
55                       // show original Image 
56                       public void actionPerformed(ActionEvent action) { 
57                           imagePanel.displayOriginalImage(); 
58                       } 
59    
60                   } // end anonymous inner class 
61           ); 
62    
63           // create JMenuItems for Java2DImageFilter 
64           JMenuItem invertMenuItem = createMenuItem( 
65                   "Invert", 'I', invertFilter); 
66           JMenuItem sharpenMenuItem = createMenuItem( 
67                   "Sharpen", 'S', sharpenFilter); 
68           JMenuItem blurMenuItem = createMenuItem( 
69                   "Blur", 'B', blurFilter); 
70           JMenuItem changeColorsMenuItem = createMenuItem( 
71                   "Change Colors", 'C', colorFilter); 
72    
73           // add JMenuItems to JMenu 
74           filterMenu.add(originalMenuItem); 
75           filterMenu.add(invertMenuItem); 
76           filterMenu.add(sharpenMenuItem); 
77           filterMenu.add(blurMenuItem); 
78           filterMenu.add(changeColorsMenuItem); 
79    
80           // add JMenu to JMenuBar 
81           menuBar.add(filterMenu); 
82    
83           Container c = getContentPane(); 
84           c.add(imagePanel, BorderLayout.CENTER); 
85       } 
86    
87    
88       // create JMenuItem and ActionListener for given filter 
89       public JMenuItem createMenuItem(String menuItemName, 
90                                       char mnemonic, 
91                                       final BufferedImageProcessor filter) { 
92           // create JMenuItem 
93           JMenuItem menuItem = new JMenuItem(menuItemName); 
94    
95           // set Mnemonic 
96           menuItem.setMnemonic(mnemonic); 
97    
98           menuItem.addActionListener( 
99                   new ActionListener() { 
100   
101                      // apply Java2DImageFilter when MenuItem accessed 
102                      public void actionPerformed(ActionEvent action) { 
103                          imagePanel.applyFilter(filter); 
104                      } 
105   
106                  } // end anonymous inner class 
107          ); 
108   
109          return menuItem; 
110   
111      } // end method createMenuItem 
112   
113      // start program 
114      public static void main(String args[]) { 
115          Main m = new Main(); 
116          m.setDefaultCloseOperation(EXIT_ON_CLOSE); 
117          m.pack(); 
118          m.setVisible(true); 
119      } 
120  } 
121   
122   
123