2
|
1 |
/*
|
|
2 |
* Copyright 1997-2006 Sun Microsystems, Inc. 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 Sun Microsystems 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 |
*/
|
|
34 |
|
|
35 |
import java.awt.*;
|
|
36 |
import java.util.*;
|
|
37 |
import java.awt.event.*;
|
|
38 |
import java.applet.Applet;
|
|
39 |
|
|
40 |
class GraphicsPanel extends Panel {
|
|
41 |
ActionListener al;
|
|
42 |
ItemListener il;
|
|
43 |
public GraphicsCards cards;
|
|
44 |
|
|
45 |
GraphicsPanel(EventListener listener) {
|
|
46 |
al = (ActionListener)listener;
|
|
47 |
il = (ItemListener)listener;
|
|
48 |
|
|
49 |
setLayout(new BorderLayout());
|
|
50 |
|
|
51 |
add("Center", cards = new GraphicsCards());
|
|
52 |
|
|
53 |
Panel p = new Panel();
|
|
54 |
//p.setLayout(new BorderLayout());
|
|
55 |
|
|
56 |
Button b = new Button("next");
|
|
57 |
b.addActionListener(al);
|
|
58 |
p.add(b);
|
|
59 |
|
|
60 |
b = new Button("previous");
|
|
61 |
b.addActionListener(al);
|
|
62 |
p.add(b);
|
|
63 |
|
|
64 |
p.add(new Label("go to:", Label.RIGHT));
|
|
65 |
|
|
66 |
Choice c = new Choice();
|
|
67 |
c.addItemListener(il);
|
|
68 |
p.add(c);
|
|
69 |
|
|
70 |
c.addItem("Arc");
|
|
71 |
c.addItem("Oval");
|
|
72 |
c.addItem("Polygon");
|
|
73 |
c.addItem("Rect");
|
|
74 |
c.addItem("RoundRect");
|
|
75 |
|
|
76 |
add("North", p);
|
|
77 |
|
|
78 |
setSize(400, 400);
|
|
79 |
}
|
|
80 |
|
|
81 |
public Dimension getPreferredSize() {
|
|
82 |
return new Dimension(200, 100);
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
public class GraphicsTest extends Applet
|
|
87 |
implements ActionListener, ItemListener {
|
|
88 |
GraphicsPanel mainPanel;
|
|
89 |
|
|
90 |
public void init() {
|
|
91 |
setLayout(new BorderLayout());
|
|
92 |
add("Center", mainPanel = new GraphicsPanel(this));
|
|
93 |
}
|
|
94 |
|
|
95 |
public void destroy() {
|
|
96 |
remove(mainPanel);
|
|
97 |
}
|
|
98 |
|
|
99 |
public void actionPerformed(ActionEvent e) {
|
|
100 |
String arg = e.getActionCommand();
|
|
101 |
|
|
102 |
if ("next".equals(arg)) {
|
|
103 |
((CardLayout)mainPanel.cards.getLayout()).next(mainPanel.cards);
|
|
104 |
}
|
|
105 |
else if ("previous".equals(arg)) {
|
|
106 |
((CardLayout)mainPanel.cards.getLayout()).previous(mainPanel.cards);
|
|
107 |
}
|
|
108 |
}
|
|
109 |
|
|
110 |
public void itemStateChanged(ItemEvent e) {
|
|
111 |
((CardLayout)mainPanel.cards.getLayout()).show(mainPanel.cards,(String)e.getItem());
|
|
112 |
}
|
|
113 |
|
|
114 |
public static void main(String args[]) {
|
|
115 |
AppletFrame.startApplet("GraphicsTest", "Graphics Test", args);
|
|
116 |
}
|
|
117 |
|
|
118 |
public String getAppletInfo() {
|
|
119 |
return "An interactive demonstration of some graphics.";
|
|
120 |
}
|
|
121 |
} // end class GraphicsTest
|
|
122 |
|
|
123 |
|
|
124 |
class GraphicsCards extends Panel {
|
|
125 |
public GraphicsCards() {
|
|
126 |
setLayout(new CardLayout());
|
|
127 |
add("Arc", new ArcCard());
|
|
128 |
add("Oval", new ShapeTest( new OvalShape() ) );
|
|
129 |
add("Polygon", new ShapeTest( new PolygonShape() ) );
|
|
130 |
add("Rect", new ShapeTest( new RectShape() ) );
|
|
131 |
add("RoundRect", new ShapeTest( new RoundRectShape() ) );
|
|
132 |
}
|
|
133 |
} // end class GraphicsCards
|
|
134 |
|
|
135 |
|
|
136 |
class ArcCard extends Panel {
|
|
137 |
public ArcCard() {
|
|
138 |
setLayout(new GridLayout(0, 2));
|
|
139 |
add(new ArcPanel(true));
|
|
140 |
add(new ArcPanel(false));
|
|
141 |
add(new ArcDegreePanel(true));
|
|
142 |
add(new ArcDegreePanel(false));
|
|
143 |
}
|
|
144 |
} // end class ArcCard
|
|
145 |
|
|
146 |
|
|
147 |
class ArcDegreePanel extends Panel {
|
|
148 |
boolean filled;
|
|
149 |
|
|
150 |
public ArcDegreePanel(boolean filled) {
|
|
151 |
this.filled = filled;
|
|
152 |
}
|
|
153 |
|
|
154 |
void arcSteps(Graphics g,
|
|
155 |
int step,
|
|
156 |
int x,
|
|
157 |
int y,
|
|
158 |
int w,
|
|
159 |
int h,
|
|
160 |
Color c1,
|
|
161 |
Color c2) {
|
|
162 |
int a1 = 0;
|
|
163 |
int a2 = step;
|
|
164 |
int progress = 0;
|
|
165 |
g.setColor(c1);
|
|
166 |
for (; (a1+a2) <= 360; a1 = a1+a2, a2 += 1) {
|
|
167 |
if (g.getColor() == c1) {
|
|
168 |
g.setColor(c2);
|
|
169 |
}
|
|
170 |
else {
|
|
171 |
g.setColor(c1);
|
|
172 |
}
|
|
173 |
|
|
174 |
if (filled) {
|
|
175 |
g.fillArc(x, y, w, h, a1, a2);
|
|
176 |
}
|
|
177 |
else {
|
|
178 |
g.drawArc(x, y, w, h, a1, a2);
|
|
179 |
}
|
|
180 |
|
|
181 |
progress = a1+a2;
|
|
182 |
} // end for
|
|
183 |
|
|
184 |
if (progress != 360) {
|
|
185 |
if (filled) {
|
|
186 |
g.fillArc(x, y, w, h, a1, 360 - progress);
|
|
187 |
}
|
|
188 |
else {
|
|
189 |
g.drawArc(x, y, w, h, a1, 360 - progress);
|
|
190 |
}
|
|
191 |
} // end if
|
|
192 |
} // end arcSteps()
|
|
193 |
|
|
194 |
public void paint(Graphics g) {
|
|
195 |
Rectangle r = getBounds();
|
|
196 |
|
|
197 |
arcSteps(g, 3, 0, 0, r.width, r.height, Color.orange, Color.blue);
|
|
198 |
|
|
199 |
arcSteps(g,
|
|
200 |
2,
|
|
201 |
r.width / 4,
|
|
202 |
r.height / 4,
|
|
203 |
r.width / 2,
|
|
204 |
r.height / 2,
|
|
205 |
Color.yellow,
|
|
206 |
Color.green);
|
|
207 |
|
|
208 |
arcSteps(g,
|
|
209 |
1,
|
|
210 |
(r.width * 3) / 8,
|
|
211 |
(r.height * 3) / 8,
|
|
212 |
r.width / 4,
|
|
213 |
r.height / 4,
|
|
214 |
Color.magenta,
|
|
215 |
Color.white);
|
|
216 |
|
|
217 |
} // end paint()
|
|
218 |
} // end class ArcDegreePanel
|
|
219 |
|
|
220 |
|
|
221 |
class ArcPanel extends Panel {
|
|
222 |
boolean filled;
|
|
223 |
|
|
224 |
public ArcPanel(boolean filled) {
|
|
225 |
this.filled = filled;
|
|
226 |
}
|
|
227 |
|
|
228 |
public void paint(Graphics g)
|
|
229 |
{
|
|
230 |
Rectangle r = getBounds();
|
|
231 |
|
|
232 |
g.setColor(Color.yellow);
|
|
233 |
if (filled)
|
|
234 |
{
|
|
235 |
g.fillArc(0, 0, r.width, r.height, 0, 45);
|
|
236 |
}
|
|
237 |
else
|
|
238 |
{
|
|
239 |
g.drawArc(0, 0, r.width, r.height, 0, 45);
|
|
240 |
}
|
|
241 |
|
|
242 |
g.setColor(Color.green);
|
|
243 |
if (filled)
|
|
244 |
{
|
|
245 |
g.fillArc(0, 0, r.width, r.height, 90, -45);
|
|
246 |
}
|
|
247 |
else
|
|
248 |
{
|
|
249 |
g.drawArc(0, 0, r.width, r.height, 90, -45);
|
|
250 |
}
|
|
251 |
|
|
252 |
g.setColor(Color.orange);
|
|
253 |
if (filled)
|
|
254 |
{
|
|
255 |
g.fillArc(0, 0, r.width, r.height, 135, -45);
|
|
256 |
}
|
|
257 |
else
|
|
258 |
{
|
|
259 |
g.drawArc(0, 0, r.width, r.height, 135, -45);
|
|
260 |
}
|
|
261 |
|
|
262 |
g.setColor(Color.magenta);
|
|
263 |
|
|
264 |
if (filled)
|
|
265 |
{
|
|
266 |
g.fillArc(0, 0, r.width, r.height, -225, 45);
|
|
267 |
}
|
|
268 |
else
|
|
269 |
{
|
|
270 |
g.drawArc(0, 0, r.width, r.height, -225, 45);
|
|
271 |
}
|
|
272 |
|
|
273 |
g.setColor(Color.yellow);
|
|
274 |
if (filled)
|
|
275 |
{
|
|
276 |
g.fillArc(0, 0, r.width, r.height, 225, -45);
|
|
277 |
}
|
|
278 |
else
|
|
279 |
{
|
|
280 |
g.drawArc(0, 0, r.width, r.height, 225, -45);
|
|
281 |
}
|
|
282 |
|
|
283 |
g.setColor(Color.green);
|
|
284 |
if (filled)
|
|
285 |
{
|
|
286 |
g.fillArc(0, 0, r.width, r.height, -135, 45);
|
|
287 |
}
|
|
288 |
else
|
|
289 |
{
|
|
290 |
g.drawArc(0, 0, r.width, r.height, -135, 45);
|
|
291 |
}
|
|
292 |
|
|
293 |
g.setColor(Color.orange);
|
|
294 |
if (filled)
|
|
295 |
{
|
|
296 |
g.fillArc(0, 0, r.width, r.height, -45, -45);
|
|
297 |
}
|
|
298 |
else
|
|
299 |
{
|
|
300 |
g.drawArc(0, 0, r.width, r.height, -45, -45);
|
|
301 |
}
|
|
302 |
|
|
303 |
g.setColor(Color.magenta);
|
|
304 |
if (filled)
|
|
305 |
{
|
|
306 |
g.fillArc(0, 0, r.width, r.height, 315, 45);
|
|
307 |
}
|
|
308 |
else
|
|
309 |
{
|
|
310 |
g.drawArc(0, 0, r.width, r.height, 315, 45);
|
|
311 |
}
|
|
312 |
|
|
313 |
} // end paint()
|
|
314 |
|
|
315 |
} // end class ArcPanel
|
|
316 |
|
|
317 |
|
|
318 |
abstract class Shape
|
|
319 |
{
|
|
320 |
abstract void draw(Graphics g, int x, int y, int w, int h);
|
|
321 |
abstract void fill(Graphics g, int x, int y, int w, int h);
|
|
322 |
}
|
|
323 |
|
|
324 |
|
|
325 |
class RectShape extends Shape
|
|
326 |
{
|
|
327 |
void draw(Graphics g, int x, int y, int w, int h)
|
|
328 |
{
|
|
329 |
g.drawRect(x, y, w, h);
|
|
330 |
}
|
|
331 |
|
|
332 |
void fill(Graphics g, int x, int y, int w, int h)
|
|
333 |
{
|
|
334 |
g.fillRect(x, y, w, h);
|
|
335 |
}
|
|
336 |
}
|
|
337 |
|
|
338 |
|
|
339 |
class OvalShape extends Shape
|
|
340 |
{
|
|
341 |
void draw(Graphics g, int x, int y, int w, int h)
|
|
342 |
{
|
|
343 |
g.drawOval(x, y, w, h);
|
|
344 |
}
|
|
345 |
|
|
346 |
void fill(Graphics g, int x, int y, int w, int h)
|
|
347 |
{
|
|
348 |
g.fillOval(x, y, w, h);
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
|
|
353 |
class RoundRectShape extends Shape
|
|
354 |
{
|
|
355 |
void draw(Graphics g, int x, int y, int w, int h)
|
|
356 |
{
|
|
357 |
g.drawRoundRect(x, y, w, h, 10, 10);
|
|
358 |
}
|
|
359 |
|
|
360 |
void fill(Graphics g, int x, int y, int w, int h)
|
|
361 |
{
|
|
362 |
g.fillRoundRect(x, y, w, h, 10, 10);
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
class PolygonShape extends Shape
|
|
367 |
{
|
|
368 |
// class variables
|
|
369 |
Polygon p;
|
|
370 |
Polygon pBase;
|
|
371 |
|
|
372 |
public PolygonShape()
|
|
373 |
{
|
|
374 |
pBase = new Polygon();
|
|
375 |
pBase.addPoint(0, 0);
|
|
376 |
pBase.addPoint(10, 0);
|
|
377 |
pBase.addPoint(5, 15);
|
|
378 |
pBase.addPoint(10, 20);
|
|
379 |
pBase.addPoint(5, 20);
|
|
380 |
pBase.addPoint(0, 10);
|
|
381 |
pBase.addPoint(0, 0);
|
|
382 |
}
|
|
383 |
|
|
384 |
void scalePolygon(float w, float h)
|
|
385 |
{
|
|
386 |
p = new Polygon();
|
|
387 |
for (int i = 0; i < pBase.npoints; ++i)
|
|
388 |
{
|
|
389 |
p.addPoint( (int) (pBase.xpoints[i] * w),
|
|
390 |
(int) (pBase.ypoints[i] * h) );
|
|
391 |
}
|
|
392 |
|
|
393 |
}
|
|
394 |
|
|
395 |
void draw(Graphics g, int x, int y, int w, int h)
|
|
396 |
{
|
|
397 |
Graphics ng = g.create();
|
|
398 |
try {
|
|
399 |
ng.translate(x, y);
|
|
400 |
scalePolygon( (float) ( (float) w / (float) 10 ),
|
|
401 |
(float) ( (float) h / (float) 20 ) );
|
|
402 |
ng.drawPolygon(p);
|
|
403 |
} finally {
|
|
404 |
ng.dispose();
|
|
405 |
}
|
|
406 |
}
|
|
407 |
|
|
408 |
void fill(Graphics g, int x, int y, int w, int h)
|
|
409 |
{
|
|
410 |
Graphics ng = g.create();
|
|
411 |
try {
|
|
412 |
ng.translate(x, y);
|
|
413 |
scalePolygon( (float) ( (float) w / (float) 10 ),
|
|
414 |
(float) ( (float) h / (float) 20 ) );
|
|
415 |
ng.fillPolygon(p);
|
|
416 |
} finally {
|
|
417 |
ng.dispose();
|
|
418 |
}
|
|
419 |
}
|
|
420 |
}
|
|
421 |
|
|
422 |
|
|
423 |
class ShapeTest extends Panel
|
|
424 |
{
|
|
425 |
Shape shape;
|
|
426 |
int step;
|
|
427 |
|
|
428 |
public ShapeTest(Shape shape, int step)
|
|
429 |
{
|
|
430 |
this.shape = shape;
|
|
431 |
this.step = step;
|
|
432 |
}
|
|
433 |
|
|
434 |
public ShapeTest(Shape shape)
|
|
435 |
{
|
|
436 |
this(shape, 10);
|
|
437 |
}
|
|
438 |
|
|
439 |
public void paint(Graphics g) {
|
|
440 |
Rectangle bounds = getBounds();
|
|
441 |
|
|
442 |
int cx, cy, cw, ch;
|
|
443 |
|
|
444 |
Color color;
|
|
445 |
|
|
446 |
for (color=Color.red,
|
|
447 |
cx=bounds.x,
|
|
448 |
cy=bounds.y,
|
|
449 |
cw=bounds.width / 2,
|
|
450 |
ch=bounds.height;
|
|
451 |
cw > 0 && ch > 0;
|
|
452 |
|
|
453 |
cx+=step,
|
|
454 |
cy += step,
|
|
455 |
cw -= (step * 2),
|
|
456 |
ch -= (step * 2),
|
|
457 |
color=ColorUtils.darker(color, 0.9) ) {
|
|
458 |
g.setColor(color);
|
|
459 |
shape.draw(g, cx, cy, cw, ch);
|
|
460 |
}
|
|
461 |
|
|
462 |
for (cx=bounds.x + bounds.width / 2,
|
|
463 |
cy=bounds.y,
|
|
464 |
cw=bounds.width / 2, ch=bounds.height;
|
|
465 |
cw > 0 && ch > 0;
|
|
466 |
|
|
467 |
cx+=step,
|
|
468 |
cy += step,
|
|
469 |
cw -= (step * 2),
|
|
470 |
ch -= (step * 2) ) {
|
|
471 |
if (g.getColor() == Color.red) {
|
|
472 |
g.setColor(Color.blue);
|
|
473 |
}
|
|
474 |
else {
|
|
475 |
g.setColor(Color.red);
|
|
476 |
}
|
|
477 |
|
|
478 |
shape.fill(g, cx, cy, cw, ch);
|
|
479 |
} // end for
|
|
480 |
} // end paint()
|
|
481 |
} // end class ShapeTest
|
|
482 |
|
|
483 |
class ColorUtils {
|
|
484 |
static Color brighter(Color c, double factor) {
|
|
485 |
return new Color( Math.min((int)(c.getRed() *(1/factor)), 255),
|
|
486 |
Math.min((int)(c.getGreen()*(1/factor)), 255),
|
|
487 |
Math.min((int)(c.getBlue() *(1/factor)), 255) );
|
|
488 |
}
|
|
489 |
|
|
490 |
static Color darker(Color c, double factor) {
|
|
491 |
return new Color( Math.max((int)(c.getRed() *factor), 0),
|
|
492 |
Math.max((int)(c.getGreen()*factor), 0),
|
|
493 |
Math.max((int)(c.getBlue() *factor), 0) );
|
|
494 |
}
|
|
495 |
}
|