-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalcswing.java
More file actions
169 lines (155 loc) · 6.74 KB
/
Calcswing.java
File metadata and controls
169 lines (155 loc) · 6.74 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calcswing extends JFrame implements ActionListener {
private final JTextField display;
private String operator;
private double firstOperand, secondOperand, result;
private boolean startNewNumber = true;
public Calcswing() {
setTitle("Calcswing");
setSize(360, 520);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new BorderLayout(10, 10));
getContentPane().setBackground(new Color(20, 20, 20));
setResizable(false);
display = new JTextField("0") {
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
GradientPaint gp = new GradientPaint(0, 0, new Color(45, 45, 45), 0, getHeight(), new Color(25, 25, 25));
g2.setPaint(gp);
g2.fillRect(0, 0, getWidth(), getHeight());
super.paintComponent(g);
}
};
display.setFont(new Font("Poppins", Font.BOLD, 38));
display.setHorizontalAlignment(SwingConstants.RIGHT);
display.setEditable(false);
display.setForeground(new Color(255, 255, 255));
display.setCaretColor(Color.WHITE);
display.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
display.setOpaque(false);
add(display, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(5, 4, 10, 10));
buttonPanel.setBackground(new Color(20, 20, 20));
add(buttonPanel, BorderLayout.CENTER);
String[] buttons = {
"C", "←", "÷", "×",
"7", "8", "9", "-",
"4", "5", "6", "+",
"1", "2", "3", "=",
"0", ".", "+/-", "%"
};
for (String text : buttons) {
JButton b = createButton(text);
buttonPanel.add(b);
}
setLocationRelativeTo(null);
}
private JButton createButton(String text) {
JButton b = new JButton(text);
b.setFont(new Font("Poppins", Font.BOLD, 22));
b.setFocusPainted(false);
b.setForeground(Color.WHITE);
b.setBackground(isOperator(text) ? new Color(255, 140, 0) : new Color(50, 50, 50));
b.setBorder(BorderFactory.createLineBorder(new Color(30, 30, 30), 1, true));
b.setCursor(new Cursor(Cursor.HAND_CURSOR));
b.setOpaque(true);
b.setBorderPainted(false);
b.setActionCommand(text);
b.addActionListener(this);
b.addMouseListener(new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
if (isOperator(text)) {
b.setBackground(new Color(255, 160, 0));
b.setBorder(BorderFactory.createLineBorder(new Color(255, 190, 90), 2, true));
// Swing doesn't support a shadow layer on JButton; skip custom shadow
} else {
b.setBackground(new Color(70, 70, 70));
b.setBorder(BorderFactory.createLineBorder(new Color(200, 200, 200, 80), 2, true));
}
}
public void mouseExited(MouseEvent e) {
b.setBackground(isOperator(text) ? new Color(255, 140, 0) : new Color(50, 50, 50));
b.setBorder(BorderFactory.createLineBorder(new Color(30, 30, 30), 1, true));
}
});
return b;
}
private boolean isOperator(String t) {
return t.equals("+") || t.equals("-") || t.equals("×") || t.equals("÷") || t.equals("=");
}
private void animateDisplay(String newText) {
Timer timer = new Timer(10, null);
final float[] alpha = {0f};
timer.addActionListener(e -> {
alpha[0] += 0.1f;
if (alpha[0] >= 1f) {
alpha[0] = 1f;
((Timer) e.getSource()).stop();
}
display.setForeground(new Color(255, 255, 255, (int) (alpha[0] * 255)));
display.setText(newText);
});
timer.start();
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.matches("[0-9]") || cmd.equals(".")) {
if (startNewNumber) {
animateDisplay(cmd.equals(".") ? "0." : cmd);
startNewNumber = false;
} else {
if (!(cmd.equals(".") && display.getText().contains(".")))
animateDisplay(display.getText() + cmd);
}
} else if (isOperator(cmd) && !cmd.equals("=")) {
operator = cmd;
firstOperand = Double.parseDouble(display.getText());
startNewNumber = true;
} else if (cmd.equals("=")) {
// if no operator was selected, do nothing
if (operator == null || operator.isEmpty()) {
// keep current display
startNewNumber = true;
return;
}
secondOperand = Double.parseDouble(display.getText());
switch (operator) {
case "+": result = firstOperand + secondOperand; break;
case "-": result = firstOperand - secondOperand; break;
case "×": result = firstOperand * secondOperand; break;
case "÷": result = secondOperand != 0 ? firstOperand / secondOperand : Double.NaN; break;
default: result = secondOperand; break;
}
animateDisplay(formatResult(result));
startNewNumber = true;
} else if (cmd.equals("C")) {
animateDisplay("0");
operator = null;
firstOperand = secondOperand = result = 0;
startNewNumber = true;
} else if (cmd.equals("←")) {
String text = display.getText();
if (text.length() > 1)
animateDisplay(text.substring(0, text.length() - 1));
else
animateDisplay("0");
} else if (cmd.equals("+/-")) {
double val = Double.parseDouble(display.getText());
animateDisplay(formatResult(-val));
} else if (cmd.equals("%")) {
double val = Double.parseDouble(display.getText());
animateDisplay(formatResult(val / 100));
}
}
private String formatResult(double val) {
if (val == (long) val) return String.format("%d", (long) val);
return String.format("%.6f", val).replaceAll("0+$", "").replaceAll("\\.$", "");
}
public static void main(String[] args) {
try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (Exception ignored) {}
SwingUtilities.invokeLater(() -> new Calcswing().setVisible(true));
}
}