Repository navigation
Expand file tree
/
Copy pathCalculator.java
More file actions
302 lines (258 loc) · 9.95 KB
/
Copy pathCalculator.java
File metadata and controls
302 lines (258 loc) · 9.95 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
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.math.BigDecimal;
import java.math.MathContext;
/**
* A simple calculator built with Java Swing.
* Uses BigDecimal so results like 0.1 + 0.2 come out as 0.3.
* Supports mouse and keyboard input.
*
* Compile: javac Calculator.java
* Run: java Calculator
*/
public class Calculator extends JFrame {
private static final Color BG = new Color(0x1E1E24);
private static final Color NUM_BG = new Color(0x3A3A44);
private static final Color FUNC_BG = new Color(0x55555F);
private static final Color OP_BG = new Color(0xFF9F0A);
private static final Color EQ_BG = new Color(0x30B050);
private static final Color TEXT = Color.WHITE;
private final JLabel history = new JLabel(" ", SwingConstants.RIGHT);
private final JLabel display = new JLabel("0", SwingConstants.RIGHT);
private BigDecimal left = null; // stored left operand
private String operator = null; // + - × ÷
private boolean startNewNumber = true;
private boolean error = false;
public Calculator() {
super("Calculator");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
getContentPane().setBackground(BG);
add(buildDisplay(), BorderLayout.NORTH);
add(buildButtons(), BorderLayout.CENTER);
setupKeyboard();
setSize(340, 520);
setMinimumSize(new Dimension(280, 420));
setLocationRelativeTo(null);
setFocusable(true);
}
// ---------- UI ----------
private JPanel buildDisplay() {
JPanel panel = new JPanel(new GridLayout(2, 1));
panel.setBackground(BG);
panel.setBorder(new EmptyBorder(16, 16, 8, 16));
history.setForeground(new Color(0x9A9AA5));
history.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));
display.setForeground(TEXT);
display.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 44));
panel.add(history);
panel.add(display);
panel.setPreferredSize(new Dimension(0, 110));
return panel;
}
private JPanel buildButtons() {
JPanel grid = new JPanel(new GridLayout(5, 4, 8, 8));
grid.setBackground(BG);
grid.setBorder(new EmptyBorder(8, 12, 12, 12));
String[] labels = {
"C", "⌫", "%", "÷",
"7", "8", "9", "×",
"4", "5", "6", "−",
"1", "2", "3", "+",
"±", "0", ".", "="
};
for (String label : labels) {
grid.add(makeButton(label));
}
return grid;
}
private JButton makeButton(String label) {
JButton b = new JButton(label);
b.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 22));
b.setForeground(TEXT);
b.setFocusPainted(false);
b.setFocusable(false); // keep keyboard focus on the frame
b.setBorderPainted(false);
b.setOpaque(true);
b.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
switch (label) {
case "C", "⌫", "%", "±" -> b.setBackground(FUNC_BG);
case "÷", "×", "−", "+" -> b.setBackground(OP_BG);
case "=" -> b.setBackground(EQ_BG);
default -> b.setBackground(NUM_BG);
}
b.addActionListener(e -> handle(label));
return b;
}
// ---------- Keyboard ----------
private void setupKeyboard() {
// Window-level bindings work even when a button or another child owns focus.
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getRootPane().getActionMap();
for (char digit = '0'; digit <= '9'; digit++) {
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke(digit), String.valueOf(digit));
}
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke('.'), ".");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke(','), ".");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke('+'), "+");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke('-'), "−");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke('*'), "×");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke('/'), "÷");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke('%'), "%");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke('='), "=");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "=");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "⌫");
bindKey(inputMap, actionMap, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "C");
}
private void bindKey(InputMap inputMap, ActionMap actionMap, KeyStroke keyStroke, String action) {
String actionName = "key-" + action + "-" + keyStroke;
inputMap.put(keyStroke, actionName);
actionMap.put(actionName, new AbstractAction() {
@Override
public void actionPerformed(java.awt.event.ActionEvent event) {
handle(action);
}
});
}
// ---------- Logic ----------
private void handle(String key) {
if (error && !key.equals("C")) {
clearAll(); // recover from an error on any input
}
switch (key) {
case "C" -> clearAll();
case "⌫" -> backspace();
case "±" -> negate();
case "%" -> percent();
case "." -> inputDot();
case "=" -> equals();
case "+", "−", "×", "÷" -> setOperator(key);
default -> inputDigit(key);
}
}
private void inputDigit(String d) {
String text = display.getText();
if (startNewNumber || text.equals("0")) {
setDisplay(d);
} else if (text.replace("-", "").replace(".", "").length() < 15) {
setDisplay(text + d);
}
startNewNumber = false;
}
private void inputDot() {
if (startNewNumber) {
setDisplay("0.");
startNewNumber = false;
} else if (!display.getText().contains(".")) {
setDisplay(display.getText() + ".");
}
}
private void backspace() {
if (startNewNumber) return;
String text = display.getText();
if (text.length() <= 1 || (text.length() == 2 && text.startsWith("-"))) {
setDisplay("0");
startNewNumber = true;
} else {
setDisplay(text.substring(0, text.length() - 1));
}
}
private void negate() {
String text = display.getText();
if (text.equals("0")) return;
setDisplay(text.startsWith("-") ? text.substring(1) : "-" + text);
}
private void percent() {
BigDecimal value = current().divide(BigDecimal.valueOf(100), MathContext.DECIMAL64);
setDisplay(format(value));
startNewNumber = true;
}
private void setOperator(String op) {
// Chain operations: 2 + 3 + ... computes 2 + 3 first.
if (operator != null && !startNewNumber) {
if (!compute()) return;
} else if (operator == null) {
left = current();
}
operator = op;
startNewNumber = true;
history.setText(format(left) + " " + op);
}
private void equals() {
if (operator == null) return;
String expr = format(left) + " " + operator + " " + format(current()) + " =";
if (compute()) {
history.setText(expr);
operator = null;
left = null;
startNewNumber = true;
}
}
/** Applies the pending operator to left and the display value. Result goes to display and left. */
private boolean compute() {
BigDecimal right = current();
BigDecimal result;
switch (operator) {
case "+" -> result = left.add(right);
case "−" -> result = left.subtract(right);
case "×" -> result = left.multiply(right);
case "÷" -> {
if (right.signum() == 0) {
showError("Cannot divide by 0");
return false;
}
result = left.divide(right, MathContext.DECIMAL64);
}
default -> { return false; }
}
left = result;
setDisplay(format(result));
startNewNumber = true;
return true;
}
private void clearAll() {
left = null;
operator = null;
startNewNumber = true;
error = false;
history.setText(" ");
setDisplay("0");
}
private void showError(String message) {
error = true;
left = null;
operator = null;
startNewNumber = true;
history.setText(message);
setDisplay("Error");
}
// ---------- Helpers ----------
private BigDecimal current() {
try {
return new BigDecimal(display.getText());
} catch (NumberFormatException ex) {
return BigDecimal.ZERO;
}
}
private static String format(BigDecimal value) {
BigDecimal stripped = value.stripTrailingZeros();
if (stripped.signum() == 0) return "0";
return stripped.toPlainString();
}
private void setDisplay(String text) {
display.setText(text);
// Shrink the font for long numbers so they stay visible.
int size = text.length() > 14 ? 24 : text.length() > 10 ? 32 : 44;
display.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, size));
}
// ---------- Main ----------
public static void main(String[] args) {
try {
// Cross-platform look & feel so button colors render the same everywhere.
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception ignored) { }
SwingUtilities.invokeLater(() -> new Calculator().setVisible(true));
}
}