2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
5506
|
15 |
* - Neither the name of Oracle nor the names of its
|
2
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
/*
|
|
33 |
*/
|
|
34 |
|
|
35 |
import java.awt.event.*;
|
|
36 |
import java.awt.*;
|
|
37 |
import java.applet.*;
|
|
38 |
|
|
39 |
import java.util.Vector;
|
|
40 |
|
|
41 |
public class DrawTest extends Applet{
|
|
42 |
DrawPanel panel;
|
|
43 |
DrawControls controls;
|
|
44 |
|
|
45 |
public void init() {
|
|
46 |
setLayout(new BorderLayout());
|
|
47 |
panel = new DrawPanel();
|
|
48 |
controls = new DrawControls(panel);
|
|
49 |
add("Center", panel);
|
|
50 |
add("South",controls);
|
|
51 |
}
|
|
52 |
|
|
53 |
public void destroy() {
|
|
54 |
remove(panel);
|
|
55 |
remove(controls);
|
|
56 |
}
|
|
57 |
|
|
58 |
public static void main(String args[]) {
|
|
59 |
Frame f = new Frame("DrawTest");
|
|
60 |
DrawTest drawTest = new DrawTest();
|
|
61 |
drawTest.init();
|
|
62 |
drawTest.start();
|
|
63 |
|
|
64 |
f.add("Center", drawTest);
|
|
65 |
f.setSize(300, 300);
|
|
66 |
f.show();
|
|
67 |
}
|
|
68 |
public String getAppletInfo() {
|
|
69 |
return "A simple drawing program.";
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
|
|
74 |
public static final int LINES = 0;
|
|
75 |
public static final int POINTS = 1;
|
|
76 |
int mode = LINES;
|
|
77 |
Vector lines = new Vector();
|
|
78 |
Vector colors = new Vector();
|
|
79 |
int x1,y1;
|
|
80 |
int x2,y2;
|
|
81 |
|
|
82 |
public DrawPanel() {
|
|
83 |
setBackground(Color.white);
|
|
84 |
addMouseMotionListener(this);
|
|
85 |
addMouseListener(this);
|
|
86 |
}
|
|
87 |
|
|
88 |
public void setDrawMode(int mode) {
|
|
89 |
switch (mode) {
|
|
90 |
case LINES:
|
|
91 |
case POINTS:
|
|
92 |
this.mode = mode;
|
|
93 |
break;
|
|
94 |
default:
|
|
95 |
throw new IllegalArgumentException();
|
|
96 |
}
|
|
97 |
}
|
|
98 |
|
|
99 |
|
|
100 |
public void mouseDragged(MouseEvent e) {
|
|
101 |
e.consume();
|
|
102 |
switch (mode) {
|
|
103 |
case LINES:
|
|
104 |
x2 = e.getX();
|
|
105 |
y2 = e.getY();
|
|
106 |
break;
|
|
107 |
case POINTS:
|
|
108 |
default:
|
|
109 |
colors.addElement(getForeground());
|
|
110 |
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
|
|
111 |
x1 = e.getX();
|
|
112 |
y1 = e.getY();
|
|
113 |
break;
|
|
114 |
}
|
|
115 |
repaint();
|
|
116 |
}
|
|
117 |
|
|
118 |
public void mouseMoved(MouseEvent e) {
|
|
119 |
}
|
|
120 |
|
|
121 |
public void mousePressed(MouseEvent e) {
|
|
122 |
e.consume();
|
|
123 |
switch (mode) {
|
|
124 |
case LINES:
|
|
125 |
x1 = e.getX();
|
|
126 |
y1 = e.getY();
|
|
127 |
x2 = -1;
|
|
128 |
break;
|
|
129 |
case POINTS:
|
|
130 |
default:
|
|
131 |
colors.addElement(getForeground());
|
|
132 |
lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
|
|
133 |
x1 = e.getX();
|
|
134 |
y1 = e.getY();
|
|
135 |
repaint();
|
|
136 |
break;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
public void mouseReleased(MouseEvent e) {
|
|
141 |
e.consume();
|
|
142 |
switch (mode) {
|
|
143 |
case LINES:
|
|
144 |
colors.addElement(getForeground());
|
|
145 |
lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
|
|
146 |
x2 = -1;
|
|
147 |
break;
|
|
148 |
case POINTS:
|
|
149 |
default:
|
|
150 |
break;
|
|
151 |
}
|
|
152 |
repaint();
|
|
153 |
}
|
|
154 |
|
|
155 |
public void mouseEntered(MouseEvent e) {
|
|
156 |
}
|
|
157 |
|
|
158 |
public void mouseExited(MouseEvent e) {
|
|
159 |
}
|
|
160 |
|
|
161 |
public void mouseClicked(MouseEvent e) {
|
|
162 |
}
|
|
163 |
|
|
164 |
public void paint(Graphics g) {
|
|
165 |
int np = lines.size();
|
|
166 |
|
|
167 |
/* draw the current lines */
|
|
168 |
g.setColor(getForeground());
|
|
169 |
for (int i=0; i < np; i++) {
|
|
170 |
Rectangle p = (Rectangle)lines.elementAt(i);
|
|
171 |
g.setColor((Color)colors.elementAt(i));
|
|
172 |
if (p.width != -1) {
|
|
173 |
g.drawLine(p.x, p.y, p.width, p.height);
|
|
174 |
} else {
|
|
175 |
g.drawLine(p.x, p.y, p.x, p.y);
|
|
176 |
}
|
|
177 |
}
|
|
178 |
if (mode == LINES) {
|
|
179 |
g.setColor(getForeground());
|
|
180 |
if (x2 != -1) {
|
|
181 |
g.drawLine(x1, y1, x2, y2);
|
|
182 |
}
|
|
183 |
}
|
|
184 |
}
|
|
185 |
}
|
|
186 |
|
|
187 |
|
|
188 |
class DrawControls extends Panel implements ItemListener {
|
|
189 |
DrawPanel target;
|
|
190 |
|
|
191 |
public DrawControls(DrawPanel target) {
|
|
192 |
this.target = target;
|
|
193 |
setLayout(new FlowLayout());
|
|
194 |
setBackground(Color.lightGray);
|
|
195 |
target.setForeground(Color.red);
|
|
196 |
CheckboxGroup group = new CheckboxGroup();
|
|
197 |
Checkbox b;
|
|
198 |
add(b = new Checkbox(null, group, false));
|
|
199 |
b.addItemListener(this);
|
|
200 |
b.setForeground(Color.red);
|
|
201 |
add(b = new Checkbox(null, group, false));
|
|
202 |
b.addItemListener(this);
|
|
203 |
b.setForeground(Color.green);
|
|
204 |
add(b = new Checkbox(null, group, false));
|
|
205 |
b.addItemListener(this);
|
|
206 |
b.setForeground(Color.blue);
|
|
207 |
add(b = new Checkbox(null, group, false));
|
|
208 |
b.addItemListener(this);
|
|
209 |
b.setForeground(Color.pink);
|
|
210 |
add(b = new Checkbox(null, group, false));
|
|
211 |
b.addItemListener(this);
|
|
212 |
b.setForeground(Color.orange);
|
|
213 |
add(b = new Checkbox(null, group, true));
|
|
214 |
b.addItemListener(this);
|
|
215 |
b.setForeground(Color.black);
|
|
216 |
target.setForeground(b.getForeground());
|
|
217 |
Choice shapes = new Choice();
|
|
218 |
shapes.addItemListener(this);
|
|
219 |
shapes.addItem("Lines");
|
|
220 |
shapes.addItem("Points");
|
|
221 |
shapes.setBackground(Color.lightGray);
|
|
222 |
add(shapes);
|
|
223 |
}
|
|
224 |
|
|
225 |
public void paint(Graphics g) {
|
|
226 |
Rectangle r = getBounds();
|
|
227 |
g.setColor(Color.lightGray);
|
|
228 |
g.draw3DRect(0, 0, r.width, r.height, false);
|
|
229 |
|
|
230 |
int n = getComponentCount();
|
|
231 |
for(int i=0; i<n; i++) {
|
|
232 |
Component comp = getComponent(i);
|
|
233 |
if (comp instanceof Checkbox) {
|
|
234 |
Point loc = comp.getLocation();
|
|
235 |
Dimension d = comp.getSize();
|
|
236 |
g.setColor(comp.getForeground());
|
|
237 |
g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
|
|
238 |
}
|
|
239 |
}
|
|
240 |
}
|
|
241 |
|
|
242 |
public void itemStateChanged(ItemEvent e) {
|
|
243 |
if (e.getSource() instanceof Checkbox) {
|
|
244 |
target.setForeground(((Component)e.getSource()).getForeground());
|
|
245 |
} else if (e.getSource() instanceof Choice) {
|
|
246 |
String choice = (String) e.getItem();
|
|
247 |
if (choice.equals("Lines")) {
|
|
248 |
target.setDrawMode(DrawPanel.LINES);
|
|
249 |
} else if (choice.equals("Points")) {
|
|
250 |
target.setDrawMode(DrawPanel.POINTS);
|
|
251 |
}
|
|
252 |
}
|
|
253 |
}
|
|
254 |
}
|