-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainframe.java
More file actions
435 lines (399 loc) · 15.6 KB
/
Copy pathmainframe.java
File metadata and controls
435 lines (399 loc) · 15.6 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
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class mainframe extends JFrame implements ActionListener{
JButton newObject = new JButton("add new object");
JButton newLine = new JButton("add new surface");
JButton newCircle = new JButton("add new round block");
JButton renderSim = new JButton("render the simulation");
JButton playSim = new JButton("play the simulation");
JButton setTime = new JButton("set time values");
JCheckBox defaultSurface = new JCheckBox("set ground by default");
JCheckBox setbounds = new JCheckBox("set bounds");
ImageIcon img = new ImageIcon("Untitled.png");
JPanel p1 = new JPanel();
JLabel ti = new JLabel("set time increment (seconds):");
JLabel tm = new JLabel("set simulation time(seconds):");
JLabel useful = new JLabel();
world obj;
mainframe(world obj)
{
this.obj = obj;
newObject.addActionListener(this);
newLine.addActionListener(this);
newCircle.addActionListener(this);
renderSim.addActionListener(this);
playSim.addActionListener(this);
setTime.addActionListener(this);
playSim.setEnabled(false);
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(6,1,10,20));
buttonPanel.add(newObject);
buttonPanel.add(newLine);
buttonPanel.add(newCircle);
buttonPanel.add(renderSim);
buttonPanel.add(playSim);
buttonPanel.add(setTime);
buttonPanel.add(defaultSurface);
buttonPanel.add(setbounds);
buttonPanel.add(useful);
//this.add(simulator);
this.add(buttonPanel);
this.setTitle("Java Physics simulator");
this.setLayout(new FlowLayout());
this.setVisible(true);
this.setResizable(false);
this.setSize(1000,800);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setIconImage(img.getImage());
this.setLocationRelativeTo(null);
this.pack();
}
class getNewObjectDimensions extends JFrame implements ActionListener
{
JButton addtoframe = new JButton("add object");
JButton cancel = new JButton("cancel");
JTextField xvelocity;
JTextField yvelocity;
JTextField mass;
JTextField radius;
JTextField ypos;
JTextField xpos;
getNewObjectDimensions()
{
addtoframe.addActionListener(this);
cancel.addActionListener(this);
sphere s = new sphere();
boolean retry = true;
while(retry){
retry = false;
double radius = Math.random() * 20;
s = new sphere(new point(radius + Math.random() * (500 - radius * 2),radius + Math.random() * (500 - radius * 2)),radius,Math.random() * 1000);
for(int i = 0; i < world.balls.size(); i++)
{
if(world.balls.get(i).c.intersects(s.c))
{ retry = true;
break;
}
}
if(!retry)
break;
else{
System.out.println("retry detected");
}
}
xvelocity = new JTextField(" " + Math.random() * 50 * Math.pow(-1, (int)(Math.random() * 2)));
yvelocity = new JTextField(" " + Math.random() * 50 * Math.pow(-1, (int)(Math.random() * 2)));
mass = new JTextField(" " + Math.random() * 1000);
radius = new JTextField(" " + s.c.radius);
ypos = new JTextField(" " + s.c.center.x);
xpos = new JTextField(" " + s.c.center.y);
JLabel xvel = new JLabel("velocity along x axis");
JLabel yvel = new JLabel("velocity along y axis");
JLabel m = new JLabel("enter mass");
JLabel r = new JLabel("enter radius");
JLabel xp = new JLabel("enter x coordinate of position");
JLabel yp = new JLabel("enter y coordinate of position");
this.setLayout(new GridLayout(7,2,10,10));
this.add(xvel);
this.add(xvelocity);
this.add(yvel);
this.add(yvelocity);
this.add(m);
this.add(mass);
this.add(r);
this.add(radius);
this.add(xp);
this.add(xpos);
this.add(yp);
this.add(ypos);
this.add(addtoframe);
this.add(cancel);
this.setTitle("add new sphere");
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addtoframe)
{
vector vel = new vector();
point cn = new point();
double mas = 1, rad = 1;
try{
vel.x = Double.parseDouble(xvelocity.getText());
vel.y = Double.parseDouble(yvelocity.getText());
cn = new point(Double.parseDouble(xpos.getText()),Double.parseDouble(ypos.getText()));
mas = Double.parseDouble(mass.getText());
rad = Double.parseDouble(radius.getText());
}catch (NumberFormatException ex){
JOptionPane.showMessageDialog(this, "Invalid input! Please enter valid numeric values.");
this.dispose();
return;
}
world.addSphere(cn,vel,rad,mas);
this.dispose();
}
if(e.getSource() == cancel)
{
this.dispose();
}
}
}
private class getLineDimensions extends JFrame implements ActionListener
{
JButton addtoframe = new JButton("add surface");
JButton cancel = new JButton("cancel");
JTextField X1 = new JTextField();
JTextField X2 = new JTextField();
JTextField Y1 = new JTextField();
JTextField Y2 = new JTextField();
getLineDimensions()
{
cancel.addActionListener(this);
addtoframe.addActionListener(this);
JLabel X1label = new JLabel("enter x1");
JLabel X2label = new JLabel("enter x2");
JLabel Y1label = new JLabel("enter y1");
JLabel Y2label = new JLabel("enter y2");
this.setLayout(new GridLayout(5,2,10,10));
this.add(X1label);
this.add(X1);
this.add(X2label);
this.add(X2);
this.add(Y1label);
this.add(Y1);
this.add(Y2label);
this.add(Y2);
this.add(addtoframe);
this.add(cancel);
this.setTitle("add new surface");
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addtoframe)
{
double x1 = 0,y1 = 0,x2 = 500,y2 = 0;
try{
x1 = Double.parseDouble(X1.getText());
y1 = Double.parseDouble(Y1.getText());
x2 = Double.parseDouble(X2.getText());
y2 = Double.parseDouble(Y2.getText());
}
catch (NumberFormatException ex){
JOptionPane.showMessageDialog(this, "Invalid input! Please enter valid numeric values.");
this.dispose();
return;
}
world.addSurface(new point(x1,y1), new point(x2,y2));
this.dispose();
}
if(e.getSource() == cancel)
{
this.dispose();
}
}
}
private class getCircleDimensions extends JFrame implements ActionListener
{
JButton addtoframe = new JButton("add roundBlock");
JButton cancel = new JButton("cancel");
JTextField radius = new JTextField();
JTextField xpos = new JTextField();
JTextField ypos = new JTextField();
getCircleDimensions()
{
cancel.addActionListener(this);
addtoframe.addActionListener(this);
JLabel rlabel = new JLabel("enter radius of round block");
JLabel xlabel = new JLabel("enter x position of round block");
JLabel ylabel = new JLabel("enter y position of round block");
this.setLayout(new GridLayout(4,2,10,10));
this.add(rlabel);
this.add(radius);
this.add(xlabel);
this.add(xpos);
this.add(ylabel);
this.add(ypos);
this.add(addtoframe);
this.add(cancel);
this.setTitle("add new surface");
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addtoframe)
{
double x = 0,r = 0,y = 0;
try{
r = Double.parseDouble(radius.getText());
x = Double.parseDouble(xpos.getText());
y = Double.parseDouble(ypos.getText());
}
catch (NumberFormatException ex){
JOptionPane.showMessageDialog(this, "Invalid input! Please enter valid numeric values.");
this.dispose();
return;
}
world.addRoundBlock(new point(x,y),r);
this.dispose();
}
if(e.getSource() == cancel)
{
this.dispose();
}
}
}
private class setWorldValues extends JFrame implements ActionListener{
JButton addtoframe = new JButton("set world values");
JTextField A = new JTextField("0.01");
JTextField B = new JTextField("10");
JTextField C = new JTextField("9.8");
JTextField D = new JTextField("1.81e-5");
setWorldValues()
{
addtoframe.addActionListener(this);
JLabel Alabel = new JLabel("time increment of the world");
JLabel Blabel = new JLabel("simulation duration");
JLabel Clabel = new JLabel("gravity of world");
JLabel Dlabel = new JLabel("drag coefficient");
this.setLayout(new GridLayout(5,2,10,10));
this.add(Alabel);
this.add(A);
this.add(Blabel);
this.add(B);
this.add(Clabel);
this.add(C);
this.add(Dlabel);
this.add(D);
this.add(addtoframe);
this.setTitle("add new surface");
this.setVisible(true);
this.setSize(400,400);
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == addtoframe)
{
double inctime = 0.1;
double maxtime = 10;
double grav = -9.8;
double eta = 1.81e-5;
try{
inctime = Double.parseDouble(A.getText());
maxtime = Double.parseDouble(B.getText());
grav = Double.parseDouble(C.getText());
eta = Double.parseDouble(D.getText());
if(inctime < 0.01)
throw new IllegalArgumentException("increment time set too low");
else if(inctime > 0.09)
JOptionPane.showMessageDialog(null, "Setting increment time too high can lead to simulation problems.","scary values!",JOptionPane.INFORMATION_MESSAGE);
if(maxtime < 0)
throw new IllegalArgumentException("max time cannot be negative");
}catch (NumberFormatException ex){
JOptionPane.showMessageDialog(null, "Invalid input! Please enter valid numeric values.","invalid values!",JOptionPane.ERROR_MESSAGE);
this.dispose();
return;
}
catch(IllegalArgumentException i){
JOptionPane.showMessageDialog(null, i.getMessage(),"invalid values!",JOptionPane.ERROR_MESSAGE);
this.dispose();
return;
}
obj.setIncTime(inctime);
obj.setMaxTime(maxtime*10);
obj.setGravity(grav);
world.eta = eta;
}
this.dispose();
}
}
private class show_started extends Thread{
public void run()
{
JOptionPane.showMessageDialog(null,"rendering simulation ... ", "please wait", JOptionPane.INFORMATION_MESSAGE);
}
}
private class do_calc extends Thread{
public void run()
{
obj.run();
useful.setText("rendering complete");
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == newObject)
{
new getNewObjectDimensions();
if(world.balls.size() > 2)
newObject.setEnabled(false);
}
if(e.getSource() == newLine)
{
new getLineDimensions();
}
if(e.getSource() == newCircle)
{
new getCircleDimensions();
}
if(e.getSource() == renderSim)
{
show_started thr1 = new show_started();
thr1.start();
renderSim.setEnabled(false);
if(defaultSurface.isSelected())
{
world.addSurface(new point(0,10), new point(500,10));
}
if(setbounds.isSelected())
{
world.addSurface(new point(0,0), new point(0,500));
world.addSurface(new point(500,0), new point(500,500));
world.addSurface(new point(0,500), new point(500,500));
world.addSurface(new point(0,0), new point(500,0));
}
///*
do_calc thr2 = new do_calc();
thr2.start();
newCircle.setEnabled(false);
newLine.setEnabled(false);
newObject.setEnabled(false);
playSim.setEnabled(true);
setTime.setEnabled(false);
}
if(e.getSource() == playSim)
{
JFrame sim = new JFrame();
sim.add(new drawpad());
sim.setVisible(true);
sim.pack();
sim.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
if(e.getSource() == setTime)
{
new setWorldValues();
}
}
}