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

1    package ip.gui.frames; 
2     
3    import java.awt.*; 
4    import java.awt.event.ActionEvent; 
5    import java.awt.event.MouseEvent; 
6    import java.awt.event.MouseListener; 
7    import java.awt.event.MouseMotionListener; 
8     
9    public class GridImageFrame extends ImageFrame 
10           implements MouseListener, MouseMotionListener { 
11    
12       MenuBar mb = new MenuBar(); 
13       Menu SettingsMenu = new Menu("Settings"); 
14    
15       MenuItem openGif_mi = addMenuItem(SettingsMenu, "[c]hange image..."); 
16       MenuItem default_mi = addMenuItem(SettingsMenu, "[d]efault image"); 
17       MenuItem revert_mi = addMenuItem(SettingsMenu, "[r]evert"); 
18       MenuItem groff_mi = addMenuItem(SettingsMenu, "[g]rid on/off"); 
19       MenuItem applyBilinear4Points_mi = addMenuItem(SettingsMenu, "[a]pplyBilinear4Points"); 
20    
21       public Polygon p = new Polygon(); 
22       Image img = null; 
23       Color c = Color.black; 
24       boolean doRevert = false; 
25       short rn[][] = new short[0][0]; 
26       short gn[][] = new short[0][0]; 
27       short bn[][] = new short[0][0]; 
28       int xPoints = 5; //grid X 
29       int yPoints = 5; //grid Y 
30       boolean busy = false; 
31       int groff = 1; 
32       int pointToMove = -1; 
33    
34    
35       public void actionPerformed(ActionEvent e) { 
36           if (match(e, applyBilinear4Points_mi)) { 
37               revert(); 
38               applyBilinear(); 
39               return; 
40           } 
41           if (match(e, revert_mi)) { 
42               revert1(); 
43               return; 
44           } 
45           if (match(e, groff_mi)) { 
46               groff = 1 - groff; 
47               repaint(); 
48               return; 
49           } 
50    
51           if (match(e, openGif_mi)) { 
52               openGif1(); 
53               return; 
54           } 
55    
56           if (match(e, default_mi)) { 
57               default1(); 
58               return; 
59           } 
60    
61           super.actionPerformed(e); 
62       } 
63    
64       public GridImageFrame(String title, Color Clr) { 
65           super(title); 
66           default1(); 
67           c = Clr; 
68           addMouseListener(this); 
69           addMouseMotionListener(this); 
70           mb.add(SettingsMenu); 
71           setMenuBar(mb); 
72           repaint(); 
73       } 
74    
75       private void init() { 
76           groff = 1; 
77           for (int i = 0; i <= yPoints; i++) 
78               for (int j = 0; j <= xPoints; j++) p.addPoint((int) (j * getImageWidth() / xPoints), (int) (i * getImageHeight() / yPoints)); 
79           groff = 0; 
80       } 
81    
82    
83       private void openGif1() { 
84           openGif(); 
85           img = getImage(); 
86           doRevert = true; 
87           p = new Polygon(); 
88           init(); 
89           setSize(getImageWidth(), getImageHeight()); 
90           repaint(); 
91       } 
92    
93       public void revert1() { 
94           revert(); 
95           img = getImage(); 
96           doRevert = true; 
97           p = new Polygon(); 
98           init(); 
99           setSize(getImageWidth(), getImageHeight()); 
100          repaint(); 
101      } 
102   
103      public void default1() { 
104          grabNumImage(); 
105          setVisible(true); 
106          setSize(getImageWidth(), getImageHeight()); 
107          img = getImage(); 
108          doRevert = true; 
109          p = new Polygon(); 
110          init(); 
111      } 
112   
113      public static void main(String args[]) { 
114          GridImageFrame af = new GridImageFrame( 
115                  "GridImage", Color.green); 
116          af.setSize(150, 150); 
117          af.setVisible(true); 
118          af.default1(); 
119      } 
120   
121   
122      public void setGrid(int x, int y) { 
123          xPoints = x; 
124          yPoints = y; 
125          revert1(); 
126      } 
127   
128      public void setGrid() { 
129          setGrid(10, 10); 
130      } 
131   
132      public void update(Graphics g) { 
133          paint(g); 
134      } 
135   
136   
137      public void paint(Graphics g) { 
138          if (doRevert) { 
139              if ((img != null)) g.drawImage(img, 0, 0, getImageWidth(), getImageHeight(), this); 
140          } 
141          g.setColor(c); 
142          if (groff == 0) { 
143              for (int i = 0; i <= yPoints; i++) 
144                  for (int j = 0; j <= xPoints; j++) { 
145                      if (j != (xPoints)) 
146                          g.drawLine(p.xpoints[j + i * (xPoints + 1)], 
147                                  p.ypoints[j + i * (xPoints + 1)], 
148                                  p.xpoints[j + 1 + i * (xPoints + 1)], 
149                                  p.ypoints[j + 1 + i * (xPoints + 1)]); 
150                      if (i != (yPoints)) 
151                          g.drawLine(p.xpoints[j + i * (xPoints + 1)], 
152                                  p.ypoints[j + i * (xPoints + 1)], 
153                                  p.xpoints[j + (i + 1) * (xPoints + 1)], 
154                                  p.ypoints[j + (i + 1) * (xPoints + 1)]); 
155                  } 
156              for (int i = 0; i < p.npoints; i++) 
157                  g.drawRect(p.xpoints[i] - 2, p.ypoints[i] - 2, 4, 4); 
158          } 
159      } 
160   
161      public void applyBilinear() { 
162          rn = new short[getImageWidth()][getImageHeight()]; 
163          gn = new short[getImageWidth()][getImageHeight()]; 
164          bn = new short[getImageWidth()][getImageHeight()]; 
165          Point s0,s1,s2,s3,d0,d1,d2,d3; 
166   
167          for (int i = 0; i < yPoints; i++) 
168              for (int j = 0; j < xPoints; j++) { 
169                  s0 = new Point((int) (j * getImageWidth() / xPoints), (int) (i * getImageHeight() / yPoints)); 
170                  s1 = new Point((int) ((j + 1) * getImageWidth() / xPoints), (int) (i * getImageHeight() / yPoints)); 
171                  s2 = new Point((int) ((j + 1) * getImageWidth() / xPoints), (int) ((i + 1) * getImageHeight() / yPoints)); 
172                  s3 = new Point((int) (j * getImageWidth() / xPoints), (int) ((i + 1) * getImageHeight() / yPoints)); 
173                  d0 = new Point(p.xpoints[j + i * (xPoints + 1)], p.ypoints[j + i * (xPoints + 1)]); 
174                  d1 = new Point(p.xpoints[j + 1 + i * (xPoints + 1)], p.ypoints[j + 1 + i * (xPoints + 1)]); 
175                  d2 = new Point(p.xpoints[j + 1 + (i + 1) * (xPoints + 1)], p.ypoints[j + 1 + (i + 1) * (xPoints + 1)]); 
176                  d3 = new Point(p.xpoints[j + (i + 1) * (xPoints + 1)], p.ypoints[j + (i + 1) * (xPoints + 1)]); 
177                  solve(s0, s1, s2, s3, d0, d1, d2, d3); 
178              } 
179          short[][] r = rn; 
180          shortImageBean.setR(r); 
181          setG(gn); 
182          setB(bn); 
183          short2Image(); 
184          img = getImage(); 
185      } 
186   
187      int Dist(Point a, Point b) { 
188          return (int) (Math.sqrt(((b.x - a.x) * (b.x - a.x)) + ((b.y - a.y) * (b.y - a.y)))); 
189      } 
190   
191      public void solve(Point s0, Point s1, Point s2, Point s3, Point d0, Point d1, Point d2, Point d3) { 
192          double xStart1, xEnd1,yStart1, yEnd1; 
193          double xStart2, xEnd2,yStart2, yEnd2; 
194          double k = 0; 
195          int MAX = 0; 
196          int MAX1 = 0; 
197          int xc, yc, xp, yp; 
198   
199          MAX = Dist(d0, d1); 
200          MAX1 = Dist(d1, d2); 
201          if (MAX1 > MAX) MAX = MAX1; 
202          MAX1 = Dist(d2, d3); 
203          if (MAX1 > MAX) MAX = MAX1; 
204          MAX1 = Dist(d3, d0); 
205          if (MAX1 > MAX) MAX = MAX1; 
206   
207          MAX1 = Dist(s0, s1); 
208          if (MAX1 > MAX) MAX = MAX1; 
209          MAX1 = Dist(s1, s2); 
210          if (MAX1 > MAX) MAX = MAX1; 
211          MAX1 = Dist(s2, s3); 
212          if (MAX1 > MAX) MAX = MAX1; 
213          MAX1 = Dist(s3, s0); 
214          if (MAX1 > MAX) MAX = MAX1; 
215          MAX = MAX + 1; 
216   
217          for (int i = 0; i <= MAX; i++) { 
218              k = (double) ((double) i / (double) MAX); 
219   
220              xStart1 = linearY(d0.x, d3.x, k); 
221              yStart1 = linearY(d0.y, d3.y, k); 
222              xEnd1 = linearY(d1.x, d2.x, k); 
223              yEnd1 = linearY(d1.y, d2.y, k); 
224   
225              xStart2 = linearY(s0.x, s3.x, k); 
226              yStart2 = linearY(s0.y, s3.y, k); 
227              xEnd2 = linearY(s1.x, s2.x, k); 
228              yEnd2 = linearY(s1.y, s2.y, k); 
229   
230              for (int j = 0; j <= MAX; j++) { 
231                  k = (double) ((double) j / (double) MAX); 
232                  xc = (int) (linearY(xStart1, xEnd1, k)); 
233                  yc = (int) (linearY(yStart1, yEnd1, k)); 
234   
235                  xp = (int) (linearY(xStart2, xEnd2, k)); 
236                  yp = (int) (linearY(yStart2, yEnd2, k)); 
237   
238                  if ((xp < getImageWidth()) && (yp < getImageHeight()) && (xp >= 0) && (yp >= 0) && 
239                          (xc < getImageWidth()) && (yc < getImageHeight()) && (xc >= 0) && (yc >= 0)) { 
240                      rn[xc][yc] = shortImageBean.getR()[xp][yp]; 
241                      gn[xc][yc] = shortImageBean.getG()[xp][yp]; 
242                      bn[xc][yc] = shortImageBean.getB()[xp][yp]; 
243                  } 
244              } 
245          } 
246      } 
247   
248   
249      double linearY(double x1, double x2, double t) { 
250          double dx = 0; 
251          dx = (double) (x2 - x1); 
252          return (double) (x1 + (double) (dx * t)); 
253      } 
254   
255      public void movePoints(MouseEvent e) { 
256          int i = pointToMove; 
257          p.xpoints[i] = getX(e); 
258          p.ypoints[i] = getY(e); 
259          repaint(); 
260      } 
261   
262      private int getX(MouseEvent e) { 
263          return (int) (e.getX()); 
264      } 
265   
266      private int getY(MouseEvent e) { 
267          return (int) (e.getY()); 
268      } 
269   
270      public void mousePressed(MouseEvent e) { 
271      } 
272   
273      public void mouseExited(MouseEvent e) { 
274      } 
275   
276      public void mouseEntered(MouseEvent e) { 
277      } 
278   
279      public void mouseClicked(MouseEvent e) { 
280      } 
281   
282      public void mouseReleased(MouseEvent e) { 
283          busy = false; 
284          repaint(); 
285      } 
286   
287      public void mouseDragged(MouseEvent e) { 
288          e.consume(); 
289          double dx = getX(e); 
290          double dy = getY(e); 
291          double sx, sy; 
292          double ray = 100000; 
293          if (!busy) { 
294              busy = true; 
295              pointToMove = -1; 
296              for (int i = 0; i < p.npoints; i++) { 
297                  sx = (p.xpoints[i] - dx) * (p.xpoints[i] - dx); 
298                  sy = (p.ypoints[i] - dy) * (p.ypoints[i] - dy); 
299                  if ((sx + sy) < ray) { 
300                      ray = sx + sy; 
301                      pointToMove = i; 
302                  } 
303              } 
304          } 
305          if (pointToMove != -1) { 
306              movePoints(e); 
307              return; 
308          } 
309          repaint(); 
310      } 
311   
312      public void mouseMoved(MouseEvent e) { 
313      } 
314   
315  }