/Users/lyon/j4p/src/bookExamples/ch26Graphics/draw2d/DrawTest.java

1    package bookExamples.ch26Graphics.draw2d; 
2     
3    //graphics.draw2d.DrawTest 
4     
5    import ip.gui.frames.ShortCutFrame; 
6    import j2d.ImageUtils; 
7     
8    import java.awt.*; 
9    import java.awt.event.ActionEvent; 
10   import java.awt.event.MouseEvent; 
11   import java.awt.event.MouseListener; 
12   import java.awt.event.MouseMotionListener; 
13    
14   public class DrawTest extends ShortCutFrame 
15           implements MouseListener, MouseMotionListener { 
16    
17       private Image img = null; 
18    
19       Font font 
20               = new Font("Times", Font.PLAIN, 12); 
21    
22    
23       private MenuBar mb = new MenuBar(); 
24    
25       private Menu fileMenu = new Menu("File"); 
26       private MenuItem saveAsPict_mi = 
27               addMenuItem(fileMenu, "[E-s]ave as pict"); 
28       private MenuItem line_mi = 
29               addMenuItem(fileMenu, "[l]ine"); 
30       private MenuItem circle_mi = 
31               addMenuItem(fileMenu, "[c]ircle"); 
32       private MenuItem oval_mi = 
33               addMenuItem(fileMenu, "[o]val"); 
34       private MenuItem rect_mi = 
35               addMenuItem(fileMenu, "[r]ect"); 
36          private MenuItem fill_rect_mi = 
37               addMenuItem(fileMenu, "[f]ill rect"); 
38    
39       private MenuItem string_mi = 
40               addMenuItem(fileMenu, "[s]tring"); 
41       private MenuItem lens_mi = 
42               addMenuItem(fileMenu, "[L]ens"); 
43       private MenuItem ray_mi = 
44               addMenuItem(fileMenu, "[R]ay shoot"); 
45       private MenuItem erase_mi = 
46               addMenuItem(fileMenu, "[e]rase"); 
47    
48       private MenuItem lastSelected = line_mi; 
49    
50       private Shapes shapeList = new Shapes(); 
51    
52       private int x1, y1; 
53       private int x2, y2; 
54       private int xl, yl; 
55    
56    
57       public DrawTest(String title) { 
58           super(title); 
59           mb.add(fileMenu); 
60           setMenuBar(mb); 
61           setBackground(Color.white); 
62           addMouseMotionListener(this); 
63           addMouseListener(this); 
64       } 
65    
66       private void save() { 
67           ImageUtils.print(this); 
68       } 
69    
70       public void actionPerformed(ActionEvent e) { 
71           if (match(e, fill_rect_mi)){ 
72               lastSelected = fill_rect_mi; 
73               return; 
74           } 
75           if (match(e, erase_mi)) { 
76               shapeList = new Shapes(); 
77               repaint(); 
78               return; 
79           } 
80           if (match(e, ray_mi)) { 
81               shapeList.addLineToLastCircle(); 
82               repaint(); 
83               return; 
84           } 
85           if (match(e, lens_mi)) { 
86               lastSelected = lens_mi; 
87               return; 
88           } 
89           if (match(e, string_mi)) { 
90               lastSelected = string_mi; 
91               return; 
92           } 
93    
94           if (match(e, rect_mi)) { 
95               lastSelected = rect_mi; 
96               return; 
97           } 
98           if (match(e, saveAsPict_mi)) { 
99               save(); 
100              return; 
101          } 
102   
103          if (match(e, line_mi)) { 
104              lastSelected = line_mi; 
105              return; 
106          } 
107          if (match(e, circle_mi)) { 
108              lastSelected = circle_mi; 
109              return; 
110          } 
111          if (match(e, oval_mi)) { 
112              lastSelected = oval_mi; 
113              return; 
114          } 
115   
116      } 
117   
118      public static void main(String args[]) { 
119          DrawTest drawTest = new DrawTest("DrawTest"); 
120          drawTest.setSize(300, 300); 
121          drawTest.show(); 
122      } 
123   
124      public void mouseDragged(MouseEvent e) { 
125          int m = e.getModifiers(); 
126          if ((m & (Event.SHIFT_MASK | Event.ALT_MASK)) != 0) { 
127              System.out.println("alt-shift-mouse"); 
128          } 
129          e.consume(); 
130          setXl(getX2()); 
131          setYl(getY2()); 
132          setX2(e.getX()); 
133          setY2(e.getY()); 
134          repaint(); 
135      } 
136   
137      public void mouseMoved(MouseEvent e) { 
138      } 
139   
140      public void mousePressed(MouseEvent e) { 
141          e.consume(); 
142          setX1(e.getX()); 
143          setY1(e.getY()); 
144          setX2(-1); 
145      } 
146   
147      public void mouseReleased(MouseEvent e) { 
148          e.consume(); 
149          if (lastSelected == fill_rect_mi) 
150              shapeList.add(new FillRect2d(getX1(), getY1(), e.getX(), e.getY())); 
151          if (lastSelected == lens_mi) 
152              shapeList.add(new Arc2d(getX1(), getY1(), e.getX(), e.getY())); 
153          if (lastSelected == string_mi) 
154              shapeList.add(new String2d(e.getX(), e.getY(), "String me!")); 
155          if (lastSelected == line_mi) 
156              shapeList.add(new Line2d(getX1(), getY1(), e.getX(), e.getY())); 
157          if (lastSelected == rect_mi) 
158              shapeList.add(new Rect2d(getX1(), getY1(), e.getX(), e.getY())); 
159          if (lastSelected == circle_mi) 
160              shapeList.add(new Circle2d(getX1(), getY1(), e.getX(), e.getY())); 
161          if (lastSelected == oval_mi) 
162              shapeList.add(new Oval2d(getX1(), getY1(), e.getX(), e.getY())); 
163          setXl(-1); 
164          setX2(-1); 
165          repaint(); 
166      } 
167   
168      public void mouseEntered(MouseEvent e) { 
169      } 
170   
171      public void mouseExited(MouseEvent e) { 
172      } 
173   
174      public void mouseClicked(MouseEvent e) { 
175      } 
176   
177      public void drawRubberBand(Graphics g) { 
178          if (getX2() == -1) return; 
179          g.setXORMode(getBackground()); 
180          if (lastSelected == lens_mi) { 
181              Arc2d a = 
182                      new Arc2d(getX1(), getY1(), getX2(), getY2()); 
183              a.paint(g); 
184              return; 
185          } 
186          if (lastSelected == string_mi) { 
187              String2d s = 
188                      new String2d(getX2(), getY2(), "String me!"); 
189              s.paint(g); 
190              return; 
191          } 
192          if (lastSelected == line_mi) { 
193              g.drawLine(getX1(), getY1(), getX2(), getY2()); 
194              return; 
195          } 
196   
197          if (lastSelected == circle_mi) { 
198              Circle2d c = new Circle2d(getX1(), getY1(), getX2(), getY2()); 
199              c.paint(g); 
200              return; 
201          } 
202          if (lastSelected == oval_mi) { 
203              Oval2d o = new Oval2d(getX1(), getY1(), getX2(), getY2()); 
204              o.paint(g); 
205              return; 
206          } 
207          if (lastSelected == rect_mi) { 
208              Rect2d r = 
209                      new Rect2d(getX1(), getY1(), getX2(), getY2()); 
210              r.paint(g); 
211              return; 
212          } 
213      } 
214   
215      public void paint(Graphics g) { 
216          Dimension d = this.getSize(); 
217          int w = d.width; 
218          int h = d.height; 
219          if (img != null) 
220              g.drawImage(img, 0, 0, w, h, this); 
221          g.setFont(font); 
222          shapeList.paint(g); 
223          drawRubberBand(g); 
224   
225      } 
226   
227      public Rectangle getRoi() { 
228          Rectangle r = new Rectangle(); 
229          Dimension d = getSize(); 
230          int w = d.width; 
231          int h = d.height; 
232          int imageWidth = img.getWidth(this); 
233          int imageHeight = img.getHeight(this); 
234          Rect2d lastRect = shapeList.getLastRectangle(); 
235          double sx = imageWidth / w; 
236          double sy = imageHeight / h; 
237          double rw = lastRect.getW() * sx; 
238          double rh = lastRect.getH() * sy; 
239          double rx = lastRect.getX() * sx; 
240          double ry = lastRect.getY() * sy; 
241          r.setRect(rx, ry, rw, rh); 
242          return r; 
243      } 
244   
245      public void setImage(Image image) { 
246          img = image; 
247          setSize(img.getWidth(this), 
248                  img.getHeight(this)); 
249          this.setResizable(false); 
250      } 
251   
252      public int getX1() { 
253          return x1; 
254      } 
255   
256      public void setX1(int x1) { 
257          this.x1 = x1; 
258      } 
259   
260      public int getY1() { 
261          return y1; 
262      } 
263   
264      public void setY1(int y1) { 
265          this.y1 = y1; 
266      } 
267   
268      public int getX2() { 
269          return x2; 
270      } 
271   
272      public void setX2(int x2) { 
273          this.x2 = x2; 
274      } 
275   
276      public int getY2() { 
277          return y2; 
278      } 
279   
280      public void setY2(int y2) { 
281          this.y2 = y2; 
282      } 
283   
284      public int getXl() { 
285          return xl; 
286      } 
287   
288      public void setXl(int xl) { 
289          this.xl = xl; 
290      } 
291   
292      public int getYl() { 
293          return yl; 
294      } 
295   
296      public void setYl(int yl) { 
297          this.yl = yl; 
298      } 
299   
300  } 
301   
302   
303   
304   
305   
306   
307   
308   
309   
310   
311   
312   
313   
314