/Users/lyon/j4p/src/ip/hak/SmallImageFrame.java

1    package ip.hak; 
2     
3    import java.awt.*; 
4    import java.awt.event.MouseEvent; 
5    import java.awt.event.MouseListener; 
6    import java.awt.event.MouseMotionListener; 
7     
8     
9    public class SmallImageFrame extends ip.gui.frames.ClosableFrame implements MouseListener, MouseMotionListener { 
10       Image im; 
11       FileDialog fd; 
12       int col, row; 
13       P4 p4[][]; 
14       Polygon sp[]; 
15       Polygon dp[]; 
16    
17    
18       public static void main(String args[]) { 
19           SmallImageFrame sif = new SmallImageFrame("Test"); 
20           sif.setSize(220, 230); 
21           sif.init(); 
22           sif.setVisible(true); 
23       } 
24    
25       public void init() { 
26           //loadFile(); 
27           QuestionDialog qd = new QuestionDialog(this, "Enter row & colum"); 
28           qd.makeDialog(); 
29           col = qd.getColum() + 1; 
30           row = qd.getRow() + 1; 
31           int sz = (col - 1) * (row - 1); 
32           sp = new Polygon[sz]; 
33           dp = new Polygon[sz]; 
34           initPoints(); 
35           addMouseListener(this); 
36           addMouseMotionListener(this); 
37       } 
38    
39       public void getPoly(Polygon[] pol) { 
40           int i = 0; 
41           int n = 4; 
42           for (int x = 0; x < col - 1; x++) 
43               for (int y = 0; y < row - 1; y++) { 
44                   int xps[] = new int[n]; 
45                   int yps[] = new int[n]; 
46    
47                   xps[0] = p4[x][y].getCenter().x - 10; 
48                   xps[1] = p4[x + 1][y].getCenter().x - 10; 
49                   xps[2] = p4[x + 1][y + 1].getCenter().x - 10; 
50                   xps[3] = p4[x][y + 1].getCenter().x - 10; 
51    
52                   yps[0] = p4[x][y].getCenter().y - 30; 
53                   yps[1] = p4[x + 1][y].getCenter().y - 30; 
54                   yps[2] = p4[x + 1][y + 1].getCenter().y - 30; 
55                   yps[3] = p4[x][y + 1].getCenter().y - 30; 
56    
57                   for (int it = 0; it < 4; it++) { 
58                       System.out.print("(" + xps[it] + " ," + yps[it] + ")  "); 
59                   } 
60                   System.out.println(""); 
61    
62                   pol[i++] = new Polygon(xps, yps, n); 
63               } 
64       } 
65    
66       public Polygon[] getSourcePoly() { 
67           return sp; 
68       } 
69    
70       public Polygon[] getDestPoly() { 
71           getPoly(dp); 
72           return dp; 
73       } 
74    
75       public void loadFile() { 
76           fd = new FileDialog(new Frame(), "Select Image File", 0); 
77           fd.setVisible(true); 
78           String fname = fd.getFile(); 
79           if (fname == null) 
80               return; 
81           String fdir = fd.getDirectory(); 
82           im = Toolkit.getDefaultToolkit().getImage(fdir + fname); 
83           repaint(); 
84       } 
85    
86       public SmallImageFrame(String title) { 
87           super(title); 
88       } 
89    
90       public void loadImage(Image img) { 
91           im = img; 
92           waitForImage(this, im); 
93           int sx = im.getWidth(this); 
94           int sy = im.getHeight(this); 
95    
96           setSize(sx + 20, sy + 40); 
97           repaint(); 
98       } 
99    
100      public static void waitForImage(Component component, Image image) { 
101          MediaTracker tracker = new MediaTracker(component); 
102          try { 
103              tracker.addImage(image, 0); 
104              tracker.waitForID(0); 
105              if (!tracker.checkID(0)) 
106                  System.out.println("Load failure!"); 
107          } catch (InterruptedException e) { 
108          } 
109      } 
110   
111      public void initPoints() { 
112          setLayout(null); 
113          Point p; 
114          Dimension d = getSize(); 
115          int gap = 10; 
116          int xg = (d.width - 2 * gap) / (col - 1); 
117          int yg = (d.height - gap - 30) / (row - 1); 
118          int xp = gap; 
119          int yp; 
120          p4 = new P4[col][row]; 
121   
122          for (int x = 0; x < col; x++) { 
123              yp = 30; 
124              for (int y = 0; y < row; y++) { 
125                  p = new Point(xp, yp); 
126                  p4[x][y] = new P4(p, x, y); 
127   
128                  Point cen = p4[x][y].getCenter(); 
129   
130                  add(p4[x][y]); 
131                  Dimension pd = p4[x][y].getPreferredSize(); 
132                  p4[x][y].setSize(pd.width, pd.height); 
133                  p4[x][y].setLocation(new Point(p.x - pd.width / 2, p.y - pd.height / 2)); 
134                  yp += yg; 
135              } 
136              xp += xg; 
137          } 
138          repaint(); 
139          getPoly(sp); 
140      } 
141   
142      public void paint(Graphics g) { 
143          if (im == null) 
144              return; 
145   
146          Dimension d = getSize(); 
147          g.drawImage(im, 10, 30, d.width - 20, d.height - 40, this); 
148          super.paint(g); 
149          if (p4 != null) { 
150              // Draw Vertical Lines 
151              for (int y = 0; y < p4[0].length - 1; y++) { 
152                  for (int x = 0; x < p4.length; x++) { 
153                      Point p1 = p4[x][y].getCenter(); 
154                      Point p2 = p4[x][y + 1].getCenter(); 
155                      g.drawLine(p1.x, p1.y, p2.x, p2.y); 
156                  } 
157              } 
158   
159              // Draw Horizontal Lines 
160              for (int y = 0; y < p4[0].length; y++) { 
161                  for (int x = 0; x < p4.length - 1; x++) { 
162                      Point p1 = p4[x][y].getCenter(); 
163                      Point p2 = p4[x + 1][y].getCenter(); 
164                      g.drawLine(p1.x, p1.y, p2.x, p2.y); 
165                  } 
166              } 
167          } 
168      } 
169   
170      public Point getIndex(int x, int y) { 
171          Point p = new Point(x, y); 
172   
173          for (int i = 0; i < p4.length; i++) 
174              for (int j = 0; j < p4[0].length; j++) { 
175                  if (p4[i][j].contains(p)) { 
176                      return new Point(i, j); 
177                  } 
178              } 
179          return null; 
180      } 
181   
182      public void mouseClicked(MouseEvent e) { 
183      } 
184   
185      public void mouseEntered(MouseEvent e) { 
186      } 
187   
188      public void mouseExited(MouseEvent e) { 
189      } 
190   
191      public void mouseMoved(MouseEvent e) { 
192      } 
193   
194      Point index = null; 
195      boolean msp = false; 
196   
197      public void mousePressed(MouseEvent e) { 
198          index = getIndex(e.getX(), e.getY()); 
199          if (index == null) { 
200              return; 
201          } 
202          msp = true; 
203          p4[index.x][index.y].invert(); 
204      } 
205   
206      public void mouseDragged(MouseEvent e) { 
207          if (index == null) 
208              return; 
209          int x = index.x, y = index.y; 
210          Point newP = new Point(e.getX(), e.getY()); 
211          p4[x][y].setCenter(newP); 
212          Dimension d = p4[x][y].getPreferredSize(); 
213          int xg = d.width / 2,yg = d.height / 2; 
214          p4[x][y].setLocation(newP.x - xg, newP.y - yg); 
215          repaint(); 
216      } 
217   
218      public void mouseReleased(MouseEvent e) { 
219          if (index == null) 
220              return; 
221          p4[index.x][index.y].invert(); 
222          msp = false; 
223          index = null; 
224      } 
225  }