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

1    package bookExamples.ch26Graphics.draw2d; 
2     
3    import ip.gui.frames.JShortCutFrame; 
4    import j2d.ImageUtils; 
5     
6    import javax.swing.JMenu; 
7    import javax.swing.JMenuBar; 
8    import javax.swing.JMenuItem; 
9    import java.awt.Color; 
10   import java.awt.Font; 
11   import java.awt.Graphics; 
12   import java.awt.event.ActionEvent; 
13   import java.awt.event.MouseEvent; 
14   import java.awt.event.MouseListener; 
15   import java.awt.event.MouseMotionListener; 
16    
17   public class JDrawTest extends JShortCutFrame 
18           implements MouseListener, MouseMotionListener { 
19       Font font 
20               = new Font("Times", Font.PLAIN, 12); 
21       private JMenuBar mb = new JMenuBar(); 
22       private JMenu fileMenu = new JMenu("File"); 
23       private JMenuItem saveAsPict_mi = 
24               addMenuItem(fileMenu, "[E-s]ave as pict"); 
25       private JMenuItem line_mi = 
26               addMenuItem(fileMenu, "[l]ine"); 
27       private JMenuItem circle_mi = 
28               addMenuItem(fileMenu, "[c]ircle"); 
29       private JMenuItem oval_mi = 
30               addMenuItem(fileMenu, "[o]val"); 
31       private JMenuItem rect_mi = 
32               addMenuItem(fileMenu, "[r]ect"); 
33       private JMenuItem string_mi = 
34               addMenuItem(fileMenu, "[s]tring"); 
35       private JMenuItem lens_mi = 
36               addMenuItem(fileMenu, "[L]ens"); 
37       private JMenuItem ray_mi = 
38               addMenuItem(fileMenu, "[R]ay shoot"); 
39       private JMenuItem erase_mi = 
40               addMenuItem(fileMenu, "[e]rase"); 
41       private JMenuItem lastSelected = line_mi; 
42       private Shapes shapeList = new Shapes(); 
43       private int x1, y1; 
44       private int x2, y2; 
45       private int xl, yl; 
46    
47       public JMenuItem addMenuItem(JMenu aMenu, String itemName) { 
48           JMenuItem mi = new JMenuItem(itemName); 
49           aMenu.add(mi); 
50           mi.addActionListener(this); 
51           return (mi); 
52       } 
53    
54       JDrawTest(String title) { 
55           super(title); 
56           mb.add(fileMenu); 
57           setJMenuBar(mb); 
58           setBackground(Color.white); 
59           addMouseMotionListener(this); 
60           addMouseListener(this); 
61       } 
62    
63       private void save() { 
64           ImageUtils.print(this); 
65       } 
66    
67       public void actionPerformed(ActionEvent e) { 
68           if (match(e, erase_mi)) { 
69               shapeList = new Shapes(); 
70               repaint(); 
71               return; 
72           } 
73           if (match(e, ray_mi)) { 
74               shapeList.addLineToLastCircle(); 
75               repaint(); 
76               return; 
77           } 
78           if (match(e, lens_mi)) { 
79               lastSelected = lens_mi; 
80               return; 
81           } 
82           if (match(e, string_mi)) { 
83               lastSelected = string_mi; 
84               return; 
85           } 
86           if (match(e, rect_mi)) { 
87               lastSelected = rect_mi; 
88               return; 
89           } 
90           if (match(e, saveAsPict_mi)) { 
91               save(); 
92               return; 
93           } 
94           if (match(e, line_mi)) { 
95               lastSelected = line_mi; 
96               return; 
97           } 
98           if (match(e, circle_mi)) { 
99               lastSelected = circle_mi; 
100              return; 
101          } 
102          if (match(e, oval_mi)) { 
103              lastSelected = oval_mi; 
104              return; 
105          } 
106      } 
107   
108      public static void main(String args[]) { 
109          JDrawTest drawTest = new JDrawTest("DrawTest"); 
110          drawTest.setSize(300, 300); 
111          drawTest.show(); 
112      } 
113   
114      public void mouseDragged(MouseEvent e) { 
115          e.consume(); 
116          setXl(getX2()); 
117          setYl(getY2()); 
118          setX2(e.getX()); 
119          setY2(e.getY()); 
120          repaint(); 
121      } 
122   
123      public void mouseMoved(MouseEvent e) { 
124      } 
125   
126      public void mousePressed(MouseEvent e) { 
127          e.consume(); 
128          setX1(e.getX()); 
129          setY1(e.getY()); 
130          setX2(-1); 
131      } 
132   
133      public void mouseReleased(MouseEvent e) { 
134          e.consume(); 
135          if (lastSelected == lens_mi) 
136              shapeList.add(new Arc2d(getX1(), getY1(), e.getX(), e.getY())); 
137          if (lastSelected == string_mi) 
138              shapeList.add(new String2d(e.getX(), e.getY(), "String me!")); 
139          if (lastSelected == line_mi) 
140              shapeList.add(new Line2d(getX1(), getY1(), e.getX(), e.getY())); 
141          if (lastSelected == rect_mi) 
142              shapeList.add(new Rect2d(getX1(), getY1(), e.getX(), e.getY())); 
143          if (lastSelected == circle_mi) 
144              shapeList.add(new Circle2d(getX1(), getY1(), e.getX(), e.getY())); 
145          if (lastSelected == oval_mi) 
146              shapeList.add(new Oval2d(getX1(), getY1(), e.getX(), e.getY())); 
147          setXl(-1); 
148          setX2(-1); 
149          repaint(); 
150      } 
151   
152      public void mouseEntered(MouseEvent e) { 
153      } 
154   
155      public void mouseExited(MouseEvent e) { 
156      } 
157   
158      public void mouseClicked(MouseEvent e) { 
159      } 
160   
161      public void drawRubberBand(Graphics g) { 
162          if (getX2() == -1) return; 
163          g.setXORMode(getBackground()); 
164          if (lastSelected == lens_mi) { 
165              Arc2d a = 
166                      new Arc2d(getX1(), getY1(), getX2(), getY2()); 
167              a.paint(g); 
168              return; 
169          } 
170          if (lastSelected == string_mi) { 
171              String2d s = 
172                      new String2d(getX2(), getY2(), "String me!"); 
173              s.paint(g); 
174              return; 
175          } 
176          if (lastSelected == line_mi) { 
177              g.drawLine(getX1(), getY1(), getX2(), getY2()); 
178              return; 
179          } 
180          if (lastSelected == circle_mi) { 
181              Circle2d c = new Circle2d(getX1(), getY1(), getX2(), getY2()); 
182              c.paint(g); 
183              return; 
184          } 
185          if (lastSelected == oval_mi) { 
186              Oval2d o = new Oval2d(getX1(), getY1(), getX2(), getY2()); 
187              o.paint(g); 
188              return; 
189          } 
190          if (lastSelected == rect_mi) { 
191              Rect2d r = new Rect2d(getX1(), getY1(), getX2(), getY2()); 
192              r.paint(g); 
193              return; 
194          } 
195      } 
196   
197      public void paint(Graphics g) { 
198          g.setFont(font); 
199          shapeList.paint(g); 
200          drawRubberBand(g); 
201      } 
202   
203      public int getX1() { 
204          return x1; 
205      } 
206   
207      public void setX1(int x1) { 
208          this.x1 = x1; 
209      } 
210   
211      public int getY1() { 
212          return y1; 
213      } 
214   
215      public void setY1(int y1) { 
216          this.y1 = y1; 
217      } 
218   
219      public int getX2() { 
220          return x2; 
221      } 
222   
223      public void setX2(int x2) { 
224          this.x2 = x2; 
225      } 
226   
227      public int getY2() { 
228          return y2; 
229      } 
230   
231      public void setY2(int y2) { 
232          this.y2 = y2; 
233      } 
234   
235      public int getXl() { 
236          return xl; 
237      } 
238   
239      public void setXl(int xl) { 
240          this.xl = xl; 
241      } 
242   
243      public int getYl() { 
244          return yl; 
245      } 
246   
247      public void setYl(int yl) { 
248          this.yl = yl; 
249      } 
250  } 
251   
252   
253   
254   
255   
256   
257   
258   
259   
260   
261   
262   
263   
264