author | prr |
Thu, 18 Dec 2014 10:45:45 -0800 | |
changeset 29908 | 83e2c403fefd |
parent 25859 | 3317bb8137f4 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
8974 | 2 |
* Copyright (c) 1997, 2011, 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 |
||
10292
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
32 |
/* |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
33 |
* This source code is provided to illustrate the usage of a given feature |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
34 |
* or technique and has been deliberately simplified. Additional steps |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
35 |
* required for a production-quality application, such as security checks, |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
36 |
* input validation and proper error handling, might not be present in |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
37 |
* this sample code. |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
38 |
*/ |
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
39 |
|
ed7db6a12c2a
7067811: Update demo/sample code to state it should not be used for production
nloodin
parents:
8974
diff
changeset
|
40 |
|
2 | 41 |
|
42 |
import java.awt.*; |
|
43 |
import java.awt.event.*; |
|
44 |
import java.applet.*; |
|
45 |
||
8974 | 46 |
|
2 | 47 |
/** |
48 |
* An interactive test of the Graphics.drawArc and Graphics.fillArc |
|
49 |
* routines. Can be run either as a standalone application by |
|
50 |
* typing "java ArcTest" or as an applet in the AppletViewer. |
|
51 |
*/ |
|
8974 | 52 |
@SuppressWarnings("serial") |
2 | 53 |
public class ArcTest extends Applet { |
8974 | 54 |
|
2 | 55 |
ArcControls controls; // The controls for marking and filling arcs |
56 |
ArcCanvas canvas; // The drawing area to display arcs |
|
57 |
||
8974 | 58 |
@Override |
2 | 59 |
public void init() { |
60 |
setLayout(new BorderLayout()); |
|
61 |
canvas = new ArcCanvas(); |
|
62 |
add("Center", canvas); |
|
63 |
add("South", controls = new ArcControls(canvas)); |
|
64 |
} |
|
65 |
||
8974 | 66 |
@Override |
2 | 67 |
public void destroy() { |
68 |
remove(controls); |
|
69 |
remove(canvas); |
|
70 |
} |
|
71 |
||
8974 | 72 |
@Override |
2 | 73 |
public void start() { |
74 |
controls.setEnabled(true); |
|
75 |
} |
|
76 |
||
8974 | 77 |
@Override |
2 | 78 |
public void stop() { |
79 |
controls.setEnabled(false); |
|
80 |
} |
|
81 |
||
8974 | 82 |
@Override |
2 | 83 |
public void processEvent(AWTEvent e) { |
84 |
if (e.getID() == Event.WINDOW_DESTROY) { |
|
85 |
System.exit(0); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
public static void main(String args[]) { |
|
90 |
Frame f = new Frame("ArcTest"); |
|
91 |
ArcTest arcTest = new ArcTest(); |
|
92 |
||
93 |
arcTest.init(); |
|
94 |
arcTest.start(); |
|
95 |
||
96 |
f.add("Center", arcTest); |
|
97 |
f.setSize(300, 300); |
|
8974 | 98 |
f.setVisible(true); |
2 | 99 |
} |
100 |
||
8974 | 101 |
@Override |
2 | 102 |
public String getAppletInfo() { |
8974 | 103 |
return "An interactive test of the Graphics.drawArc and \nGraphics." |
104 |
+ "fillArc routines. Can be run \neither as a standalone " |
|
105 |
+ "application by typing 'java ArcTest' \nor as an applet in " |
|
106 |
+ "the AppletViewer."; |
|
2 | 107 |
} |
108 |
} |
|
109 |
||
8974 | 110 |
|
111 |
@SuppressWarnings("serial") |
|
2 | 112 |
class ArcCanvas extends Canvas { |
113 |
||
8974 | 114 |
int startAngle = 0; |
115 |
int extent = 45; |
|
116 |
boolean filled = false; |
|
117 |
Font font = new java.awt.Font("SansSerif", Font.PLAIN, 12); |
|
118 |
||
119 |
@Override |
|
2 | 120 |
public void paint(Graphics g) { |
121 |
Rectangle r = getBounds(); |
|
122 |
int hlines = r.height / 10; |
|
123 |
int vlines = r.width / 10; |
|
124 |
||
125 |
g.setColor(Color.pink); |
|
126 |
for (int i = 1; i <= hlines; i++) { |
|
127 |
g.drawLine(0, i * 10, r.width, i * 10); |
|
128 |
} |
|
129 |
for (int i = 1; i <= vlines; i++) { |
|
130 |
g.drawLine(i * 10, 0, i * 10, r.height); |
|
131 |
} |
|
132 |
||
133 |
g.setColor(Color.red); |
|
134 |
if (filled) { |
|
135 |
g.fillArc(0, 0, r.width - 1, r.height - 1, startAngle, extent); |
|
136 |
} else { |
|
137 |
g.drawArc(0, 0, r.width - 1, r.height - 1, startAngle, extent); |
|
138 |
} |
|
139 |
||
140 |
g.setColor(Color.black); |
|
141 |
g.setFont(font); |
|
142 |
g.drawLine(0, r.height / 2, r.width, r.height / 2); |
|
143 |
g.drawLine(r.width / 2, 0, r.width / 2, r.height); |
|
144 |
g.drawLine(0, 0, r.width, r.height); |
|
145 |
g.drawLine(r.width, 0, 0, r.height); |
|
146 |
int sx = 10; |
|
147 |
int sy = r.height - 28; |
|
148 |
g.drawString("Start = " + startAngle, sx, sy); |
|
149 |
g.drawString("Extent = " + extent, sx, sy + 14); |
|
150 |
} |
|
151 |
||
152 |
public void redraw(boolean filled, int start, int extent) { |
|
153 |
this.filled = filled; |
|
154 |
this.startAngle = start; |
|
155 |
this.extent = extent; |
|
156 |
repaint(); |
|
157 |
} |
|
158 |
} |
|
159 |
||
8974 | 160 |
|
161 |
@SuppressWarnings("serial") |
|
2 | 162 |
class ArcControls extends Panel |
8974 | 163 |
implements ActionListener { |
164 |
||
2 | 165 |
TextField startTF; |
166 |
TextField extentTF; |
|
167 |
ArcCanvas canvas; |
|
168 |
||
8974 | 169 |
@SuppressWarnings("LeakingThisInConstructor") |
2 | 170 |
public ArcControls(ArcCanvas canvas) { |
171 |
Button b = null; |
|
172 |
||
173 |
this.canvas = canvas; |
|
174 |
add(startTF = new IntegerTextField("0", 4)); |
|
175 |
add(extentTF = new IntegerTextField("45", 4)); |
|
176 |
b = new Button("Fill"); |
|
177 |
b.addActionListener(this); |
|
178 |
add(b); |
|
179 |
b = new Button("Draw"); |
|
180 |
b.addActionListener(this); |
|
181 |
add(b); |
|
182 |
} |
|
183 |
||
8974 | 184 |
@Override |
2 | 185 |
public void actionPerformed(ActionEvent ev) { |
186 |
String label = ev.getActionCommand(); |
|
187 |
||
188 |
int start, extent; |
|
189 |
try { |
|
190 |
start = Integer.parseInt(startTF.getText().trim()); |
|
8974 | 191 |
} catch (NumberFormatException ignored) { |
2 | 192 |
start = 0; |
193 |
} |
|
194 |
try { |
|
195 |
extent = Integer.parseInt(extentTF.getText().trim()); |
|
8974 | 196 |
} catch (NumberFormatException ignored) { |
2 | 197 |
extent = 0; |
198 |
} |
|
199 |
||
200 |
canvas.redraw(label.equals("Fill"), start, extent); |
|
201 |
} |
|
202 |
} |
|
203 |
||
8974 | 204 |
|
205 |
@SuppressWarnings("serial") |
|
2 | 206 |
class IntegerTextField extends TextField { |
207 |
||
208 |
String oldText = null; |
|
209 |
||
210 |
public IntegerTextField(String text, int columns) { |
|
211 |
super(text, columns); |
|
212 |
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.TEXT_EVENT_MASK); |
|
213 |
oldText = getText(); |
|
214 |
} |
|
215 |
||
216 |
// Consume non-digit KeyTyped events |
|
217 |
// Note that processTextEvent kind of eliminates the need for this |
|
218 |
// function, but this is neater, since ideally, it would prevent |
|
219 |
// the text from appearing at all. Sigh. See bugid 4100317/4114565. |
|
220 |
// |
|
8974 | 221 |
@Override |
2 | 222 |
protected void processEvent(AWTEvent evt) { |
223 |
int id = evt.getID(); |
|
224 |
if (id != KeyEvent.KEY_TYPED) { |
|
225 |
super.processEvent(evt); |
|
226 |
return; |
|
227 |
} |
|
228 |
||
229 |
KeyEvent kevt = (KeyEvent) evt; |
|
230 |
char c = kevt.getKeyChar(); |
|
231 |
||
232 |
// Digits, backspace, and delete are okay |
|
233 |
// Note that the minus sign is allowed, but not the decimal |
|
8974 | 234 |
if (Character.isDigit(c) || (c == '\b') || (c == '\u007f') || (c |
235 |
== '\u002d')) { |
|
2 | 236 |
super.processEvent(evt); |
237 |
return; |
|
238 |
} |
|
239 |
||
240 |
Toolkit.getDefaultToolkit().beep(); |
|
241 |
kevt.consume(); |
|
242 |
} |
|
243 |
||
244 |
// Should consume TextEvents for non-integer Strings |
|
245 |
// Store away the text in the tf for every TextEvent |
|
246 |
// so we can revert to it on a TextEvent (paste, or |
|
247 |
// legal key in the wrong location) with bad text |
|
248 |
// |
|
8974 | 249 |
@Override |
2 | 250 |
protected void processTextEvent(TextEvent te) { |
251 |
// The empty string is okay, too |
|
252 |
String newText = getText(); |
|
253 |
if (newText.equals("") || textIsInteger(newText)) { |
|
254 |
oldText = newText; |
|
255 |
super.processTextEvent(te); |
|
256 |
return; |
|
257 |
} |
|
258 |
||
259 |
Toolkit.getDefaultToolkit().beep(); |
|
260 |
setText(oldText); |
|
261 |
} |
|
262 |
||
263 |
// Returns true for Integers (zero and negative |
|
264 |
// values are allowed). |
|
265 |
// Note that the empty string is not allowed. |
|
266 |
// |
|
267 |
private boolean textIsInteger(String textToCheck) { |
|
268 |
||
269 |
try { |
|
8974 | 270 |
Integer.parseInt(textToCheck, 10); |
2 | 271 |
return true; |
8974 | 272 |
} catch (NumberFormatException ignored) { |
2 | 273 |
return false; |
274 |
} |
|
275 |
} |
|
276 |
} |