Krolyxon
Commit 991282c

Branches

Refs →

jcal991282c

add History support and make buttons rounded

Author
krolxon
Date
2024-02-17 13:51 +0530
Parent
d179508
Commit
991282cd9391dc2a668780b43c5119c20d77990f

+46 −15

2 files changed

  1. M GFrame.java +46 −12
  2. M History.java +0 −3
M GFrame.java View
@@ -4,7 +4,6 @@
44 * so that window can be set to resizable.
55 */
66
7−
87 import java.util.Vector;
98 import javax.swing.*;
109 import java.awt.Color;
@@ -12,6 +11,30 @@ import java.awt.Dimension;
1211 import java.awt.Font;
1312 import java.awt.Insets;
1413 import java.awt.event.*;
14+import java.awt.*;
15+import javax.swing.border.Border;
16+import java.text.DecimalFormat;
17+
18+class RoundBtn implements Border {
19+ private int r;
20+
21+ RoundBtn(int r) {
22+ this.r = r;
23+ }
24+
25+ public Insets getBorderInsets(Component c) {
26+ return new Insets(this.r + 1, this.r + 1, this.r + 2, this.r);
27+ }
28+
29+ public boolean isBorderOpaque() {
30+ return true;
31+ }
32+
33+ public void paintBorder(Component c, Graphics g, int x, int y,
34+ int width, int height) {
35+ g.drawRoundRect(x, y, width - 1, height - 1, r, r);
36+ }
37+}
1538
1639 public class GFrame extends JFrame {
1740 Parser p;
@@ -24,6 +47,7 @@ public class GFrame extends JFrame {
2447 b.setForeground(Color.decode("#e5e5e5"));
2548 b.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
2649 b.setBounds(d1, d2, d3, d4);
50+ b.setBorder(new RoundBtn(15));
2751 b.setText(text);
2852 return b;
2953 }
@@ -38,7 +62,6 @@ public class GFrame extends JFrame {
3862 }
3963
4064 GFrame(String title) {
41− History history = new History();
4265 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
4366 this.setTitle(title);
4467 this.getContentPane().setBackground(Color.decode("#111111"));
@@ -51,11 +74,19 @@ public class GFrame extends JFrame {
5174 tf.setMargin(new Insets(0, 10, 0, 10));
5275 tf.setBounds(40, 50, 310, 40);
5376 tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
77+ tf.setBorder(new RoundBtn(5));
5478
5579 // History Text Field
56− JTextField tHist = new JTextField();
57− tHist.setBounds(100, 10, 310, 40);
80+ History history = new History();
81+ JTextArea tHist = new JTextArea();
82+ tHist.setBounds(380, 50, 200, 290);
5883 tHist.setEditable(false);
84+ tHist.setBorder(new RoundBtn(5));
85+ tHist.setBackground(Color.decode("#2B2B2B"));
86+ tHist.setForeground(Color.decode("#e5e5e5"));
87+ tHist.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
88+ tHist.setBorder(new RoundBtn(5));
89+ tHist.setMargin(new Insets(0, 10, 0, 10));
5990
6091 // Buttons
6192 JButton bEval = newButton("=", 250, 100, 100, 40);
@@ -65,7 +96,7 @@ public class GFrame extends JFrame {
6596 JButton bMul = newButton("X", 250, 200, 100, 40);
6697 JButton bDiv = newButton("%", 250, 150, 100, 40);
6798 JButton bCut = newButton("", 180, 300, 60, 40);
68− bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20 ));
99+ bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20));
69100 JButton bRightPar = newButton(")", 110, 100, 60, 40);
70101 JButton bLeftPar = newButton("(", 40, 100, 60, 40);
71102 JButton bDoubleZero = newButton("00", 40, 300, 60, 40);
@@ -102,8 +133,8 @@ public class GFrame extends JFrame {
102133 this.add(bEight);
103134 this.add(bNine);
104135 this.add(tf);
105− // this.add(tHist);
106− this.setSize(400, 400);
136+ this.add(tHist);
137+ this.setSize(600, 400);
107138 this.setLayout(null);
108139 this.setVisible(true);
109140
@@ -117,17 +148,20 @@ public class GFrame extends JFrame {
117148 bEval.addActionListener(new ActionListener() {
118149 public void actionPerformed(ActionEvent e) {
119150 if (tf.getText().length() != 0) {
151+ String expression = tf.getText();
152+ DecimalFormat format = new DecimalFormat();
120153 p = new Parser(tf.getText());
121− tf.setText(Double.toString(p.eval()));
122−
123− // Save to history
124− history.addHistory(tf.getText());
154+ String formattedResult = format.format(p.eval());
155+ tf.setText(formattedResult);
125156
126157 tHist.setText("");
127158 Vector<String> s = history.getHistory();
159+ s.add(expression + " = " + formattedResult);
128160 for (int i = 0; i < s.size(); i++) {
129161 System.out.println(s.get(i));
130− tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n"));
162+ // tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n"));
163+ tHist.append(s.get(i));
164+ tHist.append("\n");
131165 }
132166
133167 } else {
M History.java View
@@ -7,9 +7,6 @@ public class History {
77 hist = new Vector<String>();
88 }
99
10− public void addHistory(String h) {
11− hist.add(h);
12− }
1310 public Vector<String> getHistory() {
1411 return hist;
1512 }