-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculatorGUI_updated.java
More file actions
234 lines (208 loc) · 7.77 KB
/
CalculatorGUI_updated.java
File metadata and controls
234 lines (208 loc) · 7.77 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
/**
* CalculatorGUI.java
* @description simple calculator class
* @author R Samman
* @version 2.0 04-04-2022
*/
import javax.swing.*;
// import javax.swing.event.ChangeEvent;
// import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
/**
* CalculatorGUI class represents a simple calculator GUI.
*/
public class CalculatorGUI extends JPanel {
private JPanel buttonPanel, textPanel, headerPanel;
private JButton buttonArray[] = new JButton[16];
private String buttonText[] = {"7", "8", "9", "+", "4", "5", "6", "-",
"1", "2", "3", "\u00D7", "C", "0", "=",
"\u00F7"};
private JTextField output;
private Font calcFont;
private Color softBlue, lightGray, buttonPastel, lightGold;
private Response userActivity = new Response();
private Calculations_updated calc = new Calculations_updated();
/**
* Constructor to initialize the calculator GUI components.
*/
public CalculatorGUI_updated() {
setFontsColorsAndImages();
setHeaderPanel();
setTextPanel();
setButtonPanel();
placeElementsInFrame();
}
/**
* Set fonts, colors, and images used in the GUI.
*/
private void setFontsColorsAndImages() {
calcFont = new Font("Segoe UI", Font.BOLD, 50);
softBlue = new Color(200, 216, 230);
lightGray = new Color(240, 240, 240);
buttonPastel = new Color(220, 220, 220);
lightGold = new Color(255, 223, 128);
}
/**
* Set header panel labels.
*/
private void setHeaderPanel() {
headerPanel = new JPanel();
headerPanel.setPreferredSize(new Dimension(470, 70));
headerPanel.setBackground(softBlue);
headerPanel.setLayout(new BorderLayout());
}
/**
* Set the text panel.
*/
private void setTextPanel() {
output = new JTextField(20);
output.setEditable(false);
output.setFont(calcFont);
output.setText("0");
textPanel = new JPanel();
textPanel.setLayout(new BorderLayout());
textPanel.add(output, BorderLayout.CENTER);
textPanel.setPreferredSize(new Dimension(470, 100));
textPanel.setBackground(lightGray);
}
/**
* Set button panel.
*/
private void setButtonPanel() {
buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(4, 4, 10, 10));
buttonPanel.setBackground(lightGray);
for (int i = 0; i < buttonText.length; i++) {
final int index = i;
buttonArray[index] = new JButton(buttonText[index]);
buttonArray[index].setFont(calcFont);
buttonArray[index].setFocusPainted(false);
buttonArray[index].setBorderPainted(false);
buttonArray[index].addActionListener(userActivity);
buttonArray[index].setOpaque(true);
// Set light blue background for all buttons
buttonArray[index].setBackground(softBlue);
// Set gold background for operations, "=", and "C"
if (index % 4 == 3 || index == 12 || index == 14) {
buttonArray[index].setBackground(lightGold);
}
// Add soft press effect
buttonArray[index].getModel().addChangeListener(e -> {
ButtonModel model = (ButtonModel) e.getSource();
if (model.isPressed()) {
startClickAnimation(buttonArray[index]);
}
});
// Add mouse listener for white background effect
buttonArray[index].addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
resetButtonsBackground();
}
});
buttonPanel.add(buttonArray[index]);
}
buttonPanel.setPreferredSize(new Dimension(470, 470));
}
private void startClickAnimation(JButton button) {
if (!( button.getText().equals("C") )) {
button.setBackground(Color.WHITE);
Timer timer = new Timer(100, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
button.setBackground(buttonPastel);
}
});
timer.setRepeats(false);
timer.start();
}
}
/**
* Resets the buttonPanel components features.
*/
private void resetButtonsBackground() {
for (JButton button : buttonArray) {
if (button.getText().equals("=") || button.getText().equals("C") || button.getText().equals("+") ||
button.getText().equals("-") || button.getText().equals("\u00D7") || button.getText().equals("\u00F7")) {
button.setBackground(lightGold);
} else {
button.setBackground(softBlue);
}
}
}
/**
* Set the layout and add components to the frame.
*/
private void placeElementsInFrame() {
setBackground(lightGray);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
add(headerPanel);
add(textPanel);
add(buttonPanel);
}
/**
* ActionListener implementation to respond to button clicks.
*/
private class Response implements ActionListener {
private boolean lastOperation = false;
private boolean equalsPressed = false;
public void actionPerformed(ActionEvent press) {
Object buttonPressed = press.getSource();
String content = output.getText();
// zero integer
if (buttonPressed == buttonArray[13]) {
if (!content.equals("0")) {
content += buttonArray[13].getText();
output.setText(content);
}
}
// clear 'C'
if (buttonPressed == buttonArray[12]) {
output.setText("0");
equalsPressed = false; // resets the flag when 'C' is pressed
}
// operations
for (int i = 3; i < 16; i += 4) {
if (buttonPressed == buttonArray[i]) {
lastOperation = true;
calc.handleOperation(output.getText(), buttonArray[i].getText());
equalsPressed = false; // resets the flag when an operator is pressed
}
}
// equals
if (buttonPressed == buttonArray[14]) {
output.setText(calc.performCalculation(output.getText()));
equalsPressed = true; // sets the flag when equals is pressed
}
for (int i = 0; i < buttonArray.length; i++) {
if (buttonPressed == buttonArray[i] && i % 4 != 3 && i < 12) {
if (content.equals("0") || lastOperation || equalsPressed) {
output.setText(buttonArray[i].getText());
lastOperation = false;
equalsPressed = false; // resets the flag when a number is pressed after equals
} else {
content += buttonArray[i].getText();
output.setText(content);
}
}
}
}
}
/**
* Main method to create and display the calculator GUI.
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("My Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(580, 720);
frame.setResizable(false);
frame.add(new CalculatorGUI());
frame.setVisible(true);
});
}
}