KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Arcs


1 /*
2  * @(#)Arcs.java 1.26 06/08/29
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 MIDROSYSTEMS, 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  * @(#)Arcs.java 1.26 06/08/29
39  */

40
41 package java2d.demos.Arcs_Curves;
42
43 import java.awt.*;
44 import java.awt.geom.Ellipse2D JavaDoc;
45 import java.awt.geom.Arc2D JavaDoc;
46 import java.awt.geom.AffineTransform JavaDoc;
47 import java2d.AnimatingSurface;
48
49 import static java.awt.Color JavaDoc.*;
50
51
52 /**
53  * Arc2D Open, Chord & Pie arcs; Animated Pie Arc.
54  */

55 public class Arcs extends AnimatingSurface {
56
57     private static String JavaDoc types[] = {"Arc2D.OPEN","Arc2D.CHORD","Arc2D.PIE"};
58
59     private static final int CLOSE = 0;
60     private static final int OPEN = 1;
61
62     private static final int FORWARD = 0;
63     private static final int BACKWARD = 1;
64     private static final int DOWN = 2;
65     private static final int UP = 3;
66
67     private int aw, ah; // animated arc width & height
68
private int x, y;
69     private int angleStart = 45;
70     private int angleExtent = 270;
71     private int mouth = CLOSE;
72     private int direction = FORWARD;
73
74
75     public Arcs() {
76         setBackground(WHITE);
77     }
78
79
80     public void reset(int w, int h) {
81         x = 0; y = 0;
82         aw = w/12; ah = h/12;
83     }
84
85
86     public void step(int w, int h) {
87       // Compute direction
88
if (x+aw >= w-5 && direction == FORWARD ) direction = DOWN;
89         if (y+ah >= h-5 && direction == DOWN ) direction = BACKWARD;
90         if (x-aw <= 5 && direction == BACKWARD) direction = UP;
91         if (y-ah <= 5 && direction == UP ) direction = FORWARD;
92
93       // compute angle start & extent
94
if (mouth == CLOSE) {
95             angleStart -= 5;
96             angleExtent += 10;
97         }
98         if (mouth == OPEN) {
99             angleStart += 5;
100             angleExtent -= 10;
101         }
102         if (direction == FORWARD) {
103             x += 5; y = 0;
104         }
105         if (direction == DOWN) {
106             x = w; y += 5;
107         }
108         if (direction == BACKWARD) {
109             x -= 5; y = h;
110         }
111         if (direction == UP) {
112             x = 0; y -= 5;
113         }
114         if (angleStart == 0)
115             mouth = OPEN;
116         if (angleStart > 45)
117             mouth = CLOSE;
118     }
119
120
121     public void render(int w, int h, Graphics2D g2) {
122
123       // Draw Arcs
124
g2.setStroke(new BasicStroke(5.0f));
125         for (int i = 0; i < types.length; i++) {
126             Arc2D JavaDoc arc = new Arc2D.Float JavaDoc(i);
127             arc.setFrame((i+1)*w*.2, (i+1)*h*.2, w*.17, h*.17);
128             arc.setAngleStart(45);
129             arc.setAngleExtent(270);
130             g2.setColor(BLUE);
131             g2.draw(arc);
132             g2.setColor(GRAY);
133             g2.fill(arc);
134             g2.setColor(BLACK);
135             g2.drawString(types[i], (int)((i+1)*w*.2), (int)((i+1)*h*.2-3));
136         }
137
138       // Draw Animated Pie Arc
139
Arc2D JavaDoc pieArc = new Arc2D.Float JavaDoc(Arc2D.PIE);
140         pieArc.setFrame(0, 0, aw, ah);
141         pieArc.setAngleStart(angleStart);
142         pieArc.setAngleExtent(angleExtent);
143         AffineTransform JavaDoc at = AffineTransform.getTranslateInstance(x, y);
144         switch (direction) {
145             case DOWN : at.rotate(Math.toRadians( 90)); break;
146             case BACKWARD : at.rotate(Math.toRadians(180)); break;
147             case UP : at.rotate(Math.toRadians(270));
148         }
149         g2.setColor(BLUE);
150         g2.fill(at.createTransformedShape(pieArc));
151     }
152
153
154     public static void main(String JavaDoc argv[]) {
155         createDemoFrame(new Arcs());
156     }
157 }
158
Popular Tags