KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DrawTest


1 /*
2  * @(#)DrawTest.java 1.15 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)DrawTest.java 1.15 06/02/22
39  */

40
41 import java.awt.event.*;
42 import java.awt.*;
43 import java.applet.*;
44
45 import java.util.Vector JavaDoc;
46
47 public class DrawTest extends Applet{
48     DrawPanel panel;
49     DrawControls controls;
50
51     public void init() {
52     setLayout(new BorderLayout());
53     panel = new DrawPanel();
54         controls = new DrawControls(panel);
55     add("Center", panel);
56     add("South",controls);
57     }
58
59     public void destroy() {
60         remove(panel);
61         remove(controls);
62     }
63
64     public static void main(String JavaDoc args[]) {
65     Frame f = new Frame("DrawTest");
66     DrawTest drawTest = new DrawTest();
67     drawTest.init();
68     drawTest.start();
69
70     f.add("Center", drawTest);
71     f.setSize(300, 300);
72     f.show();
73     }
74     public String JavaDoc getAppletInfo() {
75         return "A simple drawing program.";
76     }
77 }
78
79 class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
80     public static final int LINES = 0;
81     public static final int POINTS = 1;
82     int mode = LINES;
83     Vector JavaDoc lines = new Vector JavaDoc();
84     Vector JavaDoc colors = new Vector JavaDoc();
85     int x1,y1;
86     int x2,y2;
87
88     public DrawPanel() {
89     setBackground(Color.white);
90     addMouseMotionListener(this);
91     addMouseListener(this);
92     }
93
94     public void setDrawMode(int mode) {
95     switch (mode) {
96       case LINES:
97       case POINTS:
98         this.mode = mode;
99         break;
100       default:
101         throw new IllegalArgumentException JavaDoc();
102     }
103     }
104
105
106     public void mouseDragged(MouseEvent e) {
107         e.consume();
108         switch (mode) {
109             case LINES:
110                 x2 = e.getX();
111                 y2 = e.getY();
112                 break;
113             case POINTS:
114             default:
115                 colors.addElement(getForeground());
116                 lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
117                 x1 = e.getX();
118                 y1 = e.getY();
119                 break;
120         }
121         repaint();
122     }
123
124     public void mouseMoved(MouseEvent e) {
125     }
126
127     public void mousePressed(MouseEvent e) {
128         e.consume();
129         switch (mode) {
130             case LINES:
131                 x1 = e.getX();
132                 y1 = e.getY();
133                 x2 = -1;
134                 break;
135             case POINTS:
136             default:
137                 colors.addElement(getForeground());
138                 lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
139                 x1 = e.getX();
140                 y1 = e.getY();
141                 repaint();
142                 break;
143         }
144     }
145
146     public void mouseReleased(MouseEvent e) {
147         e.consume();
148         switch (mode) {
149             case LINES:
150                 colors.addElement(getForeground());
151                 lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
152                 x2 = -1;
153                 break;
154             case POINTS:
155             default:
156                 break;
157         }
158         repaint();
159     }
160
161     public void mouseEntered(MouseEvent e) {
162     }
163
164     public void mouseExited(MouseEvent e) {
165     }
166
167     public void mouseClicked(MouseEvent e) {
168     }
169
170     public void paint(Graphics g) {
171     int np = lines.size();
172
173     /* draw the current lines */
174     g.setColor(getForeground());
175     for (int i=0; i < np; i++) {
176         Rectangle p = (Rectangle)lines.elementAt(i);
177         g.setColor((Color)colors.elementAt(i));
178         if (p.width != -1) {
179         g.drawLine(p.x, p.y, p.width, p.height);
180         } else {
181         g.drawLine(p.x, p.y, p.x, p.y);
182         }
183     }
184     if (mode == LINES) {
185         g.setColor(getForeground());
186         if (x2 != -1) {
187                 g.drawLine(x1, y1, x2, y2);
188         }
189     }
190     }
191 }
192
193
194 class DrawControls extends Panel implements ItemListener {
195     DrawPanel target;
196
197     public DrawControls(DrawPanel target) {
198     this.target = target;
199     setLayout(new FlowLayout());
200     setBackground(Color.lightGray);
201     target.setForeground(Color.red);
202     CheckboxGroup group = new CheckboxGroup();
203     Checkbox b;
204     add(b = new Checkbox(null, group, false));
205     b.addItemListener(this);
206     b.setForeground(Color.red);
207     add(b = new Checkbox(null, group, false));
208     b.addItemListener(this);
209     b.setForeground(Color.green);
210     add(b = new Checkbox(null, group, false));
211     b.addItemListener(this);
212     b.setForeground(Color.blue);
213     add(b = new Checkbox(null, group, false));
214     b.addItemListener(this);
215     b.setForeground(Color.pink);
216     add(b = new Checkbox(null, group, false));
217     b.addItemListener(this);
218     b.setForeground(Color.orange);
219     add(b = new Checkbox(null, group, true));
220     b.addItemListener(this);
221     b.setForeground(Color.black);
222     target.setForeground(b.getForeground());
223     Choice shapes = new Choice();
224     shapes.addItemListener(this);
225     shapes.addItem("Lines");
226     shapes.addItem("Points");
227     shapes.setBackground(Color.lightGray);
228     add(shapes);
229     }
230
231     public void paint(Graphics g) {
232     Rectangle r = getBounds();
233     g.setColor(Color.lightGray);
234     g.draw3DRect(0, 0, r.width, r.height, false);
235
236         int n = getComponentCount();
237         for(int i=0; i<n; i++) {
238             Component comp = getComponent(i);
239             if (comp instanceof Checkbox) {
240                 Point loc = comp.getLocation();
241                 Dimension d = comp.getSize();
242                 g.setColor(comp.getForeground());
243                 g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
244             }
245         }
246     }
247
248   public void itemStateChanged(ItemEvent e) {
249     if (e.getSource() instanceof Checkbox) {
250       target.setForeground(((Component)e.getSource()).getForeground());
251     } else if (e.getSource() instanceof Choice) {
252       String JavaDoc choice = (String JavaDoc) e.getItem();
253       if (choice.equals("Lines")) {
254     target.setDrawMode(DrawPanel.LINES);
255       } else if (choice.equals("Points")) {
256     target.setDrawMode(DrawPanel.POINTS);
257       }
258     }
259   }
260 }
261
262
263
264
265
Popular Tags