-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyGui.java
More file actions
258 lines (207 loc) · 7 KB
/
MyGui.java
File metadata and controls
258 lines (207 loc) · 7 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
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
*
* @author Alex Weeks
*
*/
public class MyGui extends JFrame {
// put your instance fields here
private Universe uni;
private JButton pauseButton = new JButton();
private JButton speedUp = new JButton();
private JButton slowDown = new JButton();
private JButton changeColors = new JButton();
private JButton resetColors = new JButton();
private JButton addBall = new JButton();
private JButton toggleGravity = new JButton();
private Font defaultFont = new Font("Arial", 0, 12);
Thread universeThread;
/**
* Constructor for the GUI
*/
public MyGui() {
setLayout(null); // throw away the layout manager for this container
setSize(720,800); //change the size if you want
setLocation(20,40); //change the location if you want
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Elastic Collision Simulation");
//Initialize universe
uni = new Universe( new DoubleVector( new double[] {0, 0} ) );
uni.setSize(700, 650);
uni.setLocation(10, 10);
uni.setBackground(Color.GRAY);
uni.setVisible(true);
uni.setLayout(null);
this.getContentPane().add(uni);
//Pause button
pauseButton.setSize(150, 30);
pauseButton.setLocation(30, 670);
pauseButton.setText("Start / Stop");
pauseButton.setVisible(true);
pauseButton.addActionListener(new Pause());
pauseButton.setFont(defaultFont);
this.getContentPane().add(pauseButton);
//Speed up button
speedUp.setSize(150, 30);
speedUp.setLocation(200, 670);
speedUp.setText("Speed Up");
speedUp.setVisible(true);
speedUp.addActionListener(new SpeedUp());
speedUp.setFont(defaultFont);
this.getContentPane().add(speedUp);
//Slow down button
slowDown.setSize(150, 30);
slowDown.setLocation(350, 670);
slowDown.setText("Slow Down");
slowDown.setVisible(true);
slowDown.addActionListener(new SlowDown());
slowDown.setFont(defaultFont);
this.getContentPane().add(slowDown);
//Change colors button
changeColors.setSize(200, 30);
changeColors.setLocation(30, 700);
changeColors.setText("Randomize Colors");
changeColors.setVisible(true);
changeColors.addActionListener(new ChangeColors());
changeColors.setFont(defaultFont);
this.getContentPane().add(changeColors);
//Reset colors button
resetColors.setSize(200, 30);
resetColors.setLocation(30, 730);
resetColors.setText("Reset Colors");
resetColors.setVisible(true);
resetColors.addActionListener(new ResetColors());
resetColors.setFont(defaultFont);
this.getContentPane().add(resetColors);
//Add ball button
addBall.setSize(200, 30);
addBall.setLocation(250, 700);
addBall.setText("Add a Ball");
addBall.setVisible(true);
addBall.addActionListener(new AddBall());
addBall.setFont(defaultFont);
this.getContentPane().add(addBall);
//Toggle gravity button
toggleGravity.setSize(200, 30);
toggleGravity.setLocation(500, 700);
toggleGravity.setText("Gravity on/off");
toggleGravity.setVisible(true);
toggleGravity.addActionListener(new ToggleGravity());
toggleGravity.setFont(defaultFont);
this.getContentPane().add(toggleGravity);
//Initialize starting random balls
uni.addRandomActors(7);
JOptionPane.showMessageDialog(this,
"Please note: There are several known issues with this simulation, and I do not \n" +
"consider it complete from a simulation accuracy, or reliability standpoint. Known issues include: \n\n" +
"-Increasing the speed excessively causes objects to be lost\n" +
"-Adding an excessive number of balls both increases the simulation complexity, and increases the\n" +
"chances of objects becoming stuck within other objects, despite the great deal of effort\n" +
"I have put into preventing this.\n" +
"-Occasionally, even at normal speeds, and with a reasonable number of balls present,\n" +
"particularly when adding balls, a ball will be lost out of a wall or become stuck in another object.\n\n" +
"The majority of these issues stem from the unusual method of simulation: This program does not operate\n" +
"iteratively. It solves for moments of collision exactly, finds the soonest collision, performs the collision, and moves on. \n" +
"In particular, collisions involving more than two objects are not implemented, and if two objects should collide\n" +
"within a very short amount of time, one usually ends up inside another. \n\n" +
"Given more time, these problems could probably be solved, but alas the deadline is approaching...\n\n" +
"If the simulation freaks out, re-launching the program is the most sure way to solve it. Have fun :)");
universeThread = new Thread(uni);
universeThread.start();
}
/**
* Implenents start/stop action
*
*/
public class Pause implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ( universeThread == null ) {
universeThread = new Thread(uni);
universeThread.start();
} else {
universeThread.interrupt();
universeThread = null;
}
}
}
/**
*
* Speeds up simulation
*
*/
public class SpeedUp implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ( universeThread != null ) universeThread.interrupt();
uni.timeStep *= 1.25;
universeThread = new Thread(uni);
universeThread.start();
}
}
/**
* Slows down simulation
*
*/
public class SlowDown implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ( universeThread != null ) universeThread.interrupt();
uni.timeStep *= 4d/5d;
universeThread = new Thread(uni);
universeThread.start();
}
}
/**
* Randomizes colors of all balls
*
*/
public class ChangeColors implements ActionListener {
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
for ( Ball ball: uni.actors) {
ball.color = new Color( rnd.nextInt(255), rnd.nextInt(255), rnd.nextInt(255) );
}
uni.paintImmediately(0, 0, uni.getWidth(), uni.getWidth());
}
}
/**
* Resets all ball colors to black
*/
public class ResetColors implements ActionListener {
public void actionPerformed(ActionEvent e) {
Random rnd = new Random();
for ( Ball ball: uni.actors) {
ball.color = Color.BLACK;
}
uni.paintImmediately(0, 0, uni.getWidth(), uni.getWidth());
}
}
/**
* Adds a randomized ball
*/
public class AddBall implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ( universeThread != null ) universeThread.interrupt();
uni.addRandomActors(1);
universeThread = new Thread(uni);
universeThread.start();
}
}
/**
* Toggles gravity for the universe
*/
public class ToggleGravity implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ( universeThread != null ) universeThread.interrupt();
if ( uni.gravity.magnitude() == 0 ) {
uni.gravity = new DoubleVector( new double[] {0, 400} );
} else {
uni.gravity.scalarMultTo(0);
}
uni.nextCollision = uni.nextCollision();
universeThread = new Thread(uni);
universeThread.start();
}
}
}