-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTreeWorld.java
381 lines (322 loc) · 9.91 KB
/
TreeWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/**
* SimpleCanvas.java
*
* Part of the basic graphics Template.
*
*/
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.swing.*;
import javax.swing.event.MouseInputListener;
import libraries.AdditiveComposite;
import libraries.GifSequenceWriter;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.TimerTask;
import java.util.Timer;
//stores the tree's generation rules, generates and draws tree
public class TreeWorld extends JPanel implements MouseInputListener, KeyListener
{
Random rand = new Random();
Color colorBackground = Color.black;
Color colorAxis = Color.red;
Color rootColor = Color.lightGray;
TreeBranch3D tree;
double controlSize = 1;
double rootSize;
ArrayList <TreeBranch3D> activeBranches = new ArrayList <TreeBranch3D>();
TreeData3D trunk_rules;
TreeData3D branch_rules;
TreeData3D trunk_tip_rules;
TreeData3D branch_tip_rules;
boolean enable_tip_rules = false;
Point3D goal = new Point3D(0,-1,0);
double rotation; //camera angle
Timer timer = new Timer();
TimerTask task;
Timer rotate_timer = new Timer();
TimerTask rotate_task;
public TreeWorld ()
{
trunk_tip_rules = trunk_rules = TreeData3D.DefaultTrunk();
branch_tip_rules = branch_rules = TreeData3D.DefaultBranch();
setPreferredSize(new Dimension(900,900));
setBackground(Color.black);
//startTree();
addMouseMotionListener(this);
addMouseListener(this);
addKeyListener(this);
setFocusable(true);
}
private void startTree() {
if (task != null)
task.cancel();
activeBranches.clear();
double average_size = (trunk_rules.size+branch_rules.size)/2;
rootSize = 120/average_size*controlSize;
tree = new TreeBranch3D(0, -100, 0, rootSize, rootColor);
activeBranches.add(tree);
setTimer();
}
private class MyTimerTask extends TimerTask {
public void run() {
if (!step()) { //step until tree is grown
cancel();
}
}
}
private boolean step(){
ArrayList<TreeBranch3D> newBranches = new ArrayList<TreeBranch3D>();
ArrayList<TreeBranch3D> deadBranches = new ArrayList<TreeBranch3D>();
for (TreeBranch3D branch: activeBranches) {
if (branch.grow(10)){
TreeBranch3D baby_trunk;
TreeBranch3D baby_branch;
if (enable_tip_rules) {
double size = branch.size/rootSize;
baby_trunk = branch.makeBaby(TreeData3D.blend(trunk_tip_rules, trunk_rules, size), goal);
baby_branch = branch.makeBaby(TreeData3D.blend(branch_tip_rules, branch_rules, size), goal);
}
else { //use root rules for whole tree
baby_trunk = branch.makeBaby(trunk_rules, goal);
baby_branch = branch.makeBaby(branch_rules, goal);
}
if (baby_trunk != null) {
newBranches.add(baby_trunk);
}
if (baby_branch != null) {
newBranches.add(baby_branch);
}
deadBranches.add(branch);
}
}
for (TreeBranch3D branch : deadBranches) {
activeBranches.remove(branch);
}
activeBranches.addAll(newBranches);
repaint();
if (activeBranches.isEmpty()) {
return false;
}
return true;
}
public void setMode(boolean rules) {
enable_tip_rules = rules;
branch_tip_rules = branch_rules;
trunk_tip_rules = trunk_rules;
}
public void setRules(TreeData3D rules, int branch_number){
if (branch_number == 0)
{trunk_rules = rules;}
else
{branch_rules = rules;}
startTree();
}
public void setRules(TreeData3D rules1, TreeData3D rules2, int branch_number){
if (branch_number == 0)
{trunk_rules = rules1;
trunk_tip_rules = rules2;}
else
{branch_rules = rules1;
branch_tip_rules = rules2;}
startTree();
}
public void randomizeRules(){
trunk_rules = TreeData3D.Random(trunk_rules.min_size, trunk_rules.color_warp);
branch_rules = TreeData3D.Random(branch_rules.min_size, branch_rules.color_warp);
if (enable_tip_rules) {
trunk_tip_rules = TreeData3D.Random(trunk_rules.min_size, trunk_rules.color_warp);
branch_tip_rules = TreeData3D.Random(branch_rules.min_size, branch_rules.color_warp);
}
startTree();
}
public void setRootColor(Color c) {
rootColor = c;
startTree();
}
public void setRootSize(Double s) {
controlSize = s;
startTree();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
g2d.setXORMode(Color.white);
g2d.setComposite(new AdditiveComposite(getBackground()));
g2d.translate(getWidth()/2,getHeight()-50);//origin is on bottom of window
tree.drawMe(g2d, Math.sin(rotation), Math.cos(rotation));
}
private void setTimer() {
timer = new Timer();
task = new MyTimerTask();
timer.scheduleAtFixedRate(task, 0, 50);
}
public void growGIF(){
double rotate = Math.PI/60;
int end_frames = 20;
String name = "grow_tree";
File saveFile;
int n = 0;
do {
n++;
saveFile = new File(name + "_" + n +".gif");
}
while (saveFile.exists());
System.out.println("saving growth as: " + saveFile.getAbsolutePath());
try{
FileImageOutputStream outputstream = new FileImageOutputStream(saveFile);
GifSequenceWriter writer = new GifSequenceWriter( outputstream, BufferedImage.TYPE_INT_RGB, 1, true);
startTree();
task.cancel();
int i = 0;
while (step()) {
rotate(rotate);
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
printAll(g);
writer.writeToSequence(image);
i++;
System.out.print(i + "-");
}
System.out.println();
for (int a = 0; a < end_frames; a++) {
rotate(Math.PI*2/120);
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
printAll(g);
writer.writeToSequence(image);
i++;
System.out.print(i + "-");
}
writer.close();
outputstream.close();
System.out.println();
System.out.println("saved to gif: " + saveFile.getAbsolutePath());
}
catch(Exception e){
System.out.println("Oops!");
e.printStackTrace();
}
}
public void saveToGIF(){
String name = "tree";
File saveFile;
int n = 0;
do {
n++;
saveFile = new File(name + "_" + n +".gif");
}
while (saveFile.exists());
System.out.println("saving as: " + saveFile.getAbsolutePath());
try{
FileImageOutputStream outputstream = new FileImageOutputStream(saveFile);
GifSequenceWriter writer = new GifSequenceWriter( outputstream, BufferedImage.TYPE_INT_RGB, 1, true);
for (int i = 0; i < 60; i++) {
rotate(Math.PI*2/60);
BufferedImage image = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
printAll(g);
writer.writeToSequence(image);
System.out.print(i + "-");
}
writer.close();
outputstream.close();
System.out.println();
System.out.println("saved to gif: " + saveFile.getAbsolutePath());
}
catch(Exception e){
System.out.println("Oops!");
e.printStackTrace();
}
}
public void saveImage(String image_path) {
File saveFile = new File(image_path + ".png");
int i = 0;
while (saveFile.exists()) {
saveFile = new File(image_path + i +".png");
i++;
}
BufferedImage awtImage = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics g = awtImage.getGraphics();
printAll(g);
try {
// retrieve image
ImageIO.write(awtImage, "png", saveFile);
System.out.println("Saving your image to " + (saveFile.getAbsolutePath()) );
} catch (IOException e) {
System.out.println("can't save image :(");
e.printStackTrace();
}
}
//click to make a new graph
public void mouseClicked(MouseEvent e)
{
goal = getWorldCoordinates(e.getX(), e.getY());
startTree();
}
//Empty methods to satisfy MouseListener interface
public void mouseEntered(MouseEvent e){
requestFocus();
}
public void mouseExited(MouseEvent e){
//task.cancel();
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void rotate(double amount) {
rotation = (rotation + amount)%(Math.PI*2);
repaint();
}
//get direction in 3space of mouse
public Point3D getWorldCoordinates(int xxx, int yyy) {
double x = -(double)xxx+getWidth()/2;
double y= (double)yyy-getHeight();;
Point3D point = new Point3D(x, y, 0);
point = TreeBranch3D.rotateAroundY(point, Math.sin(-rotation), Math.cos(-rotation));
point.printPoint();
return point;
}
@Override
public void mouseDragged(MouseEvent e) {
goal = getWorldCoordinates(e.getX(), e.getY());
//rotate(.01);
}
@Override
public void mouseMoved(MouseEvent arg0) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();
switch( keyCode ) {
case KeyEvent.VK_LEFT:
rotate(.05);
break;
case KeyEvent.VK_RIGHT :
rotate(-.05);
break;
case KeyEvent.VK_SPACE :
saveImage("my_tree");
break;
case KeyEvent.VK_G :
growGIF();
break;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}// SimpleCanvas