/Users/lyon/j4p/src/bookExamples/ch26Graphics/draw2d/Spiral.java
|
1 package bookExamples.ch26Graphics.draw2d;
2
3 import j2d.ImageUtils;
4 import ip.gui.frames.ShortCutFrame;
5 import ip.gui.frames.XformFrame;
6 import math.Mat3;
7
8 import java.awt.*;
9 import java.awt.event.ActionEvent;
10 import java.awt.event.MouseEvent;
11
12 public class Spiral extends ShortCutFrame {
13
14 MenuBar mb = new MenuBar();
15
16 Menu fileMenu = new Menu("File");
17
18 MenuItem saveAsPict_mi = addMenuItem(fileMenu, "[s]ave as pict...");
19 MenuItem print_mi = addMenuItem(fileMenu, "print...");
20
21 private Polygon p = new Polygon();
22 Mat3 at;
23 int x1 = 0;
24 int y1 = 0;
25 int width;
26 int height;
27 int centroid[] = {width / 2, height / 2};
28 int xtranslate = width;
29 int ytranslate = height;
30
31 private void save() {
32 graphics.dclap.SavePICT.toFile(this);
33 ImageUtils.print(this);
34 }
35
36 private void print() {
37 ImageUtils.print(this);
38 }
39
40 public Polygon getPolygon() {
41 return at.transform(p);
42 }
43
44 public void actionPerformed(ActionEvent e) {
45
46 if (match(e, print_mi)) {
47 print();
48 return;
49 }
50
51 if (match(e, saveAsPict_mi)) {
52 save();
53 return;
54 }
55
56 super.actionPerformed(e);
57 }
58
59 Spiral(String title, int w, int h) {
60 super(title);
61 width = w;
62 height = w;
63 init();
64 mb.add(fileMenu);
65 setMenuBar(mb);
66 }
67
68 // p0 p1
69 // p3 p2
70 private void init() {
71 int x2 = x1 + width / 2;
72 int y2 = y1 + height / 2;
73 p.addPoint(x1, y1);
74 p.addPoint(x2, y1);
75 p.addPoint(x2, y2);
76 p.addPoint(x1, y2);
77 centroid = Mat3.centroid(p);
78 setPose(0, 1, 1);
79
80 }
81
82 private void revert() {
83 p = new Polygon();
84 init();
85 repaint();
86 }
87
88 public void setPose(double theta, double sx, double sy) {
89 Mat3 tr1 = new Mat3();
90 Mat3 tr2 = new Mat3();
91 Mat3 rt = new Mat3();
92 Mat3 sc = new Mat3();
93 centroid = rt.centroid(p);
94
95 tr1.setTranslation(centroid[0], centroid[1]);
96 sc.setScale(sx, sy);
97 rt.setRotation(theta);
98 tr2.setTranslation(-centroid[0], -centroid[1]);
99 at = tr1.multiply(rt);
100 at = at.multiply(sc);
101 at = at.multiply(tr2);
102
103 }
104
105 public void setShear(double theta, double shx, double shy) {
106 Mat3 tr1 = new Mat3();
107 Mat3 tr2 = new Mat3();
108 Mat3 rt = new Mat3();
109 Mat3 sc = new Mat3();
110 centroid = rt.centroid(p);
111
112 tr1.setTranslation(centroid[0], centroid[1]);
113 sc.setShear(shx, shy);
114 rt.setRotation(theta);
115 tr2.setTranslation(-centroid[0], -centroid[1]);
116 at = tr1.multiply(rt);
117 at = at.multiply(sc);
118 at = at.multiply(tr2);
119
120 }
121
122 public static void main(String args[]) {
123 SketchFrame af = new SketchFrame(
124 "SketchFrame", new XformFrame("SketchFrame"), 100, 100);
125 af.setSize(150, 150);
126 af.setVisible(true);
127 }
128
129 public void drawPolygon(Graphics g, Polygon p) {
130 int n = p.xpoints.length;
131 for (int i = 0; i < n - 1; i++)
132 g.drawLine(p.xpoints[i], p.ypoints[i],
133 p.xpoints[i + 1], p.ypoints[i + 1]);
134 g.drawLine(p.xpoints[0], p.ypoints[0],
135 p.xpoints[n - 1], p.ypoints[n - 1]);
136 }
137
138 public void paint2(Graphics g) {
139 Font f = new Font("Serif", Font.PLAIN, 12);
140 g.setFont(f);
141 Polygon pt = at.transform(p);
142 g.translate(50, 50);
143 drawPolygon(g, pt);
144 for (int i = 0; i < pt.npoints; i++)
145 g.drawString("p" + i, pt.xpoints[i], pt.ypoints[i]);
146 Rectangle r = pt.getBounds();
147 g.drawString("h=" + r.height + " w=" + r.width, r.height / 2, r.width / 2);
148 }
149
150 public void paint(Graphics g) {
151 Font f = new Font("Serif", Font.PLAIN, 12);
152 g.setFont(f);
153 g.translate(50, 50);
154 for (float theta = 0; theta < 360; theta += 10f) {
155 setPose(theta, sin(theta), sin(theta));
156 Polygon pt = at.transform(p);
157 drawPolygon(g, pt);
158 }
159 }
160
161 public static final double PI_ON_180
162 = Math.PI / 180f;
163
164 public float sin(float theta) {
165 return
166 (float) Math.sin(theta * PI_ON_180);
167 }
168
169 public float cos(float theta) {
170 return
171 (float) Math.cos(theta * PI_ON_180);
172 }
173
174 public void apply() {
175 p = at.transform(p);
176 }
177
178 public void setPoint(int i, int x, int y) {
179 p.xpoints[i] = x;
180 p.ypoints[i] = y;
181 repaint();
182 }
183
184 public void translatePoints(int x, int y) {
185 for (int i = 0; i < p.xpoints.length; i++) {
186 p.xpoints[i] += x;
187 p.ypoints[i] += y;
188 }
189 }
190
191 private int getX(MouseEvent e) {
192 return (int) (e.getX() - xtranslate);
193 }
194
195 private int getY(MouseEvent e) {
196 return (int) (e.getY() - ytranslate);
197 }
198 }