-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDCApp.java
More file actions
88 lines (70 loc) · 2.66 KB
/
DCApp.java
File metadata and controls
88 lines (70 loc) · 2.66 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
/**
* Created by Stephen S. and Sophia L. on 10/11/14.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class DCApp implements ActionListener {
JTextField runsBox = new JTextField(40);
JTextArea log = new JTextArea(10, 10);
JButton button = new JButton("Click Me!");
JFrame frame = new JFrame("Calculating Pi");
static DCApp app;
String str;
int val_str;
public static void main(String[] args) {
app = new DCApp();
app.init();
}
public void init() {
DCApp gui = new DCApp();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button.addActionListener(gui);
runsBox.addKeyListener(new KeyAdapter() {
public void keyTyped (KeyEvent e) {
char c = e.getKeyChar();
if (!(Character.isDigit(c)) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE))
{
e.consume();
}
}
});
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(button, BorderLayout.NORTH);
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
panel.add(new JLabel("Runs: "));
panel.add(runsBox);
log.setEditable(false);
frame.getContentPane().add(log, BorderLayout.CENTER);
frame.getContentPane().add(runsBox, BorderLayout.SOUTH);
frame.setSize(600, 600);
}
public void actionPerformed(ActionEvent evt) {
str = app.runsBox.getText();
if (str.length() != 0)
{
val_str = Integer.parseInt(str);
}
try {
if (!(str.equals("") || val_str == 0)) {
System.out.println(str);
MonteCarloSim sim = new MonteCarloSim(Integer.parseInt(str.trim()));
sim.runAll();
long hits = sim.getHits();
System.out.println(hits);
Connection connection = new Connection("http://tensile-tenure-727.appspot.com/totals");
connection.post(hits, Integer.parseInt(str.trim()));
} else {
JOptionPane.showMessageDialog(null, "Please only insert a positive integer for the number of runs.");
System.out.println("User did not insert a valid value for the number of runs.");
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Exception Occurred.");
}
}
}