-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerrainGUI.java
More file actions
638 lines (577 loc) · 21.8 KB
/
Copy pathTerrainGUI.java
File metadata and controls
638 lines (577 loc) · 21.8 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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.ListCellRenderer;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
/**
* Generates Terrain GUI: Calls on Display.java and Rotate.java based on Event
*/
public class TerrainGUI {
private static int algorithm;
private static int size;
private static int color;
private static int mapType;
private static int dev;
private static double zoom;
private static double phi;
private static double theta;
private static double luminance;
private static Point light;
private static boolean showAxes;
private static Point[][] mat;
// Swing components
// Shows and adjusts algorithms
private static JComboBox<String> algBox;
// Shows and adjusts preset colors
private static JComboBox<String> colorBox;
// Shows and adjusts mapType
private static JComboBox<String> mapTypeBox;
// Shows and adjusts size
private static JSlider sizeSlider;
// Shows and adjusts dev
private static JSlider devSlider;
// Zoom In/Out should call on set x-scale/y-scale (may need to call repaint());
// JSlider should zoom in and out of panel with displayed terrain
// Later: Clicking a specific location and zooming in or out changes x and y-scales and then zooms
private static JSlider zoomSlider;
// Shows and adjusts phi
private static JSlider phiSlider;
// Shows and adjusts theta
private static JSlider thetaSlider;
// Shows and adjusts luminance
private static JSlider lumSlider;
// Shows and adjust light (direction)
private static JSlider lightSlider;
// Shows or hides helper axes
private static JButton axes;
// Reset fields to default values
private static JButton reset;
// Reapplies map generation to current field values
private static JButton reapply;
// Shows and adjusts Perlin Noise variables
final static JPanel perlinPanel = Algorithms.getPerlinPanel();
final static List<String> ALGORITHMS = Arrays.asList("Midpoint Displacement", "Diamond Square", "Perlin Noise");
final static List<String> COLORS = Arrays.asList("Gray", "Red", "Green", "Blue");
final static List<String> MAP_TYPES = Arrays.asList("Points", "Mesh", "Terrain");
// Minimum and maximum values for sliders
final static int MIN_SIZE = 0;
final static int MAX_SIZE = 10;
final static int MIN_DEV = 0;
final static int MAX_DEV = 10;
final static int MIN_ZOOM = 0;
final static int MAX_ZOOM = 200;
final static int MIN_PHI = -90;
final static int MAX_PHI = 90;
final static int MIN_THETA = -180;
final static int MAX_THETA = 180;
final static int MIN_LUM = 0;
final static int MAX_LUM = 100;
final static int MIN_LIGHT = -180;
final static int MAX_LIGHT = 180;
// Adds components to two subpanels added to JPanel in StdDraw.init()
public static void addComponents(JPanel components0, JPanel components1) {
components0.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.GRAY, Color.GRAY));
components1.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED, Color.GRAY, Color.GRAY));
// initializes to-be added components
initComponents();
// new JPanel containing JComboBoxes
JPanel comboBoxes = new JPanel();
comboBoxes.setLayout(new BoxLayout(comboBoxes, BoxLayout.Y_AXIS));
comboBoxes.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
comboBoxes.add(algBox);
comboBoxes.add(colorBox);
comboBoxes.add(mapTypeBox);
components0.add(comboBoxes);
// adds invisible component (space)
components0.add(Box.createVerticalStrut(20));
// new JPanel containing JSliders
JPanel sliders0 = new JPanel();
sliders0.setLayout(new BoxLayout(sliders0, BoxLayout.Y_AXIS));
sliders0.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
sliders0.add(sizeSlider);
sliders0.add(devSlider);
sliders0.add(zoomSlider);
sliders0.add(phiSlider);
sliders0.add(thetaSlider);
components0.add(sliders0);
// adds JPanel with Perlin Noise variables
components0.add(perlinPanel);
// adds JPanel with custom color slider
components1.add(new CustomColorSliderPanel());
components1.add(Box.createVerticalStrut(20));
// new JPanel and other JSliders
JPanel sliders1 = new JPanel();
sliders1.setLayout(new BoxLayout(sliders1, BoxLayout.Y_AXIS));
sliders1.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
sliders1.add(lumSlider);
sliders1.add(lightSlider);
components1.add(sliders1);
components1.add(Box.createVerticalStrut(20));
// new JPanel containing Jbuttons
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout());
buttons.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
buttons.add(axes);
buttons.add(reapply);
buttons.add(reset);
components1.add(buttons);
}
// Initializes GUI setup
public static void initComponents() {
// JComboBoxes
algBox = new JComboBox<String>(ALGORITHMS.toArray(new String[0]));
algBox.setRenderer(new ComboBoxRenderer("ALGORITHM"));
colorBox = new JComboBox<String>(COLORS.toArray(new String[0]));
colorBox.setRenderer(new ComboBoxRenderer("COLOR"));
mapTypeBox = new JComboBox<String>(MAP_TYPES.toArray(new String[0]));
mapTypeBox.setRenderer(new ComboBoxRenderer("DISPLAY"));
// JSliders (Shows and adjust respective fields)
sizeSlider = new JSlider(MIN_SIZE, MAX_SIZE);
Hashtable<Integer, JLabel> sizeLabels = new Hashtable<>();
for (int i = 0; i <= 10; i++) {
sizeLabels.put(i, new JLabel(Integer.toString(i)));
}
setSlider(sizeSlider, "Size", sizeLabels);
sizeSlider.setMajorTickSpacing(1);
sizeSlider.setSnapToTicks(true);
devSlider = new JSlider(MIN_DEV, MAX_DEV);
// devLabels = sizeLabels
setSlider(devSlider, "Deviation", sizeLabels);
devSlider.setMajorTickSpacing(1);
devSlider.setSnapToTicks(true);
// nonlinear scale from 0 to 1, then linear from 1 to 2
// if zoom >= 1, then val = 100*zoom
// else val = 200 - 100/zoom
zoomSlider = new JSlider(MIN_ZOOM, MAX_ZOOM);
Hashtable<Integer, JLabel> zoomLabels = new Hashtable<>();
zoomLabels.put(0, new JLabel("50%"));
zoomLabels.put(100, new JLabel("100%"));
zoomLabels.put(200, new JLabel("200%"));
setSlider(zoomSlider, "Zoom", zoomLabels);
phiSlider = new JSlider(MIN_PHI, MAX_PHI);
Hashtable<Integer, JLabel> phiLabels = new Hashtable<>();
phiLabels.put(-90, new JLabel("-90°"));
phiLabels.put(0, new JLabel("0°"));
phiLabels.put(90, new JLabel("90°"));
setSlider(phiSlider, "Phi (X-axis Rotation)", phiLabels);
phiSlider.setMajorTickSpacing(45);
thetaSlider = new JSlider(MIN_THETA, MAX_THETA);
Hashtable<Integer, JLabel> thetaLabels = new Hashtable<>();
thetaLabels.put(-180, new JLabel("-180°"));
thetaLabels.put(0, new JLabel("0°"));
thetaLabels.put(180, new JLabel("180°"));
setSlider(thetaSlider, "Theta (Z-axis Rotation)", thetaLabels);
thetaSlider.setMajorTickSpacing(45);
lumSlider = new JSlider(MIN_LUM, MAX_LUM);
Hashtable<Integer, JLabel> lumLabels = new Hashtable<>();
for (int i = 0; i <= 100; i += 10) {
lumLabels.put(i, new JLabel(Integer.toString(i)));
}
setSlider(lumSlider, "Luminance", lumLabels);
lumSlider.setMajorTickSpacing(10);
lightSlider = new JSlider(MIN_LIGHT, MAX_LIGHT);
// lightLabels = thetaLabels
setSlider(lightSlider, "Light Angle", thetaLabels);
lightSlider.setMajorTickSpacing(45);
// JButtons
axes = new JButton("Axes", new ImageIcon("icons/axes.png"));
reapply = new JButton("Reapply", new ImageIcon("icons/reapply.png"));
reset = new JButton("Reset", new ImageIcon("icons/reset.png"));
}
// Sets state of sliders
public static void setSlider(JSlider slider, String title, Hashtable<Integer, JLabel> labels) {
TitledBorder titledBorder = BorderFactory.createTitledBorder(title);
titledBorder.setTitleJustification(TitledBorder.CENTER);
slider.setBorder(titledBorder);
slider.setPaintTicks(true);
slider.setLabelTable(labels);
slider.setPaintLabels(true);
slider.setPreferredSize(new Dimension(300, 70));
}
// Following methods take no argument for simplicity and due to event listeners keeping track of class state (field values).
// Class state must be maintained for features like reapply and rotate to work.
// Additionally, this supports code scalability.
// Initializes Terrain GUI
public static void initGUI() {
// intializes StdDraw (calls StdDraw.init(), which calls addComponents())
StdDraw.setCanvasSize(700, 700);
StdDraw.enableDoubleBuffering();
// initial display
initDisplay();
// adds listeners (must be called after initDisplay() to prevent event firing)
addListeners();
}
// Adds event listeners to Swing components
public static void addListeners() {
// combo boxes' listeners
algBox.addActionListener(ae -> {
int selAlg = ALGORITHMS.indexOf(algBox.getSelectedItem());
if (selAlg != algorithm) {
algorithm = selAlg;
reapply();
}
});
colorBox.addActionListener(ae -> {
color = COLORS.indexOf(colorBox.getSelectedItem());
setColor();
display();
});
mapTypeBox.addActionListener(ae -> {
int selMapType = MAP_TYPES.indexOf(mapTypeBox.getSelectedItem());
if (selMapType != mapType) {
mapType = selMapType;
display();
}
});
// sliders' listeners
sizeSlider.addChangeListener(ce -> {
int sizeVal = (int) Math.pow(2, sizeSlider.getValue()) + 1;
if (sizeVal != size) {
size = sizeVal;
setScale();
initMatrix();
reapply();
}
});
devSlider.addChangeListener(ce -> {
int devVal = devSlider.getValue();
if (devVal != dev) {
dev = devVal;
reapply();
}
});
zoomSlider.addChangeListener(ce -> {
int zoomVal = zoomSlider.getValue();
if (zoomVal >= 100) zoom = zoomVal/100.0;
else zoom = 100.0/(200-zoomVal);
setScale();
display();
});
phiSlider.addChangeListener(ce -> {
phi = phiSlider.getValue();
setPhi();
rotate();
display();
});
thetaSlider.addChangeListener(ce -> {
theta = thetaSlider.getValue();
setTheta();
rotate();
display();
});
lumSlider.addChangeListener(ce -> {
luminance = lumSlider.getValue()/100.0;
setLum();
display();
});
lightSlider.addChangeListener(ce -> {
double rad = lightSlider.getValue() / 180.0 * Math.PI;
light = new Point(Math.cos(rad), Math.sin(rad), 0);
setLight();
display();
});
// buttons' listeners
axes.addActionListener(ae -> {
if (showAxes) showAxes = false;
else showAxes = true;
display();
});
reapply.addActionListener(ae -> {
reapply();
});
reset.addActionListener(ae -> {
setDefaultGUI();
initDisplay();
display();
});
}
// Rerenders (common action taken by event listeners)
public static void reapply() {
setMatrix();
setAlg();
rotate();
display();
}
// Displays intial or reset map (called by reset's Listener)
public static void initDisplay() {
// sets initial JComboBoxes display
algBox.setSelectedIndex(algorithm);
colorBox.setSelectedIndex(color);
mapTypeBox.setSelectedIndex(mapType);
// sets intial JSliders display
sizeSlider.setValue((int) (Math.log(size-1)/Math.log(2)));
devSlider.setValue(dev);
zoomSlider.setValue(zoom >= 1 ? 100 * (int) zoom : 200 - (int) (100/zoom));
phiSlider.setValue((int) phi);
thetaSlider.setValue((int) theta);
lumSlider.setValue((int) (100*luminance));
lightSlider.setValue((int) (0));
// sets default luminance
setLum();
// sets default light
setLight();
// sets default phi
setPhi();
// sets default theta
setTheta();
// initializes and sets matrix
initMatrix();
setMatrix();
// sets scale
setScale();
// sets color
setColor();
// sets algorithm
setAlg();
// rotates default phi and theta
rotate();
// displays map
display();
}
// Displays map (called by every listener)
public static void display() {
StdDraw.clear();
if (mapType != -1 && algorithm != -1) {
if (mapType == 0) Display.displayPoints(mat);
else if (mapType == 1) Display.displayMesh(mat);
else Display.displayTerrain(mat);
if (showAxes) Display.displayAxes(phi, theta, size);
}
StdDraw.show();
}
// Initializes matrix (called by sizeSlider's listener)
public static void initMatrix() {
mat = new Point[size][size];
}
// Sets matrix (called by {sizeSlider, reapply}'s listeners)
public static void setMatrix() {
Transform.setMatrix(mat);
}
// Sets the algorithm (called by {algBox, sizeSlider, devSlider, reset}'s listener)
public static void setAlg() {
Algorithms.setAlg(algorithm);
if (algorithm != -1) {
if (algorithm == 2) perlinPanel.setVisible(true);
else perlinPanel.setVisible(false);
Algorithms.setDev(dev/10.0);
Algorithms.setMatrix(mat);
// keeps copy of transformed matrix
Transform.algMatrix(mat);
}
else perlinPanel.setVisible(false);
}
// Sets the scale (called by {zoomSlider, sizeSlider}'s listeners)
public static void setScale() {
StdDraw.setXscale(-0.7*size/zoom, 0.7*size/zoom);
StdDraw.setYscale(-0.7*size/zoom, 0.7*size/zoom);
StdDraw.setPenRadius(1.0/(size/zoom*20));
}
// Sets luminance (called by lumSlider's listener)
public static void setLum() {
Display.setLuminance(luminance);
}
// Sets light (called by lightSlider's listenerr)
// Precond: field light is normalized
public static void setLight() {
Display.setLight(light);
}
// Sets phi (called by phiSlider's listener)
public static void setPhi() {
Display.setPhi(phi);
}
// Sets theta (called by thetaSlider's listener)
public static void setTheta() {
Display.setTheta(theta);
}
// Rotates phi degrees about the x-axis and theta degrees about z-axis (called by {phiSlider, thetaSlider}'s Listeners)
public static void rotate() {
if (algorithm != -1) Transform.rotate(mat, phi, theta);
}
// Sets rgb values dependent on preset colors in colorBox (called by colorBox's listener)
// Note: colors are set to light to provide light and height (z-coord) contrast
public static void setColor() {
if (color != -1) {
if (color == 0) {
// GRAY
Display.setColor(204, 204, 204);
}
else if (color == 1) {
// RED
Display.setColor(255, 102, 102);
}
else if (color == 2) {
// GREEN
Display.setColor(102, 255, 102);
}
else {
// BLUE
Display.setColor(51, 204, 255);
}
}
}
// Sets default field values (called by reset's listener and main())
public static void setDefaultGUI() {
algorithm = -1;
size = 33;
color = -1;
mapType = -1;
dev = 5;
zoom = 1;
phi = 45;
theta = 0;
luminance = 0.5;
light = new Point(1, 0, 0);
showAxes = false;
}
// Called directly from TerrainMap.java and changes default values
public static void setTerrainGUI(int terAlgorithm, int terSize, int terColor, int terMapType, int terDeviation) {
algorithm = terAlgorithm;
size = terSize;
color = terColor;
mapType = terMapType;
dev = terDeviation;
zoom = 1;
phi = 45;
theta = 0;
luminance = 0.5;
light = new Point(0, 1, 0);
showAxes = false;
}
public static void main(String[] args) {
setDefaultGUI();
initGUI();
}
}
// Renderer used by comboBox components
class ComboBoxRenderer extends JLabel implements ListCellRenderer<String> {
private Color defaultBackgroundColor = Color.LIGHT_GRAY;
private String title;
public ComboBoxRenderer(String title) {
setOpaque(true);
this.title = title;
}
@Override
// Gets JLabel with selected icon and text
public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) {
setBackground(list.getBackground());
if (isSelected) setBackground(defaultBackgroundColor);
if (index == -1 && value == null) {
setIcon(null);
setText(title);
}
else {
setIcon(new ImageIcon("icons/" + value + ".png"));
setText(value);
}
return this;
}
}
// Custom color slider panel
class CustomColorSliderPanel extends JPanel {
// JPanel with rgb sliders
private JPanel rgbSliders = new JPanel();
private JSlider rSlider = new JSlider(0, 255, 128);
private JSlider gSlider = new JSlider(0, 255, 128);
private JSlider bSlider = new JSlider(0, 255, 128);
// JPanel with canvas and button
private JPanel colorPanel = new JPanel();
// Canvas to show color
private ColorDisplay colorDisplay = new ColorDisplay();
// JButton to apply color to canvas
private JButton apply = new JButton("Apply");
private int r = 128;
private int g = 128;
private int b = 128;
public CustomColorSliderPanel() {
setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
setPanels();
addPanelComponents();
add(rgbSliders);
add(colorPanel);
colorDisplay.setBackgroundColor();
}
// sets JPanels
public void setPanels() {
rgbSliders.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
rgbSliders.setLayout(new BoxLayout(rgbSliders, BoxLayout.Y_AXIS));
colorPanel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
colorPanel.setLayout(new BoxLayout(colorPanel, BoxLayout.Y_AXIS));
}
// adds panel components: rgb sliders, canvas, and button
public void addPanelComponents() {
setRGBSliders();
addListeners();
rgbSliders.add(rSlider);
rgbSliders.add(gSlider);
rgbSliders.add(bSlider);
colorPanel.add(colorDisplay);
colorPanel.add(Box.createVerticalStrut(20));
apply.setAlignmentX(JButton.CENTER_ALIGNMENT);
colorPanel.add(apply);
}
// sets rgb sliders
public void setRGBSliders() {
Hashtable<Integer, JLabel> rgbLabels = new Hashtable<>();
rgbLabels.put(0, new JLabel("0"));
rgbLabels.put(255, new JLabel("255"));
TerrainGUI.setSlider(rSlider, "Red", rgbLabels);
TerrainGUI.setSlider(gSlider, "Green", rgbLabels);
TerrainGUI.setSlider(bSlider, "Blue", rgbLabels);
rSlider.setPreferredSize(new Dimension(200, 70));
gSlider.setPreferredSize(new Dimension(200, 70));
bSlider.setPreferredSize(new Dimension(200, 70));
}
// adds listeners
public void addListeners() {
rSlider.addChangeListener(ce -> {
int rVal = rSlider.getValue();
if (rVal != r) {
r = rVal;
colorDisplay.setBackgroundColor();
}
});
gSlider.addChangeListener(ce -> {
int gVal = gSlider.getValue();
if (gVal != g) {
g = gVal;
colorDisplay.setBackgroundColor();
}
});
bSlider.addChangeListener(ce -> {
int bVal = bSlider.getValue();
if (bVal != b) {
b = bVal;
colorDisplay.setBackgroundColor();
}
});
apply.addActionListener(ae -> {
Display.setColor(r, g, b);
TerrainGUI.display();
});
}
class ColorDisplay extends Canvas {
public ColorDisplay() {
setSize(80, 80);
setBackgroundColor();
}
public void setBackgroundColor() {
setBackground(new Color(r, g, b));
}
}
}