/Users/lyon/j4p/src/ip/gui/frames/AffineFrame.java

1    package ip.gui.frames; 
2     
3    import math.Mat3; 
4     
5    import java.awt.*; 
6    import java.awt.event.ActionEvent; 
7    import java.awt.event.MouseEvent; 
8    import java.awt.event.MouseListener; 
9    import java.awt.event.MouseMotionListener; 
10    
11   public class AffineFrame extends ShortCutFrame 
12           implements MouseListener, MouseMotionListener { 
13    
14       XformFrame xf = null; 
15    
16       MenuBar mb = new MenuBar(); 
17    
18       Menu transformMenu = getMenu("Transform"); 
19    
20       Menu pointMenu = getMenu("Point..."); 
21    
22    
23       MenuItem rotate_mi = addMenuItem(transformMenu, "[r]otate"); 
24       MenuItem scale_mi = addMenuItem(transformMenu, "scale - xy"); 
25       MenuItem scalex_mi = addMenuItem(transformMenu, "[X]scale - x"); 
26       MenuItem scaley_mi = addMenuItem(transformMenu, "[Y]scale - y"); 
27       MenuItem scaleRotate_mi = addMenuItem(transformMenu, "[*]scalexy+rotate"); 
28       MenuItem shearx_mi = addMenuItem(transformMenu, "shear x"); 
29       MenuItem sheary_mi = addMenuItem(transformMenu, "[y]shear y"); 
30       MenuItem shearRotate_mi = addMenuItem(transformMenu, "[R]otate+shear"); 
31       MenuItem revert_mi = addMenuItem(transformMenu, "[E-R]evert to saved"); 
32       MenuItem apply_mi = addMenuItem(transformMenu, "[a]pply transform"); 
33       MenuItem applyBilinear4Points_mi = addMenuItem(transformMenu, "[4]applyBilinear4Points"); 
34       MenuItem polarTransform_mi = addMenuItem(transformMenu, "[p]olarTransform"); 
35    
36       MenuItem xformFeedback_mi = addMenuItem(transformMenu, "[f]eedback xform"); 
37       MenuItem zedSquare_mi = addMenuItem(transformMenu, "w=z**2"); 
38       MenuItem sqrt_mi = addMenuItem(transformMenu, "[E-s]qrt"); 
39       MenuItem fishEye_mi = addMenuItem(transformMenu, "Fish Eye"); 
40    
41       MenuItem colorize_mi = addMenuItem(transformMenu, "[c]olorize"); 
42    
43       MenuItem movePoint0_mi = addMenuItem(pointMenu, "[E-0]move point 0"); 
44       MenuItem movePoint1_mi = addMenuItem(pointMenu, "[E-1]move point 1"); 
45       MenuItem movePoint2_mi = addMenuItem(pointMenu, "[E-2]move point 2"); 
46       MenuItem movePoint3_mi = addMenuItem(pointMenu, "[E-3]move point 3"); 
47       MenuItem movePointd_mi = addMenuItem(pointMenu, "[E-d]dont move points"); 
48       MenuItem printPoints_mi = addMenuItem(pointMenu, "print points"); 
49       MenuItem selection = rotate_mi; 
50    
51       private Polygon p = new Polygon(); 
52       Mat3 at; 
53       int x1 = 0; 
54       int y1 = 0; 
55       int width; 
56       int height; 
57       int centroid[] = {width / 2, height / 2}; 
58       int xtranslate = 50; 
59       int ytranslate = 50; 
60    
61       int pointToMove = -1; 
62    
63       public Polygon getPolygon() { 
64           return at.transform(p); 
65       } 
66    
67       public void actionPerformed(ActionEvent e) { 
68    
69           if (match(e, printPoints_mi)) { 
70               printPoints(); 
71               return; 
72           } 
73           if (match(e, fishEye_mi)) { 
74               xf.fishEye(); 
75               return; 
76           } 
77    
78           if (match(e, zedSquare_mi)) { 
79               xf.zedSquare(); 
80               return; 
81           } 
82           if (match(e, colorize_mi)) { 
83               new ColorGridFrame(this); 
84               return; 
85           } 
86           if (match(e, sqrt_mi)) { 
87               xf.sqrt(); 
88               return; 
89           } 
90           if (match(e, polarTransform_mi)) { 
91               xf.polarTransform(); 
92               return; 
93           } 
94           if (match(e, movePointd_mi)) { 
95               pointToMove = -1; 
96               return; 
97           } 
98           if (match(e, movePoint0_mi)) { 
99               pointToMove = 0; 
100              return; 
101          } 
102          if (match(e, movePoint1_mi)) { 
103              pointToMove = 1; 
104              return; 
105          } 
106          if (match(e, movePoint2_mi)) { 
107              pointToMove = 2; 
108              return; 
109          } 
110          if (match(e, movePoint3_mi)) { 
111              pointToMove = 3; 
112              return; 
113          } 
114          if (match(e, applyBilinear4Points_mi)) { 
115              xf.revert(); 
116              xf.applyBilinear4Points(); 
117              return; 
118          } 
119          if (match(e, revert_mi)) { 
120              revert(); 
121              return; 
122          } 
123          if (match(e, rotate_mi)) { 
124              selection = rotate_mi; 
125              return; 
126          } 
127          if (match(e, xformFeedback_mi)) { 
128              xformFeedback(); 
129              return; 
130          } 
131   
132          if (match(e, apply_mi)) { 
133              apply(); 
134              return; 
135          } 
136          if (match(e, shearRotate_mi)) { 
137              selection = shearRotate_mi; 
138              return; 
139          } 
140          if (match(e, scaleRotate_mi)) { 
141              selection = scaleRotate_mi; 
142              return; 
143          } 
144          if (match(e, scale_mi)) { 
145              selection = scale_mi; 
146              return; 
147          } 
148          if (match(e, scalex_mi)) { 
149              selection = scalex_mi; 
150              return; 
151          } 
152          if (match(e, scaley_mi)) { 
153              selection = scaley_mi; 
154              return; 
155          } 
156          if (match(e, shearx_mi)) { 
157              selection = shearx_mi; 
158              return; 
159          } 
160          if (match(e, sheary_mi)) { 
161              selection = sheary_mi; 
162              return; 
163          } 
164          super.actionPerformed(e); 
165      } 
166   
167      public void xformFeedback() { 
168          xf.applyBilinear4PointsFeedback(); 
169      } 
170   
171      public AffineFrame(String title, XformFrame _xf, int w, int h) { 
172          super(title); 
173          xf = _xf; 
174          width = w; 
175          height = w; 
176          init(); 
177          addMouseListener(this); 
178          addMouseMotionListener(this); 
179          transformMenu.add(pointMenu); 
180          mb.add(transformMenu); 
181          setMenuBar(mb); 
182      } 
183   
184      //  p0      p1 
185      //  p3      p2 
186      private void init() { 
187          int x2 = x1 + width; 
188          int y2 = y1 + height; 
189          p.addPoint(x1, y1); 
190          p.addPoint(x2, y1); 
191          p.addPoint(x2, y2); 
192          p.addPoint(x1, y2); 
193          centroid = Mat3.centroid(p); 
194          setPose(0, 1, 1); 
195   
196      } 
197   
198      private void revert() { 
199          xf.revertNoResize(); 
200          p = new Polygon(); 
201          init(); 
202          repaint(); 
203      } 
204   
205      public void setPose(double theta, double sx, double sy) { 
206          Mat3 tr1 = new Mat3(); 
207          Mat3 tr2 = new Mat3(); 
208          Mat3 rt = new Mat3(); 
209          Mat3 sc = new Mat3(); 
210          centroid = rt.centroid(p); 
211   
212          tr1.setTranslation(centroid[0], centroid[1]); 
213          sc.setScale(sx, sy); 
214          rt.setRotation(theta); 
215          tr2.setTranslation(-centroid[0], -centroid[1]); 
216          at = tr1.multiply(rt); 
217          at = at.multiply(sc); 
218          at = at.multiply(tr2); 
219   
220      } 
221   
222      public void setShear(double theta, double shx, double shy) { 
223          Mat3 tr1 = new Mat3(); 
224          Mat3 tr2 = new Mat3(); 
225          Mat3 rt = new Mat3(); 
226          Mat3 sc = new Mat3(); 
227          centroid = rt.centroid(p); 
228   
229          tr1.setTranslation(centroid[0], centroid[1]); 
230          sc.setShear(shx, shy); 
231          rt.setRotation(theta); 
232          tr2.setTranslation(-centroid[0], -centroid[1]); 
233          at = tr1.multiply(rt); 
234          at = at.multiply(sc); 
235          at = at.multiply(tr2); 
236   
237      } 
238   
239      public static void main(String args[]) { 
240          AffineFrame af = new AffineFrame( 
241                  "AffineFrame", new XformFrame("XformFrame"), 100, 100); 
242          af.setSize(150, 150); 
243          af.setVisible(true); 
244      } 
245   
246      public void paint(Graphics g) { 
247          Polygon pt = at.transform(p); 
248          g.translate(xtranslate, ytranslate); 
249          g.drawPolygon(pt); 
250          for (int i = 0; i < pt.npoints; i++) 
251              g.drawString("p" + i, pt.xpoints[i], pt.ypoints[i]); 
252          Rectangle r = pt.getBounds(); 
253          g.drawString("h=" + r.height + " w=" + r.width, r.height / 2, r.width / 2); 
254      } 
255   
256      public void apply() { 
257          p = at.transform(p); 
258          xf.xform(at); 
259      } 
260   
261      public void movePoints(MouseEvent e) { 
262          int i = pointToMove; 
263          p.xpoints[i] = getX(e); 
264          p.ypoints[i] = getY(e); 
265          repaint(); 
266      } 
267   
268      public void printPoints() { 
269          for (int i = 0; i < p.xpoints.length; i++) { 
270              System.out.println("af.setPoint(" + i + "," + p.xpoints[i] + "," + p.ypoints[i] + ");"); 
271          } 
272      } 
273   
274      public void setPoint(int i, int x, int y) { 
275          p.xpoints[i] = x; 
276          p.ypoints[i] = y; 
277          repaint(); 
278      } 
279   
280      private int getX(MouseEvent e) { 
281          return (int) (e.getX() - xtranslate); 
282      } 
283   
284      private int getY(MouseEvent e) { 
285          return (int) (e.getY() - ytranslate); 
286      } 
287   
288      public void mousePressed(MouseEvent e) { 
289      } 
290   
291      public void mouseExited(MouseEvent e) { 
292      } 
293   
294      public void mouseEntered(MouseEvent e) { 
295      } 
296   
297      public void mouseClicked(MouseEvent e) { 
298      } 
299   
300      public void mouseReleased(MouseEvent e) { 
301   
302      } 
303   
304      public void mouseDragged(MouseEvent e) { 
305          e.consume(); 
306          double dx = getX(e) - centroid[0]; 
307          double dy = getY(e) - centroid[1]; 
308          double sx = 2 * Math.sqrt(dx * dx + dy * dy) / getSize().width; 
309          double sy = sx; 
310          double theta = Math.atan2(dy, dx) * 180 / Math.PI; 
311          double shx = Math.abs(dx) / getSize().width; 
312          double shy = Math.abs(dy) / getSize().height; 
313          if (pointToMove != -1) { 
314              movePoints(e); 
315              return; 
316          } 
317          if (selection == rotate_mi) { 
318              setPose(theta, 1, 1); 
319          } 
320          if (selection == scale_mi) { 
321              setPose(0, sx, sy); 
322          } 
323          if (selection == scalex_mi) { 
324              setPose(0, sx, 1); 
325          } 
326          if (selection == scaley_mi) { 
327              setPose(0, 1, sy); 
328          } 
329          if (selection == scaleRotate_mi) { 
330              setPose(theta, sx, sy); 
331          } 
332          if (selection == shearx_mi) { 
333              setShear(0, shx, 0); 
334          } 
335          if (selection == sheary_mi) { 
336              setShear(0, 0, shy); 
337          } 
338          if (selection == shearRotate_mi) { 
339              setShear(theta, shx, shy); 
340          } 
341          repaint(); 
342      } 
343   
344      public void mouseMoved(MouseEvent e) { 
345      } 
346   
347  }