-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProject3_Codebase.java
More file actions
464 lines (382 loc) · 18.3 KB
/
Project3_Codebase.java
File metadata and controls
464 lines (382 loc) · 18.3 KB
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Stack;
public class Project3_Codebase {
//UI Variables
static int WIDTH = 500;
static int HEIGHT = 500;
static BufferedImage Display;
static RulePanel r1;
static RulePanel r2;
static JTextField width,length, angle, start, iteration;
//Panning and Zooming Variables
static float TurtleScale = 1.0f;
static float XOffset = 0;
static float YOffset = 0;
//Animation Variables
static Timer AnimationTimer;
static int TurtleStepCount = 0;
//LSystem
static String LSystemString = "";
// static int iterations = Integer.parseInt(iteration.getText());
// static String starting_axiom = start.getText();
// //inputs
static float stroke_width;// global because animationa nad run need them
static float segment_length ;
static boolean draw_turtle;
static float angles ;
//Rules, made by extending the JPanel class to create a new component
static class RulePanel extends JPanel {
String name;
JLabel nameLabel;
JTextField word;
JTextField rule;
RulePanel(String name){
this.setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
nameLabel = new JLabel(name);
word = new JTextField("A");
rule = new JTextField("AB");
rule.setPreferredSize(new Dimension(100,25));
this.add(nameLabel);
this.add(word);
this.add(rule);
}
public String GetWord(){
return word.getText();
}
public String GetRule(){
return rule.getText();
}
}
public static void main(String[] args) {
//make our UI on EDT using invokeLater
//have an action listener on a button that will make and start a new thread for calucation/image drawing
//when that thread makes the image, repaint the window on the EDT using invoke later
SwingUtilities.invokeLater(new Runnable() {
public void run() {
//Creating the window
JFrame window = new JFrame("L-System");
window.setPreferredSize(new Dimension(WIDTH + 100, HEIGHT + 50));
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
//Display panel containing the BufferedImage
JPanel DisplayPanel = new JPanel();
DisplayPanel.setBackground(Color.ORANGE);
Display = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
Display.setRGB(250, 250, Color.BLACK.getRGB());
DisplayPanel.add(new JLabel(new ImageIcon(Display)));
window.add(DisplayPanel, BorderLayout.CENTER);
//==============Menu Bar===============
//sets up some default configurations for different L-systems
JMenuBar MenuBar = new JMenuBar();
window.setJMenuBar(MenuBar);
JMenu Configure = new JMenu("Load Configuration");
MenuBar.add(Configure);
JMenuItem bin_tree = new JMenuItem("Binary Tree");
bin_tree.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
angle.setText("45");
start.setText("0");
width.setText("1");
r1.word.setText("0");
r1.rule.setText("1[*<0]>0");
r2.word.setText("1");
r2.rule.setText("11");
}
});
Configure.add(bin_tree);
JMenuItem Sierpinksi = new JMenuItem("Sierpinksi Triangle");
Sierpinksi.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
angle.setText("120");
start.setText("F>F>F");
width.setText("1");
r1.word.setText("F");
r1.rule.setText("F>G<F<G>F");
r2.word.setText("G");
r2.rule.setText("GG");
}
});
Configure.add(Sierpinksi);
JMenuItem Dragon = new JMenuItem("Dragon Curve");
Dragon.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
angle.setText("90");
start.setText("F");
width.setText("1");
r1.word.setText("F");
r1.rule.setText("F<G");
r2.word.setText("G");
r2.rule.setText("F>G");
}
});
Configure.add(Dragon);
JMenuItem Fern = new JMenuItem("Barnsley Fern");
Fern.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
angle.setText("25");
start.setText("X");
width.setText("10");
r1.word.setText("X");
r1.rule.setText("F>--[[X]<X]<F[>FX]>X");
r2.word.setText("F");
r2.rule.setText("FF");
}
});
Configure.add(Fern);
//==========Config panel=============
JPanel Configuration = new JPanel();
Configuration.setBackground(new Color(230, 230, 230));
Configuration.setPreferredSize(new Dimension(250, 500));
Configuration.setLayout(new FlowLayout());
//iterations
JPanel Iterations = new JPanel();
Iterations.add(new JLabel("Number of Iterations"));
iteration = new JTextField("5");
iteration.setPreferredSize(new Dimension(50, 20));
Iterations.add(iteration);
Configuration.add(Iterations);
//Angle
JPanel Angle = new JPanel();
Angle.add(new JLabel("Turn Angle"));
angle = new JTextField("45");
angle.setPreferredSize(new Dimension(50, 20));
Angle.add(angle);
Configuration.add(Angle);
//Length
JPanel Length = new JPanel();
Length.add(new JLabel("Segment Length"));
length = new JTextField("10");
length.setPreferredSize(new Dimension(50, 20));
Length.add(length);
Configuration.add(Length);
//Width
JPanel Width = new JPanel();
Width.add(new JLabel("Segment Width"));
width = new JTextField("1");
width.setPreferredSize(new Dimension(50, 20));
Width.add(width);
Configuration.add(Width);
//Start
JPanel Start = new JPanel();
Start.add(new JLabel("Start"));
start = new JTextField("A");
start.setPreferredSize(new Dimension(50, 20));
Start.add(start);
Start.setPreferredSize(new Dimension(150, 25));
Configuration.add(Start);
r1 = new RulePanel("Rule 1");
Configuration.add(r1);
r2 = new RulePanel("Rule 2");
Configuration.add(r2);
JCheckBox DrawTurtleCheck = new JCheckBox("Draw Turtle");
Configuration.add(DrawTurtleCheck);
JPanel SpeedPanel = new JPanel();
SpeedPanel.add(new JLabel("Speed:"));
JSlider Speed = new JSlider(1,1000);
Speed.setValue(50);
Speed.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
//TODO: Adjust the initial delay of the timer when the slider moves
int awaittime = Speed.getValue();
if (AnimationTimer == null ) { //but is it running DOUBLECHECK
AnimationTimer = new Timer(awaittime, null);
}AnimationTimer.setDelay(awaittime);
}
});
SpeedPanel.add(Speed);
Configuration.add(SpeedPanel);
//Run Button
JButton Run = new JButton("Generate");
Run.setPreferredSize(new Dimension(200,50));
Run.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
HashMap<Character,String> rulemap = new HashMap<Character, String>();
if(r1.GetWord().length()!=0)
rulemap.put(r1.GetWord().charAt(0),r1.GetRule());
if(r2.GetWord().length()!=0)
rulemap.put(r2.GetWord().charAt(0),r2.GetRule());
int iterations = Integer.parseInt(iteration.getText());
String starting_axiom = start.getText();
//inputs
stroke_width=Float.parseFloat(width.getText());
segment_length = Float.parseFloat(length.getText());
draw_turtle=DrawTurtleCheck.isSelected();
angles = Integer.parseInt(angle.getText());
//TODO: Generate the L-system string, then draw it. Later, draw it in a Timer to animate it.
//wait does this mean to draw it inside of run?
LSystemString= Project3_Functions.Generate_LSystem(iterations,starting_axiom,rulemap);
TurtleStepCount=0;
if (AnimationTimer != null) {
AnimationTimer.stop();
}//consider creating a new method to fix speed
//TODO FIND ANIMATIONGLITCH!!!!
//TODO do: reserch on animation method
AnimationTimer.start();
//
}
});
Configuration.add(Run);
window.add(Configuration,BorderLayout.EAST);
AnimationTimer = new Timer(1000 - Speed.getValue(), new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (TurtleStepCount <= LSystemString.length()) {
new Thread(new Runnable() {
@Override
public void run() {
BufferedImage img = Draw_LSystem(LSystemString, segment_length, stroke_width, angles, draw_turtle);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
UpdateDisplay(img);
DisplayPanel.repaint();
}
});
}
}).start();
TurtleStepCount++;
} else if (AnimationTimer != null && AnimationTimer.isRunning()) {
AnimationTimer.stop();
}
}
});
//==========EVENTS==============
DisplayPanel.setFocusable(true);
DisplayPanel.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.getWheelRotation() < 0) {
TurtleScale *= 1.1f;
} else {
TurtleScale /= 1.1f;
}
TurtleScale = Math.max(0.1f, Math.min(TurtleScale, 10.0f));
BufferedImage img = Draw_LSystem(LSystemString, segment_length, stroke_width, angles, draw_turtle);
UpdateDisplay(img);
DisplayPanel.repaint();
}
});
DisplayPanel.addMouseMotionListener(new MouseMotionListener() {
int prevx;
int prevy;
@Override
public void mouseDragged(MouseEvent e) {
//TODO: Use the MouseEvent to modify the current X and Y offsets, then redraw the L-system at the new scale on its own thread, then repaint the UI on the EDT. You should account for the difference between the coordinate systems of the turtle and the JPanel
int secondx = e.getX() - prevx;
int secondy = e.getY() - prevy;
XOffset += secondx;
YOffset += secondy;
prevx = e.getX();
prevy = e.getY();
BufferedImage img = Draw_LSystem(LSystemString, segment_length, stroke_width, angles, draw_turtle);
UpdateDisplay(img);
DisplayPanel.repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
//TODO: Keep track of the mouse position even if the mouse isn't clicked
prevx = e.getX();
prevy = e.getY();
}
});
}
});
}
//Recommended function... not required to use this but this might be helpful.
//You can give it a known system string and implement the drawing part without needed to generate your own strings, for testing.
//You can also use in you timer to draw the system while animating.
static BufferedImage Draw_LSystem( String system, float segment_length, float width, float rotation_angle, boolean drawturtle){
BufferedImage img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
Graphics2D pen = img.createGraphics();
float maniwidth = width;
pen.setStroke(new BasicStroke(maniwidth));
pen.translate(WIDTH / 2 + XOffset, HEIGHT / 2 + YOffset);
pen.scale(TurtleScale, TurtleScale);
pen.rotate(Math.toRadians(180));
Stack<AffineTransform> posRecord = new Stack<>();
Stack<Float> wRecord = new Stack<>();
for (int i = 0; i < TurtleStepCount && i < system.length(); i++) {
char character = system.charAt(i);
switch (character) {
case '+':
maniwidth += 1;
pen.setStroke(new BasicStroke(maniwidth));
break;
case '-':
maniwidth -= 1;
if (maniwidth < 1) {//fixing weird behavior if it became neg
maniwidth = 1;
}
pen.setStroke(new BasicStroke(maniwidth));
break;
case '[':
posRecord.push(pen.getTransform());
wRecord.push(maniwidth);
break;
case ']':
pen.setTransform(posRecord.pop());
maniwidth = wRecord.pop();
pen.setStroke(new BasicStroke(maniwidth));
break;
case '>':
pen.rotate(Math.toRadians(rotation_angle));
break;
case '<':
pen.rotate(Math.toRadians(-rotation_angle));
break;
case '*':
pen.setColor(Color.RED);
int radius = (int) (segment_length / 4);
pen.fillOval(-radius, -radius, radius * 2, radius * 2);
pen.setColor(Color.GREEN);
break;
default:
pen.setColor(Color.GREEN);
pen.drawLine(0, 0, 0, (int) segment_length);
pen.translate(0, segment_length);
break;
}
}
if (drawturtle) {
AffineTransform savedtransf = pen.getTransform();
DrawTurtle(pen);
pen.setTransform(savedtransf);
}
pen.dispose();
return img;
}
//==========PROVIDED FUNCTIONS===========
static void UpdateDisplay(BufferedImage img){
Graphics2D g = (Graphics2D) Display.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(0,0,WIDTH,HEIGHT);
g.drawImage(img,0,0,null);
}
//Draws the Turtle at its current spot/orientation.
static void DrawTurtle(Graphics2D pen){
pen.setStroke(new BasicStroke(1)); //why is the width changing, reset stroke al.l together
//body (centered at current X,Y location)
pen.setColor(new Color(255,255,100));
pen.fillOval(-5,-5,10,10);
//right arm (facing in +X direction)
pen.setColor(new Color(150,50,50));
pen.drawLine(0,0,10,0);
//head (facing in +Y direction)
pen.setColor(new Color(50,100,50));
pen.drawLine(0,0,0,10);
}
}