|
1
|
|
|
package example; |
|
2
|
|
|
|
|
3
|
|
|
import org.gannacademy.cdf.graphics.Drawable; |
|
4
|
|
|
import org.gannacademy.cdf.graphics.Text; |
|
5
|
|
|
import org.gannacademy.cdf.graphics.geom.Rectangle; |
|
6
|
|
|
import org.gannacademy.cdf.graphics.geom.*; |
|
7
|
|
|
import org.gannacademy.cdf.graphics.ui.AppWindow; |
|
8
|
|
|
|
|
9
|
|
|
import java.awt.*; |
|
10
|
|
|
import java.awt.geom.Point2D; |
|
11
|
|
|
|
|
12
|
|
|
public class TransformationDemo extends AppWindow { |
|
13
|
|
|
Path rect, roundRect, ellipse, arc; |
|
|
|
|
|
|
14
|
|
|
Rectangle r; |
|
|
|
|
|
|
15
|
|
|
RoundRectangle rr; |
|
|
|
|
|
|
16
|
|
|
Ellipse e; |
|
|
|
|
|
|
17
|
|
|
Arc a; |
|
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
boolean inflation = true; |
|
20
|
|
|
int steps = 0; |
|
21
|
|
|
|
|
22
|
|
|
private void centerOnShape(Text label, Drawable shape) { |
|
23
|
|
|
label.setLocation(shape.getX() + (shape.getWidth() - label.getWidth()) / 2, shape.getY() + (shape.getHeight() - label.getHeight()) / 2 + label.getMaxAscent()); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
@Override |
|
27
|
|
|
protected void setup() { |
|
28
|
|
|
Color |
|
29
|
|
|
transparentRed = new Color(1f, 0f, 0f, 0.5f), |
|
30
|
|
|
transparentBlue = new Color(0f, 0f, 1f, 0.5f); |
|
31
|
|
|
|
|
32
|
|
|
r = new Rectangle(100, 50, 150, 100, getDrawingPanel()); |
|
33
|
|
|
r.setFillColor(transparentRed); |
|
34
|
|
|
rr = new RoundRectangle(350, 50, 150, 100, 25, 25, getDrawingPanel()); |
|
35
|
|
|
rr.setFillColor(transparentRed); |
|
36
|
|
|
e = new Ellipse(100, 250, 150, 100, getDrawingPanel()); |
|
37
|
|
|
e.setFillColor(transparentRed); |
|
38
|
|
|
a = new Arc(350, 250, 150, 100, 117, 312, getDrawingPanel()); |
|
39
|
|
|
a.setFillColor(transparentRed); |
|
40
|
|
|
|
|
41
|
|
|
rect = r.getAsPath(); |
|
42
|
|
|
rect.setFillColor(transparentBlue); |
|
43
|
|
|
roundRect = rr.getAsPath(); |
|
44
|
|
|
roundRect.setFillColor(transparentBlue); |
|
45
|
|
|
ellipse = e.getAsPath(); |
|
46
|
|
|
ellipse.setFillColor(transparentBlue); |
|
47
|
|
|
arc = a.getAsPath(); |
|
48
|
|
|
arc.setFillColor(transparentBlue); |
|
49
|
|
|
|
|
50
|
|
|
Text label = new Text("Translate", 0, 0, getDrawingPanel()); |
|
51
|
|
|
centerOnShape(label, r); |
|
52
|
|
|
label = new Text("Shear", 0, 0, getDrawingPanel()); |
|
53
|
|
|
centerOnShape(label, rr); |
|
54
|
|
|
label = new Text("Scale", 0, 0, getDrawingPanel()); |
|
55
|
|
|
centerOnShape(label, e); |
|
56
|
|
|
label = new Text("Rotate", 0, 0, getDrawingPanel()); |
|
57
|
|
|
centerOnShape(label, a); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
@Override |
|
61
|
|
|
protected void loop() { |
|
62
|
|
|
steps++; |
|
63
|
|
|
if (steps > 100) { |
|
64
|
|
|
inflation = !inflation; |
|
65
|
|
|
steps = 0; |
|
66
|
|
|
} |
|
67
|
|
|
rect.translate(.6 * (inflation ? 1 : 0 - 1), .4 * (inflation ? 1 : 0 - 1)); |
|
68
|
|
|
Point2D p = ellipse.getLocation(); |
|
69
|
|
|
ellipse.scale(1 + (inflation ? 1 : 0 - 1) * .01, 1 + (inflation ? 1 : 0 - 1) * .01); |
|
70
|
|
|
ellipse.setLocation(p.getX(), p.getY()); |
|
71
|
|
|
p = roundRect.getLocation(); |
|
72
|
|
|
roundRect.shear(.01 * (inflation ? 1 : 0 - 1), .01 * (inflation ? 1 : 0 - 1)); |
|
73
|
|
|
roundRect.setLocation(p.getX(), p.getY()); |
|
74
|
|
|
arc.rotate(Math.PI / 180 * (inflation ? 1 : 0 - 1), 425, 300); |
|
75
|
|
|
sleep(100); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public static void main(String[] args) { |
|
79
|
|
|
new TransformationDemo(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|