42 import java.applet.Applet;
43 import java.awt.BorderLayout;
44 import java.awt.Checkbox;
45 import java.awt.CheckboxGroup;
46 import java.awt.Choice;
47 import java.awt.Color;
48 import java.awt.Component;
49 import java.awt.Dimension;
50 import java.awt.FlowLayout;
51 import java.awt.Frame;
52 import java.awt.Graphics;
53 import java.awt.Panel;
54 import java.awt.Point;
55 import java.awt.Rectangle;
56 import java.awt.event.ItemEvent;
57 import java.awt.event.ItemListener;
58 import java.awt.event.MouseEvent;
59 import java.awt.event.MouseListener;
60 import java.awt.event.MouseMotionListener;
61 import java.util.ArrayList;
62 import java.util.List;
65 @SuppressWarnings(
"serial")
73 setLayout(
new BorderLayout());
77 add(
"South", controls);
86 public static void main(String args[]) {
87 Frame f =
new Frame(
"DrawTest");
92 f.add(
"Center", drawTest);
99 return "A simple drawing program.";
104 @SuppressWarnings(
"serial")
107 public static final int LINES = 0;
108 public static final int POINTS = 1;
110 List<Rectangle> lines =
new ArrayList<Rectangle>();
111 List<Color> colors =
new ArrayList<Color>();
115 @SuppressWarnings(
"LeakingThisInConstructor")
117 setBackground(Color.white);
118 addMouseMotionListener(
this);
119 addMouseListener(
this);
129 throw new IllegalArgumentException();
143 colors.add(getForeground());
144 lines.add(
new Rectangle(x1, y1, e.getX(), e.getY()));
167 colors.add(getForeground());
168 lines.add(
new Rectangle(e.getX(), e.getY(), -1, -1));
181 colors.add(getForeground());
182 lines.add(
new Rectangle(x1, y1, e.getX(), e.getY()));
206 int np = lines.size();
209 g.setColor(getForeground());
210 for (
int i = 0; i < np; i++) {
211 Rectangle p = lines.get(i);
212 g.setColor(colors.get(i));
214 g.drawLine(p.x, p.y, p.width, p.height);
216 g.drawLine(p.x, p.y, p.x, p.y);
220 g.setColor(getForeground());
222 g.drawLine(x1, y1, x2, y2);
229 @SuppressWarnings(
"serial")
234 @SuppressWarnings(
"LeakingThisInConstructor")
236 this.target = target;
237 setLayout(
new FlowLayout());
238 setBackground(Color.lightGray);
239 target.setForeground(Color.red);
240 CheckboxGroup group =
new CheckboxGroup();
242 add(b =
new Checkbox(
null, group,
false));
243 b.addItemListener(
this);
244 b.setForeground(Color.red);
245 add(b =
new Checkbox(
null, group,
false));
246 b.addItemListener(
this);
247 b.setForeground(Color.green);
248 add(b =
new Checkbox(
null, group,
false));
249 b.addItemListener(
this);
250 b.setForeground(Color.blue);
251 add(b =
new Checkbox(
null, group,
false));
252 b.addItemListener(
this);
253 b.setForeground(Color.pink);
254 add(b =
new Checkbox(
null, group,
false));
255 b.addItemListener(
this);
256 b.setForeground(Color.orange);
257 add(b =
new Checkbox(
null, group,
true));
258 b.addItemListener(
this);
259 b.setForeground(Color.black);
260 target.setForeground(b.getForeground());
261 Choice shapes =
new Choice();
262 shapes.addItemListener(
this);
263 shapes.addItem(
"Lines");
264 shapes.addItem(
"Points");
265 shapes.setBackground(Color.lightGray);
271 Rectangle r = getBounds();
272 g.setColor(Color.lightGray);
273 g.draw3DRect(0, 0, r.width, r.height,
false);
275 int n = getComponentCount();
276 for (
int i = 0; i < n; i++) {
277 Component comp = getComponent(i);
278 if (comp instanceof Checkbox) {
279 Point loc = comp.getLocation();
280 Dimension d = comp.getSize();
281 g.setColor(comp.getForeground());
282 g.drawRect(loc.x - 1, loc.y - 1, d.width + 1, d.height + 1);
289 if (e.getSource() instanceof Checkbox) {
290 target.setForeground(((Component) e.getSource()).getForeground());
291 }
else if (e.getSource() instanceof Choice) {
292 String choice = (String) e.getItem();
293 if (choice.equals(
"Lines")) {
295 }
else if (choice.equals(
"Points")) {