/Users/lyon/j4p/src/bookExamples/ch26Graphics/sprials/Spirals.java
|
1 package bookExamples.ch26Graphics.sprials;
2
3 import j2d.ImageUtils;
4 import j2d.ShortImageBean;
5
6 import java.awt.*;
7 import java.awt.image.BufferedImage;
8
9 public class Spirals {
10 private int cx = 0; // always translated
11 private int cy = 0;
12
13 public void move(double x, double y) {
14 move((int) x, (int) y);
15 }
16
17 public void move(int x, int y) {
18 cx = x + tx;
19 cy = y + ty;
20 }
21
22 private int tx = 60;
23 private int ty = 60;
24
25 public void setSize(java.awt.Dimension d) {
26 tx = d.width / 2;
27 ty = d.height / 2;
28 }
29
30 public void drawp(int r, double a, java.awt.Graphics g) {
31 a = a * Math.PI / 180;
32 draw((int) (r * Math.cos(a)), (int) (r * Math.sin(a)), g);
33 }
34
35 public void draw(int x, int y, java.awt.Graphics g) {
36 g.drawLine(cx, cy, x + tx, (y + ty));
37 move(x, y);
38 }
39
40 public void drawTree(Graphics g) {
41 move(25, 25);
42 drawTree(2, 2.3, 4, -90, 6, 5, 5, g);
43 }
44
45 public void drawTree(double fanRatio, double lengthRatio, int numBranch,
46 double a, double l, double f, int n, Graphics g) {
47 int num;
48 double delang, ang;
49 if (n <= 0) {
50 drawLeaf(g);
51 return;
52 }
53 drawp(l, a, g);
54 num = numBranch;
55 if (num > 1)
56 delang = f / (num - 1.0);
57 else
58 delang = 0;
59 ang = a - f / 2.0 - delang;
60 for (int i = 0; i < num; i++) {
61 ang = ang + delang;
62 drawTree(fanRatio,
63 lengthRatio,
64 numBranch,
65 ang - (ang * 0.1 * Math.random()),
66 l * lengthRatio,
67 f * fanRatio,
68 n - 1, g);
69 }
70 }
71
72 private java.awt.Image img = getImage();
73
74 public Image getImage() {
75 ShortImageBean sib = new ShortImageBean(200,200);
76 return sib.getImage();
77 }
78 public void rdrawImage(double x, double y, java.awt.Graphics g) {
79 g.drawImage(img, (int) x, (int) y, 20, 20, null);
80 }
81
82 public void draw(double x, double y, java.awt.Graphics g) {
83 draw((int) x, (int) y, g);
84 }
85
86 public void drawp(double x, double y, java.awt.Graphics g) {
87 drawp((int) x, (int) y, g);
88 }
89
90 public void movep(int r, double a) {
91 a = a * Math.PI / 180;
92 move((int) (r * Math.cos(a)), (int) (r * Math.sin(a)));
93 }
94
95 public void movep(double x, double y) {
96 movep((int) x, (int) y);
97 }
98
99
100 public void drawCircle(java.awt.Graphics g) {
101 for (int a = 0; a <= 360; a = a + 360 / 8) {
102 if (a == 0) movep(50, a);
103 drawp(50, a, g);
104 }
105 }
106
107 public static javax.swing.JComponent getArchimedesSpiral() {
108 return new javax.swing.JComponent() {
109 Spirals s = new Spirals();
110
111 public void paint(Graphics g) {
112 s.setSize(getSize());
113 s.archimedesSpiral(0.1, g);
114 }
115
116 public Dimension getPreferredSize() {
117 return new Dimension(200, 200);
118 }
119 };
120
121 }
122
123 public double archimedes(double k, double theta) {
124 return k * theta;
125 }
126
127 public void archimedesSpiral(double k, java.awt.Graphics g) {
128 double theta = 0;
129 double r = archimedes(k, theta);
130 movep(r, theta);
131 for (int s = 1; s < 12000; s++, theta += 3) {
132 r = archimedes(k, theta);
133 drawp(r, theta, g);
134 }
135 }
136
137 public void archimedesImage(double k, java.awt.Graphics g) {
138 double theta = 0;
139 double r = archimedes(k, theta);
140 movep(r, theta);
141 for (int s = 1; s < 12000; s++, theta += 3) {
142 r = archimedes(k, theta);
143 drawImage(r, theta, g);
144 }
145 }
146
147 public void equiangularSpiral(java.awt.Graphics g) {
148 double a = 0;
149 double k = 1.5;
150 for (int s = 1; s < 1600; s++, a += 3) {
151 double r = Math.pow(k, a);
152 if (s == 1) movep(r, a);
153 drawp(r, a, g);
154 }
155 }
156
157 public void fermatsSpiral(
158 double k,
159 double c,
160 java.awt.Graphics g) {
161 double d = 360 / c;
162 double a = 0;
163 for (int s = -600; s < 600; s++, a += d) {
164 double r = Math.sqrt(a) * k;
165 movep(r, a);
166 //drawHex(r,a,g);
167 drawImage(r, a, g);
168 }
169 }
170
171 public void tile31_1(int modf,
172 double alpha, double gamma,
173 java.awt.Dimension d, java.awt.Graphics g) {
174 double beta1 = -11;
175 double beta2 = -12;
176 for (int i = 0; i < d.width; i++)
177 for (int j = 0; j < d.height; j++) {
178 double x = beta1 + gamma * i;
179 double y = beta2 + gamma * j;
180 double z = alpha * (Math.sin(x) + Math.sin(y));
181 int c = (int) z;
182 if ((c % modf) == 0) {
183 g.setColor(new java.awt.Color(c));
184 move(i, j);
185 draw(i, j, g);
186 }
187 }
188 }
189
190 public void drawHex(double r, double a, java.awt.Graphics g) {
191 for (int t = 0; t <= 6; t++) {
192 double aa = 360 * t / 6 + a;
193 double rr = 3;
194 double x = rr * cos(aa) + r * cos(a);
195 double y = rr * sin(aa) + r * sin(a);
196 if (t == 0) move(x, y);
197 draw(x, y, g);
198 }
199 }
200
201 public void drawLeaf(Graphics g) {
202 //g.drawString("*",cx,cy);
203 g.drawImage(img, cx, cy, 30, 30, null);
204 }
205
206 public void drawImage(double r, double a, java.awt.Graphics g) {
207 double rr = 3;
208 a = -a * Math.PI / 180;
209 int x = (int) Math.round(r * Math.cos(a));
210 int y = (int) Math.round(r * Math.sin(a));
211 g.drawString("*", cx, cy);
212
213 //g.drawImage(img,(int)x,(int)y,10,10,null);
214 }
215
216 public static double cos(double a) {
217 return Math.cos(a * Math.PI / 180);
218 }
219
220 public static double sin(double a) {
221 return Math.sin(a * Math.PI / 180);
222 }
223
224 public void starPolygon(int n, int m, java.awt.Graphics g) {
225 for (int s = 0; s <= n; s++) {
226 double a = 360 * s * m / n;
227 if (s == 0) movep(5, a);
228 drawp(5, a, g);
229 }
230 }
231
232 public static void main(String args[]) {
233 gui.ClosableJFrame cf = new gui.ClosableJFrame();
234 java.awt.Container c = cf.getContentPane();
235 c.setLayout(new java.awt.FlowLayout());
236 c.add(getArchimedesSpiral());
237 //c.add(getArchimedesSpiral());
238 cf.setSize(200, 200);
239 cf.setVisible(true);
240 }
241 }
242