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.Arcs_Curves;
|
|
33 |
|
|
34 |
|
|
35 |
import java.awt.*;
|
|
36 |
import java.awt.geom.Ellipse2D;
|
|
37 |
import java.awt.geom.Rectangle2D;
|
|
38 |
import java.awt.geom.QuadCurve2D;
|
|
39 |
import java.awt.geom.CubicCurve2D;
|
|
40 |
import java.awt.geom.PathIterator;
|
|
41 |
import java.awt.geom.FlatteningPathIterator;
|
|
42 |
import java.awt.font.TextLayout;
|
|
43 |
import java.awt.font.FontRenderContext;
|
|
44 |
import java2d.Surface;
|
|
45 |
import static java.awt.Color.*;
|
|
46 |
import static java.awt.geom.PathIterator.*;
|
|
47 |
|
|
48 |
|
|
49 |
/**
|
|
50 |
* CubicCurve2D & QuadCurve2D curves includes FlattenPathIterator example.
|
|
51 |
*/
|
|
52 |
@SuppressWarnings("serial")
|
|
53 |
public class Curves extends Surface {
|
|
54 |
|
|
55 |
private static Color colors[] = { BLUE, GREEN, RED };
|
|
56 |
|
|
57 |
public Curves() {
|
|
58 |
setBackground(WHITE);
|
|
59 |
}
|
|
60 |
|
|
61 |
@Override
|
|
62 |
public void render(int w, int h, Graphics2D g2) {
|
|
63 |
|
|
64 |
g2.setColor(BLACK);
|
|
65 |
FontRenderContext frc = g2.getFontRenderContext();
|
|
66 |
TextLayout tl = new TextLayout("QuadCurve2D", g2.getFont(), frc);
|
|
67 |
float xx = (float) (w * .5 - tl.getBounds().getWidth() / 2);
|
|
68 |
tl.draw(g2, xx, tl.getAscent());
|
|
69 |
|
|
70 |
tl = new TextLayout("CubicCurve2D", g2.getFont(), frc);
|
|
71 |
xx = (float) (w * .5 - tl.getBounds().getWidth() / 2);
|
|
72 |
tl.draw(g2, xx, h * .5f);
|
|
73 |
g2.setStroke(new BasicStroke(5.0f));
|
|
74 |
|
|
75 |
float yy = 20;
|
|
76 |
|
|
77 |
for (int i = 0; i < 2; i++) {
|
|
78 |
for (int j = 0; j < 3; j++) {
|
|
79 |
Shape shape = null;
|
|
80 |
|
|
81 |
if (i == 0) {
|
|
82 |
shape = new QuadCurve2D.Float(w * .1f, yy, w * .5f, 50, w
|
|
83 |
* .9f, yy);
|
|
84 |
} else {
|
|
85 |
shape = new CubicCurve2D.Float(w * .1f, yy, w * .4f, yy - 15,
|
|
86 |
w * .6f, yy + 15, w * .9f, yy);
|
|
87 |
}
|
|
88 |
g2.setColor(colors[j]);
|
|
89 |
if (j != 2) {
|
|
90 |
g2.draw(shape);
|
|
91 |
}
|
|
92 |
|
|
93 |
if (j == 1) {
|
|
94 |
g2.setColor(LIGHT_GRAY);
|
|
95 |
PathIterator f = shape.getPathIterator(null);
|
|
96 |
while (!f.isDone()) {
|
|
97 |
float[] pts = new float[6];
|
|
98 |
switch (f.currentSegment(pts)) {
|
|
99 |
case SEG_MOVETO:
|
|
100 |
case SEG_LINETO:
|
|
101 |
g2.fill(new Rectangle2D.Float(pts[0], pts[1], 5,
|
|
102 |
5));
|
|
103 |
break;
|
|
104 |
case SEG_CUBICTO:
|
|
105 |
case SEG_QUADTO:
|
|
106 |
g2.fill(new Rectangle2D.Float(pts[0], pts[1], 5,
|
|
107 |
5));
|
|
108 |
if (pts[2] != 0) {
|
|
109 |
g2.fill(new Rectangle2D.Float(pts[2], pts[3],
|
|
110 |
5, 5));
|
|
111 |
}
|
|
112 |
if (pts[4] != 0) {
|
|
113 |
g2.fill(new Rectangle2D.Float(pts[4], pts[5],
|
|
114 |
5, 5));
|
|
115 |
}
|
|
116 |
}
|
|
117 |
f.next();
|
|
118 |
}
|
|
119 |
} else if (j == 2) {
|
|
120 |
PathIterator p = shape.getPathIterator(null);
|
|
121 |
FlatteningPathIterator f =
|
|
122 |
new FlatteningPathIterator(p, 0.1);
|
|
123 |
while (!f.isDone()) {
|
|
124 |
float[] pts = new float[6];
|
|
125 |
switch (f.currentSegment(pts)) {
|
|
126 |
case SEG_MOVETO:
|
|
127 |
case SEG_LINETO:
|
|
128 |
g2.fill(new Ellipse2D.Float(pts[0], pts[1], 3, 3));
|
|
129 |
}
|
|
130 |
f.next();
|
|
131 |
}
|
|
132 |
}
|
|
133 |
yy += h / 6;
|
|
134 |
}
|
|
135 |
yy = h / 2 + 15;
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
public static void main(String argv[]) {
|
|
140 |
createDemoFrame(new Curves());
|
|
141 |
}
|
|
142 |
}
|