50146
|
1 |
/*
|
|
2 |
*
|
|
3 |
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
|
|
4 |
*
|
|
5 |
* Redistribution and use in source and binary forms, with or without
|
|
6 |
* modification, are permitted provided that the following conditions
|
|
7 |
* are met:
|
|
8 |
*
|
|
9 |
* - Redistributions of source code must retain the above copyright
|
|
10 |
* notice, this list of conditions and the following disclaimer.
|
|
11 |
*
|
|
12 |
* - Redistributions in binary form must reproduce the above copyright
|
|
13 |
* notice, this list of conditions and the following disclaimer in the
|
|
14 |
* documentation and/or other materials provided with the distribution.
|
|
15 |
*
|
|
16 |
* - Neither the name of Oracle nor the names of its
|
|
17 |
* contributors may be used to endorse or promote products derived
|
|
18 |
* from this software without specific prior written permission.
|
|
19 |
*
|
|
20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
21 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
22 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
23 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
24 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
25 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
26 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
27 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
29 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
30 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
31 |
*/
|
|
32 |
package java2d.demos.Lines;
|
|
33 |
|
|
34 |
|
|
35 |
import static java.awt.Color.BLACK;
|
|
36 |
import static java.awt.Color.GRAY;
|
|
37 |
import static java.awt.Color.LIGHT_GRAY;
|
|
38 |
import static java.awt.Color.PINK;
|
|
39 |
import static java.awt.Color.WHITE;
|
|
40 |
import java.awt.BasicStroke;
|
|
41 |
import java.awt.Color;
|
|
42 |
import java.awt.Graphics2D;
|
|
43 |
import java.awt.geom.AffineTransform;
|
|
44 |
import java.awt.geom.Ellipse2D;
|
|
45 |
import java.awt.geom.GeneralPath;
|
|
46 |
import java.awt.geom.Line2D;
|
|
47 |
import java.awt.geom.PathIterator;
|
|
48 |
import java.awt.geom.Point2D;
|
|
49 |
import java2d.AnimatingSurface;
|
|
50 |
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Lines & Paths animation illustrating BasicStroke attributes.
|
|
54 |
*/
|
|
55 |
@SuppressWarnings("serial")
|
|
56 |
public class LineAnim extends AnimatingSurface {
|
|
57 |
|
|
58 |
private static int caps[] = { BasicStroke.CAP_BUTT,
|
|
59 |
BasicStroke.CAP_SQUARE, BasicStroke.CAP_ROUND };
|
|
60 |
private static int joins[] = { BasicStroke.JOIN_MITER,
|
|
61 |
BasicStroke.JOIN_BEVEL, BasicStroke.JOIN_ROUND };
|
|
62 |
private static Color colors[] = { GRAY, PINK, LIGHT_GRAY };
|
|
63 |
private static BasicStroke bs1 = new BasicStroke(1.0f);
|
|
64 |
private static final int CLOCKWISE = 0;
|
|
65 |
private Line2D lines[] = new Line2D[3];
|
|
66 |
private int rAmt[] = new int[lines.length];
|
|
67 |
private int direction[] = new int[lines.length];
|
|
68 |
private int speed[] = new int[lines.length];
|
|
69 |
private BasicStroke strokes[] = new BasicStroke[lines.length];
|
|
70 |
private GeneralPath path;
|
|
71 |
private Point2D[] pts;
|
|
72 |
private float size;
|
|
73 |
private Ellipse2D ellipse = new Ellipse2D.Double();
|
|
74 |
|
|
75 |
public LineAnim() {
|
|
76 |
setBackground(WHITE);
|
|
77 |
}
|
|
78 |
|
|
79 |
@Override
|
|
80 |
public void reset(int w, int h) {
|
|
81 |
size = (w > h) ? h / 6f : w / 6f;
|
|
82 |
for (int i = 0; i < lines.length; i++) {
|
|
83 |
lines[i] = new Line2D.Float(0, 0, size, 0);
|
|
84 |
strokes[i] = new BasicStroke(size / 3, caps[i], joins[i]);
|
|
85 |
rAmt[i] = i * 360 / lines.length;
|
|
86 |
direction[i] = i % 2;
|
|
87 |
speed[i] = i + 1;
|
|
88 |
}
|
|
89 |
|
|
90 |
path = new GeneralPath();
|
|
91 |
path.moveTo(size, -size / 2);
|
|
92 |
path.lineTo(size + size / 2, 0);
|
|
93 |
path.lineTo(size, +size / 2);
|
|
94 |
|
|
95 |
ellipse.setFrame(w / 2 - size * 2 - 4.5f, h / 2 - size * 2 - 4.5f, size
|
|
96 |
* 4, size * 4);
|
|
97 |
PathIterator pi = ellipse.getPathIterator(null, 0.9);
|
|
98 |
Point2D[] points = new Point2D[100];
|
|
99 |
int num_pts = 0;
|
|
100 |
while (!pi.isDone()) {
|
|
101 |
float[] pt = new float[6];
|
|
102 |
switch (pi.currentSegment(pt)) {
|
|
103 |
case PathIterator.SEG_MOVETO:
|
|
104 |
case PathIterator.SEG_LINETO:
|
|
105 |
points[num_pts] = new Point2D.Float(pt[0], pt[1]);
|
|
106 |
num_pts++;
|
|
107 |
}
|
|
108 |
pi.next();
|
|
109 |
}
|
|
110 |
pts = new Point2D[num_pts];
|
|
111 |
System.arraycopy(points, 0, pts, 0, num_pts);
|
|
112 |
}
|
|
113 |
|
|
114 |
@Override
|
|
115 |
public void step(int w, int h) {
|
|
116 |
for (int i = 0; i < lines.length; i++) {
|
|
117 |
if (direction[i] == CLOCKWISE) {
|
|
118 |
rAmt[i] += speed[i];
|
|
119 |
if (rAmt[i] == 360) {
|
|
120 |
rAmt[i] = 0;
|
|
121 |
}
|
|
122 |
} else {
|
|
123 |
rAmt[i] -= speed[i];
|
|
124 |
if (rAmt[i] == 0) {
|
|
125 |
rAmt[i] = 360;
|
|
126 |
}
|
|
127 |
}
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
@Override
|
|
132 |
public void render(int w, int h, Graphics2D g2) {
|
|
133 |
|
|
134 |
ellipse.setFrame(w / 2 - size, h / 2 - size, size * 2, size * 2);
|
|
135 |
g2.setColor(BLACK);
|
|
136 |
g2.draw(ellipse);
|
|
137 |
|
|
138 |
for (int i = 0; i < lines.length; i++) {
|
|
139 |
AffineTransform at = AffineTransform.getTranslateInstance(w / 2, h
|
|
140 |
/ 2);
|
|
141 |
at.rotate(Math.toRadians(rAmt[i]));
|
|
142 |
g2.setStroke(strokes[i]);
|
|
143 |
g2.setColor(colors[i]);
|
|
144 |
g2.draw(at.createTransformedShape(lines[i]));
|
|
145 |
g2.draw(at.createTransformedShape(path));
|
|
146 |
|
|
147 |
int j = (int) ((double) rAmt[i] / 360 * pts.length);
|
|
148 |
j = (j == pts.length) ? pts.length - 1 : j;
|
|
149 |
ellipse.setFrame(pts[j].getX(), pts[j].getY(), 9, 9);
|
|
150 |
g2.fill(ellipse);
|
|
151 |
}
|
|
152 |
|
|
153 |
g2.setStroke(bs1);
|
|
154 |
g2.setColor(BLACK);
|
|
155 |
for (int i = 0; i < pts.length; i++) {
|
|
156 |
ellipse.setFrame(pts[i].getX(), pts[i].getY(), 9, 9);
|
|
157 |
g2.draw(ellipse);
|
|
158 |
}
|
|
159 |
}
|
|
160 |
|
|
161 |
public static void main(String argv[]) {
|
|
162 |
createDemoFrame(new LineAnim());
|
|
163 |
}
|
|
164 |
}
|