Crossfire JXClient, Trunk
DrawTest.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
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  *
15  * - Neither the name of Oracle nor the names of its
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  * This source code is provided to illustrate the usage of a given feature
34  * or technique and has been deliberately simplified. Additional steps
35  * required for a production-quality application, such as security checks,
36  * input validation and proper error handling, might not be present in
37  * this sample code.
38  */
39 
40 
41 
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;
63 
64 
65 @SuppressWarnings("serial")
66 public class DrawTest extends Applet {
67 
68  DrawPanel panel;
69  DrawControls controls;
70 
71  @Override
72  public void init() {
73  setLayout(new BorderLayout());
74  panel = new DrawPanel();
75  controls = new DrawControls(panel);
76  add("Center", panel);
77  add("South", controls);
78  }
79 
80  @Override
81  public void destroy() {
82  remove(panel);
83  remove(controls);
84  }
85 
86  public static void main(String args[]) {
87  Frame f = new Frame("DrawTest");
88  DrawTest drawTest = new DrawTest();
89  drawTest.init();
90  drawTest.start();
91 
92  f.add("Center", drawTest);
93  f.setSize(300, 300);
94  f.setVisible(true);
95  }
96 
97  @Override
98  public String getAppletInfo() {
99  return "A simple drawing program.";
100  }
101 }
102 
103 
104 @SuppressWarnings("serial")
105 class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
106 
107  public static final int LINES = 0;
108  public static final int POINTS = 1;
109  int mode = LINES;
110  List<Rectangle> lines = new ArrayList<Rectangle>();
111  List<Color> colors = new ArrayList<Color>();
112  int x1, y1;
113  int x2, y2;
114 
115  @SuppressWarnings("LeakingThisInConstructor")
116  public DrawPanel() {
117  setBackground(Color.white);
118  addMouseMotionListener(this);
119  addMouseListener(this);
120  }
121 
122  public void setDrawMode(int mode) {
123  switch (mode) {
124  case LINES:
125  case POINTS:
126  this.mode = mode;
127  break;
128  default:
129  throw new IllegalArgumentException();
130  }
131  }
132 
133  @Override
134  public void mouseDragged(MouseEvent e) {
135  e.consume();
136  switch (mode) {
137  case LINES:
138  x2 = e.getX();
139  y2 = e.getY();
140  break;
141  case POINTS:
142  default:
143  colors.add(getForeground());
144  lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));
145  x1 = e.getX();
146  y1 = e.getY();
147  break;
148  }
149  repaint();
150  }
151 
152  @Override
153  public void mouseMoved(MouseEvent e) {
154  }
155 
156  @Override
157  public void mousePressed(MouseEvent e) {
158  e.consume();
159  switch (mode) {
160  case LINES:
161  x1 = e.getX();
162  y1 = e.getY();
163  x2 = -1;
164  break;
165  case POINTS:
166  default:
167  colors.add(getForeground());
168  lines.add(new Rectangle(e.getX(), e.getY(), -1, -1));
169  x1 = e.getX();
170  y1 = e.getY();
171  repaint();
172  break;
173  }
174  }
175 
176  @Override
177  public void mouseReleased(MouseEvent e) {
178  e.consume();
179  switch (mode) {
180  case LINES:
181  colors.add(getForeground());
182  lines.add(new Rectangle(x1, y1, e.getX(), e.getY()));
183  x2 = -1;
184  break;
185  case POINTS:
186  default:
187  break;
188  }
189  repaint();
190  }
191 
192  @Override
193  public void mouseEntered(MouseEvent e) {
194  }
195 
196  @Override
197  public void mouseExited(MouseEvent e) {
198  }
199 
200  @Override
201  public void mouseClicked(MouseEvent e) {
202  }
203 
204  @Override
205  public void paint(Graphics g) {
206  int np = lines.size();
207 
208  /* draw the current lines */
209  g.setColor(getForeground());
210  for (int i = 0; i < np; i++) {
211  Rectangle p = lines.get(i);
212  g.setColor(colors.get(i));
213  if (p.width != -1) {
214  g.drawLine(p.x, p.y, p.width, p.height);
215  } else {
216  g.drawLine(p.x, p.y, p.x, p.y);
217  }
218  }
219  if (mode == LINES) {
220  g.setColor(getForeground());
221  if (x2 != -1) {
222  g.drawLine(x1, y1, x2, y2);
223  }
224  }
225  }
226 }
227 
228 
229 @SuppressWarnings("serial")
230 class DrawControls extends Panel implements ItemListener {
231 
232  DrawPanel target;
233 
234  @SuppressWarnings("LeakingThisInConstructor")
235  public DrawControls(DrawPanel target) {
236  this.target = target;
237  setLayout(new FlowLayout());
238  setBackground(Color.lightGray);
239  target.setForeground(Color.red);
240  CheckboxGroup group = new CheckboxGroup();
241  Checkbox b;
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);
266  add(shapes);
267  }
268 
269  @Override
270  public void paint(Graphics g) {
271  Rectangle r = getBounds();
272  g.setColor(Color.lightGray);
273  g.draw3DRect(0, 0, r.width, r.height, false);
274 
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);
283  }
284  }
285  }
286 
287  @Override
288  public void itemStateChanged(ItemEvent e) {
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")) {
294  target.setDrawMode(DrawPanel.LINES);
295  } else if (choice.equals("Points")) {
296  target.setDrawMode(DrawPanel.POINTS);
297  }
298  }
299  }
300 }
DrawTest.getAppletInfo
String getAppletInfo()
Definition: DrawTest.java:98
DrawTest.main
static void main(String args[])
Definition: DrawTest.java:86
DrawTest.destroy
void destroy()
Definition: DrawTest.java:81
DrawTest.init
void init()
Definition: DrawTest.java:72
DrawTest
Definition: DrawTest.java:66