KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ArcTest


1 /*
2  * @(#)ArcTest.java 1.16 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  * @(#)ArcTest.java 1.16 06/02/22
39  */

40
41 import java.awt.*;
42 import java.awt.event.*;
43 import java.applet.*;
44
45 /**
46  * An interactive test of the Graphics.drawArc and Graphics.fillArc
47  * routines. Can be run either as a standalone application by
48  * typing "java ArcTest" or as an applet in the AppletViewer.
49  */

50 public class ArcTest extends Applet {
51     ArcControls controls; // The controls for marking and filling arcs
52
ArcCanvas canvas; // The drawing area to display arcs
53

54     public void init() {
55     setLayout(new BorderLayout());
56     canvas = new ArcCanvas();
57     add("Center", canvas);
58     add("South", controls = new ArcControls(canvas));
59     }
60
61     public void destroy() {
62         remove(controls);
63         remove(canvas);
64     }
65
66     public void start() {
67     controls.setEnabled(true);
68     }
69
70     public void stop() {
71     controls.setEnabled(false);
72     }
73
74     public void processEvent(AWTEvent e) {
75         if (e.getID() == Event.WINDOW_DESTROY) {
76             System.exit(0);
77         }
78     }
79
80     public static void main(String JavaDoc args[]) {
81     Frame f = new Frame("ArcTest");
82     ArcTest arcTest = new ArcTest();
83
84     arcTest.init();
85     arcTest.start();
86
87     f.add("Center", arcTest);
88     f.setSize(300, 300);
89     f.show();
90     }
91
92     public String JavaDoc getAppletInfo() {
93         return "An interactive test of the Graphics.drawArc and \nGraphics.fillArc routines. Can be run \neither as a standalone application by typing 'java ArcTest' \nor as an applet in the AppletViewer.";
94     }
95 }
96
97 class ArcCanvas extends Canvas {
98     int startAngle = 0;
99     int extent = 45;
100     boolean filled = false;
101     Font font = new java.awt.Font JavaDoc("SansSerif", Font.PLAIN, 12);
102
103     public void paint(Graphics g) {
104     Rectangle r = getBounds();
105     int hlines = r.height / 10;
106     int vlines = r.width / 10;
107
108     g.setColor(Color.pink);
109     for (int i = 1; i <= hlines; i++) {
110         g.drawLine(0, i * 10, r.width, i * 10);
111     }
112     for (int i = 1; i <= vlines; i++) {
113         g.drawLine(i * 10, 0, i * 10, r.height);
114     }
115
116     g.setColor(Color.red);
117     if (filled) {
118         g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, extent);
119     } else {
120         g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, extent);
121     }
122
123     g.setColor(Color.black);
124     g.setFont(font);
125     g.drawLine(0, r.height / 2, r.width, r.height / 2);
126     g.drawLine(r.width / 2, 0, r.width / 2, r.height);
127     g.drawLine(0, 0, r.width, r.height);
128     g.drawLine(r.width, 0, 0, r.height);
129     int sx = 10;
130     int sy = r.height - 28;
131     g.drawString("Start = " + startAngle, sx, sy);
132     g.drawString("Extent = " + extent, sx, sy + 14);
133     }
134
135     public void redraw(boolean filled, int start, int extent) {
136     this.filled = filled;
137     this.startAngle = start;
138     this.extent = extent;
139     repaint();
140     }
141 }
142
143 class ArcControls extends Panel
144                   implements ActionListener {
145     TextField startTF;
146     TextField extentTF;
147     ArcCanvas canvas;
148
149     public ArcControls(ArcCanvas canvas) {
150     Button b = null;
151
152     this.canvas = canvas;
153     add(startTF = new IntegerTextField("0", 4));
154     add(extentTF = new IntegerTextField("45", 4));
155     b = new Button("Fill");
156     b.addActionListener(this);
157     add(b);
158     b = new Button("Draw");
159     b.addActionListener(this);
160     add(b);
161     }
162
163     public void actionPerformed(ActionEvent ev) {
164     String JavaDoc label = ev.getActionCommand();
165
166         int start, extent;
167         try {
168             start = Integer.parseInt(startTF.getText().trim());
169         } catch (NumberFormatException JavaDoc nfe) {
170             start = 0;
171         }
172         try {
173             extent = Integer.parseInt(extentTF.getText().trim());
174         } catch (NumberFormatException JavaDoc nfe) {
175             extent = 0;
176         }
177
178     canvas.redraw(label.equals("Fill"), start, extent);
179     }
180 }
181
182 class IntegerTextField extends TextField {
183
184     String JavaDoc oldText = null;
185
186     public IntegerTextField(String JavaDoc text, int columns) {
187         super(text, columns);
188         enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.TEXT_EVENT_MASK);
189         oldText = getText();
190     }
191
192     // Consume non-digit KeyTyped events
193
// Note that processTextEvent kind of eliminates the need for this
194
// function, but this is neater, since ideally, it would prevent
195
// the text from appearing at all. Sigh. See bugid 4100317/4114565.
196
//
197
protected void processEvent(AWTEvent evt) {
198         int id = evt.getID();
199         if (id != KeyEvent.KEY_TYPED) {
200             super.processEvent(evt);
201             return;
202         }
203
204         KeyEvent kevt = (KeyEvent) evt;
205         char c = kevt.getKeyChar();
206
207         // Digits, backspace, and delete are okay
208
// Note that the minus sign is allowed, but not the decimal
209
if (Character.isDigit(c) || (c == '\b') || (c == '\u007f') ||
210             (c == '\u002d')) {
211             super.processEvent(evt);
212             return;
213         }
214
215         Toolkit.getDefaultToolkit().beep();
216         kevt.consume();
217     }
218
219     // Should consume TextEvents for non-integer Strings
220
// Store away the text in the tf for every TextEvent
221
// so we can revert to it on a TextEvent (paste, or
222
// legal key in the wrong location) with bad text
223
//
224
protected void processTextEvent(TextEvent te) {
225         // The empty string is okay, too
226
String JavaDoc newText = getText();
227         if (newText.equals("") || textIsInteger(newText)) {
228             oldText = newText;
229             super.processTextEvent(te);
230             return;
231         }
232
233         Toolkit.getDefaultToolkit().beep();
234         setText(oldText);
235     }
236
237     // Returns true for Integers (zero and negative
238
// values are allowed).
239
// Note that the empty string is not allowed.
240
//
241
private boolean textIsInteger(String JavaDoc textToCheck) {
242         int value = -1;
243
244         try {
245             value = Integer.parseInt(textToCheck, 10);
246             return true;
247         } catch (NumberFormatException JavaDoc nfe) {
248             return false;
249         }
250     }
251 }
252
253
254
Popular Tags