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

1    package j2d.examples; 
2     
3    import java.awt.BorderLayout; 
4    import java.awt.GridLayout; 
5    import java.awt.event.ActionEvent; 
6    import java.awt.event.ActionListener; 
7    import java.awt.image.renderable.ParameterBlock; 
8     
9    import javax.media.jai.BorderExtender; 
10   import javax.media.jai.BorderExtenderConstant; 
11   import javax.media.jai.JAI; 
12   import javax.media.jai.PlanarImage; 
13   import javax.swing.JComboBox; 
14   import javax.swing.JFrame; 
15   import javax.swing.JLabel; 
16   import javax.swing.JPanel; 
17   import javax.swing.JScrollPane; 
18   import javax.swing.JSpinner; 
19   import javax.swing.SpinnerModel; 
20   import javax.swing.SpinnerNumberModel; 
21   import javax.swing.event.ChangeEvent; 
22   import javax.swing.event.ChangeListener; 
23    
24   import com.sun.media.jai.widget.DisplayJAI; 
25   import gui.ClosableJFrame; 
26    
27   /** 
28    * This class demonstrates the use of the border operator. The user can 
29    * interactively choose the image border dimensions and the border type. 
30    */ 
31   public class BorderFrame extends 
32           ClosableJFrame implements ChangeListener, ActionListener { 
33       // The component and original image. 
34       private DisplayJAI display; 
35       private PlanarImage image; 
36    
37       // A JComboBox for the existing border types. 
38       private JComboBox borderTypes; 
39       private String[] borderTypesLabels = 
40               {"Zero", "Constant", "Copy", "Reflection", "Wrap"}; 
41    
42       // JSpinners for the four border dimensions. 
43       private JSpinner top, left, right, bottom; 
44    
45       /** 
46        * The constructor for the class, which sets its GUI. 
47        * 
48        * @param image the image which will receive a border and be displayed. 
49        */ 
50       public BorderFrame(PlanarImage image) { 
51           super("BorderFrame operator example"); 
52           this.image = image; 
53           // Create a control panel for the combo box and spinners. 
54           JPanel controlPanel = new JPanel(new GridLayout(5, 2)); 
55           // Add the border type combo box. 
56           borderTypes = new JComboBox(borderTypesLabels); 
57           borderTypes.addActionListener(this); 
58           controlPanel.add(new JLabel("BorderFrame Type")); 
59           controlPanel.add(borderTypes); 
60           // Add the four sides spinners. We need four instances of SpinnerModel... 
61           SpinnerModel modelT = new SpinnerNumberModel(10, 0, 100, 5); 
62           top = new JSpinner(modelT); 
63           top.addChangeListener(this); 
64           controlPanel.add(new JLabel("Top border")); 
65           controlPanel.add(top); 
66           SpinnerModel modelB = new SpinnerNumberModel(10, 0, 100, 5); 
67           bottom = new JSpinner(modelB); 
68           bottom.addChangeListener(this); 
69           controlPanel.add(new JLabel("Bottom border")); 
70           controlPanel.add(bottom); 
71           SpinnerModel modelL = new SpinnerNumberModel(10, 0, 100, 5); 
72           left = new JSpinner(modelL); 
73           left.addChangeListener(this); 
74           controlPanel.add(new JLabel("Left border")); 
75           controlPanel.add(left); 
76           SpinnerModel modelR = new SpinnerNumberModel(10, 0, 100, 5); 
77           right = new JSpinner(modelR); 
78           right.addChangeListener(this); 
79           controlPanel.add(new JLabel("Right border")); 
80           controlPanel.add(right); 
81           // Small trick to make the control panel look better. 
82           JPanel positionControlPanel = new JPanel(new BorderLayout()); 
83           positionControlPanel.add(controlPanel, BorderLayout.NORTH); 
84           // Create the display component. 
85           display = new DisplayJAI(image); // Temporarily uses the original image. 
86           resetImage(); // Now that the instance of DisplayJAI is created, set 
87           // an image with borders. 
88           // Add to the JFrame's ContentPane the display and the control panel. 
89           getContentPane().add(new JScrollPane(display), BorderLayout.CENTER); 
90           getContentPane().add(positionControlPanel, BorderLayout.EAST); 
91           // Set the closing operation so the application is finished. 
92           setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
93           pack(); // adjust the frame size using preferred dimensions. 
94           show(); // show the frame. 
95       } 
96    
97       /** 
98        * This method will be called when the spinners' contents change. 
99        */ 
100      public void stateChanged(ChangeEvent e) { 
101          resetImage(); 
102      } 
103   
104      /** 
105       * This method will be called when the combo box contents change. 
106       */ 
107      public void actionPerformed(ActionEvent e) { 
108          resetImage(); 
109      } 
110   
111      /** 
112       * This method will create a border around the image with the values from 
113       * the spinners and combo box. The new image will be used for displaying. 
114       */ 
115      private void resetImage() { 
116          // Gets values from the spinners and combo box. 
117          int topValue = Integer.parseInt(top.getValue().toString()); 
118          int bottomValue = Integer.parseInt(bottom.getValue().toString()); 
119          int leftValue = Integer.parseInt(left.getValue().toString()); 
120          int rightValue = Integer.parseInt(right.getValue().toString()); 
121          int borderType = borderTypes.getSelectedIndex(); 
122          // Create a ParameterBlock for the border operation. 
123          ParameterBlock pb = new ParameterBlock(); 
124          pb.addSource(image); 
125          pb.add(new Integer(leftValue)); 
126          pb.add(new Integer(rightValue)); 
127          pb.add(new Integer(topValue)); 
128          pb.add(new Integer(bottomValue)); 
129          switch (borderType) { 
130              case 0: 
131                  pb.add(BorderExtender.createInstance(BorderExtender.BORDER_ZERO)); 
132                  break; 
133              case 1: // Construction of constant borders is different. 
134                  pb.add(new BorderExtenderConstant(new double[]{255., 0., 180.})); 
135                  break; 
136              case 2: 
137                  pb.add(BorderExtender.createInstance(BorderExtender.BORDER_COPY)); 
138                  break; 
139              case 3: 
140                  pb.add(BorderExtender.createInstance(BorderExtender.BORDER_REFLECT)); 
141                  break; 
142              case 4: 
143                  pb.add(BorderExtender.createInstance(BorderExtender.BORDER_WRAP)); 
144                  break; 
145          } 
146          PlanarImage borderImage = JAI.create("border", pb); 
147          // Oops, the border will be set "outside" the original image extents, 
148          // which will cause problems for the display component. Let's reposition 
149          // it so the image origin is back at (0,0). 
150          pb = new ParameterBlock(); 
151          pb.addSource(borderImage); 
152          pb.add(1.0f * leftValue); 
153          pb.add(1.0f * topValue); 
154          // Create the output image by translating itself. 
155          borderImage = JAI.create("translate", pb, null); 
156          display.set(borderImage); 
157      } 
158   
159      /** 
160       * The application entry point. 
161       * 
162       * @param args the command line arguments. We need one arguments, the image 
163       *             file name. 
164       */ 
165      public static void main(String[] args) { 
166   
167          // Read the image. 
168          PlanarImage input = JAI.create("fileload", 
169                  futils.Futil.getReadFileName("select an image")); 
170          // Create an instance of the JFrame. 
171          new BorderFrame(input); 
172      } 
173   
174  } 
175