forked from pH-7/Simple-Java-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUI.java
More file actions
221 lines (186 loc) · 6.36 KB
/
Copy pathUI.java
File metadata and controls
221 lines (186 loc) · 6.36 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
/**
* @name Simple Java Calculator
* @package ph.calculator
* @file UI.java
* @author SORIA Pierre-Henry
* @email pierrehs@hotmail.com
* @link http://github.com/pH-7
* @copyright Copyright Pierre-Henry SORIA, All Rights Reserved.
* @license Apache (http://www.apache.org/licenses/LICENSE-2.0)
* @create 2012-03-30
*
* @modifiedby Achintha Gunasekara
* @modifiedby Kydon Chantzaridis
* @modweb http://www.achinthagunasekara.com
* @modemail contact@achinthagunasekara.com
* @modemail kchantza@csd.auth.gr
*/
package simplejavacalculator;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class UI implements ActionListener {
private final JFrame frame;
private final JPanel panel;
private final JTextArea text;
private final JButton but[], butAdd, butMinus, butMultiply, butDivide,
butEqual, butCancel, butSquareRoot, butSquare, butOneDevidedBy,
butCos, butSin, butTan, butxpowerofy, butlog, butrate;
private final Calculator calc;
private final String[] buttonValue = { "0", "1", "2", "3", "4", "5", "6",
"7", "8", "9" };
public UI() {
frame = new JFrame("Calculator PH");
frame.setResizable(false);
panel = new JPanel(new FlowLayout());
text = new JTextArea(2, 25);
but = new JButton[10];
for (int i = 0; i < 10; i++) {
but[i] = new JButton(String.valueOf(i));
}
butAdd = new JButton("+");
butMinus = new JButton("-");
butMultiply = new JButton("*");
butDivide = new JButton("/");
butEqual = new JButton("=");
butSquareRoot = new JButton("√");
butSquare = new JButton("x*x");
butOneDevidedBy = new JButton("1/x");
butCos = new JButton("Cos");
butSin = new JButton("Sin");
butTan = new JButton("Tan");
butxpowerofy = new JButton("x^y");
butlog = new JButton("log10(x)");
butrate = new JButton("x%");
butCancel = new JButton("C");
calc = new Calculator();
}
public void init() {
frame.setVisible(true);
frame.setSize(330, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.add(text);
for (int i = 1; i < 10; i++) {
panel.add(but[i]);
but[i].addActionListener(this);
}
panel.add(but[0]);
but[0].addActionListener(this);
panel.add(butAdd);
panel.add(butMinus);
panel.add(butMultiply);
panel.add(butDivide);
panel.add(butSquare);
panel.add(butSquareRoot);
panel.add(butOneDevidedBy);
panel.add(butCos);
panel.add(butSin);
panel.add(butTan);
panel.add(butxpowerofy);
panel.add(butlog);
panel.add(butrate);
panel.add(butEqual);
panel.add(butCancel);
butAdd.addActionListener(this);
butMinus.addActionListener(this);
butMultiply.addActionListener(this);
butDivide.addActionListener(this);
butSquare.addActionListener(this);
butSquareRoot.addActionListener(this);
butOneDevidedBy.addActionListener(this);
butCos.addActionListener(this);
butSin.addActionListener(this);
butTan.addActionListener(this);
butxpowerofy.addActionListener(this);
butlog.addActionListener(this);
butrate.addActionListener(this);
butEqual.addActionListener(this);
butCancel.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
final Object source = e.getSource();
for (int i = 0; i < 10; i++) {
if (source == but[i]) {
text.replaceSelection(buttonValue[i]);
return;
}
}
if (source == butAdd) {
writer(calc.calculateBi(Calculator.BiOperatorModes.add, reader()));
}
if (source == butMinus) {
writer(calc.calculateBi(Calculator.BiOperatorModes.minus, reader()));
}
if (source == butMultiply) {
writer(calc.calculateBi(Calculator.BiOperatorModes.multiply,
reader()));
}
if (source == butDivide) {
writer(calc
.calculateBi(Calculator.BiOperatorModes.divide, reader()));
}
if (source == butxpowerofy) {
writer(calc
.calculateBi(Calculator.BiOperatorModes.xpowerofy, reader()));
}
if (source == butSquare) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.square,
reader()));
}
if (source == butSquareRoot) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.squareRoot,
reader()));
}
if (source == butOneDevidedBy) {
writer(calc.calculateMono(
Calculator.MonoOperatorModes.oneDevidedBy, reader()));
}
if (source == butCos) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.cos,
reader()));
}
if (source == butSin) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.sin,
reader()));
}
if (source == butTan) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.tan,
reader()));
}
if (source == butlog) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.log,
reader()));
}
if (source == butrate) {
writer(calc.calculateMono(Calculator.MonoOperatorModes.rate,
reader()));
}
if (source == butEqual) {
writer(calc.calculateEqual(reader()));
}
if (source == butCancel) {
writer(calc.reset());
}
text.selectAll();
}
public Double reader() {
Double num;
String str;
str = text.getText();
num = Double.valueOf(str);
return num;
}
public void writer(final Double num) {
if (Double.isNaN(num)) {
text.setText("");
} else {
text.setText(Double.toString(num));
}
}
}