Krolyxon
Commit 01f32e1

Branches

Refs →

jcalsrcGFrame.java

239 lines · 8.4 KB · Java

1/*
2* TODO:
3* Item positions are hard coded, make them dynamic
4* so that window can be set to resizable.
5*/
6
7import java.util.Vector;
8import javax.swing.*;
9import java.awt.Color;
10import java.awt.Dimension;
11import java.awt.Font;
12import java.awt.Insets;
13import java.awt.event.*;
14import java.awt.*;
15import javax.swing.border.Border;
16import java.text.DecimalFormat;
17import java.lang.StringBuilder;
18
19class RoundBtn implements Border {
20 private int r;
21
22 RoundBtn(int r) {
23 this.r = r;
24 }
25
26 public Insets getBorderInsets(Component c) {
27 return new Insets(this.r + 1, this.r + 1, this.r + 2, this.r);
28 }
29
30 public boolean isBorderOpaque() {
31 return true;
32 }
33
34 public void paintBorder(Component c, Graphics g, int x, int y,
35 int width, int height) {
36 g.drawRoundRect(x, y, width - 1, height - 1, r, r);
37 }
38}
39
40public class GFrame extends JFrame {
41 Parser p;
42 JTextField tf;
43
44 private JButton newButton(String text, int d1, int d2, int d3, int d4) {
45 JButton b = new JButton();
46 // b.setBackground(Color.decode("#F78361"));
47 b.setBackground(Color.decode("#2B2B2B"));
48 b.setForeground(Color.decode("#e5e5e5"));
49 b.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
50 b.setBounds(d1, d2, d3, d4);
51 b.setBorder(new RoundBtn(15));
52 b.setText(text);
53 return b;
54 }
55
56 private void actionAdderForTextField(JButton b, String val) {
57 b.addActionListener(new ActionListener() {
58 public void actionPerformed(ActionEvent e) {
59 tf.setText(tf.getText() + val);
60 }
61 });
62
63 }
64
65 GFrame(String title) {
66 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
67 this.setTitle(title);
68 this.getContentPane().setBackground(Color.decode("#111111"));
69 this.setResizable(false);
70
71 // Text Field
72 tf = new JTextField();
73 tf.setBackground(Color.decode("#2B2B2B"));
74 tf.setForeground(Color.decode("#e5e5e5"));
75 tf.setMargin(new Insets(0, 10, 0, 10));
76 tf.setBounds(40, 50, 240, 40);
77 tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
78 tf.setBorder(new RoundBtn(5));
79 tf.setCaretColor(Color.decode("#e5e5e5"));
80
81 // History
82 DefaultListModel<String> histList = new DefaultListModel<>();
83 JList<String> lHist = new JList<>(histList);
84 lHist.setBackground(Color.decode("#2B2B2B"));
85 lHist.setForeground(Color.decode("#e5e5e5"));
86 lHist.setBounds(380, 50, 200, 240);
87 lHist.setFont(new Font("Times New Roman", Font.PLAIN, 20));
88 lHist.setBorder(new RoundBtn(5));
89 this.add(lHist);
90
91 // Buttons
92 JButton bEval = newButton("=", 290, 50, 60, 40);
93 JButton bExp = newButton("^", 250, 100, 100, 40);
94 JButton bClear = newButton("CL", 180, 100, 60, 40);
95 JButton bClearHistory = newButton("Clear History", 380, 300, 200, 40);
96 JButton bAdd = newButton("+", 250, 300, 100, 40);
97 JButton bSub = newButton("-", 250, 250, 100, 40);
98 JButton bMul = newButton("x", 250, 200, 100, 40);
99 JButton bDiv = newButton("÷", 250, 150, 100, 40);
100 JButton bCut = newButton("", 180, 300, 60, 40);
101 bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20));
102 JButton bRightPar = newButton(")", 110, 100, 60, 40);
103 JButton bLeftPar = newButton("(", 40, 100, 60, 40);
104 JButton bDoubleZero = newButton("00", 40, 300, 60, 40);
105 JButton bZero = newButton("0", 110, 300, 60, 40);
106 JButton bOne = newButton("1", 40, 250, 60, 40);
107 JButton bTwo = newButton("2", 110, 250, 60, 40);
108 JButton bThree = newButton("3", 180, 250, 60, 40);
109 JButton bFour = newButton("4", 40, 200, 60, 40);
110 JButton bFive = newButton("5", 110, 200, 60, 40);
111 JButton bSix = newButton("6", 180, 200, 60, 40);
112 JButton bSeven = newButton("7", 40, 150, 60, 40);
113 JButton bEight = newButton("8", 110, 150, 60, 40);
114 JButton bNine = newButton("9", 180, 150, 60, 40);
115
116 // Add the buttons to the Frame
117 this.add(bExp);
118 this.add(bEval);
119 this.add(bClear);
120 this.add(bClearHistory);
121 this.add(bAdd);
122 this.add(bSub);
123 this.add(bMul);
124 this.add(bDiv);
125 this.add(bCut);
126 this.add(bRightPar);
127 this.add(bLeftPar);
128 this.add(bDoubleZero);
129 this.add(bZero);
130 this.add(bOne);
131 this.add(bTwo);
132 this.add(bThree);
133 this.add(bFour);
134 this.add(bFive);
135 this.add(bSix);
136 this.add(bSeven);
137 this.add(bEight);
138 this.add(bNine);
139 this.add(tf);
140 this.setSize(600, 400);
141 this.setLayout(null);
142 this.setVisible(true);
143
144 // JLabel label = new JLabel("Enter Expression: ");
145 // label.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
146 // label.setBounds(40, 20, 150, 40);
147 // label.setForeground(Color.decode("#e5e5e5"));
148 // this.add(label);
149
150 // ActionsListeners
151 bEval.addActionListener(new ActionListener() {
152 public void actionPerformed(ActionEvent e) {
153 if (tf.getText().length() != 0) {
154 String expression = tf.getText();
155 DecimalFormat format = new DecimalFormat();
156 p = new Parser(tf.getText());
157 String formattedResult = format.format(p.eval());
158 tf.setText(formattedResult);
159 String formattedExpr = expression + " = " + formattedResult;
160
161 // Dont add to history if its already present in histList
162 for (int i = 0; i < histList.size(); i++) {
163 if (histList.get(i).equals(formattedExpr)) {
164 return;
165 }
166 }
167 histList.addElement(formattedExpr);
168 } else {
169 tf.setText("No input");
170 }
171 }
172 });
173
174 // Common actions that just appends the symbols to the text field
175 actionAdderForTextField(bExp, "^");
176 actionAdderForTextField(bAdd, "+");
177 actionAdderForTextField(bSub, "-");
178 actionAdderForTextField(bMul, "*");
179 actionAdderForTextField(bDiv, "/");
180 actionAdderForTextField(bRightPar, ")");
181 actionAdderForTextField(bLeftPar, "(");
182 actionAdderForTextField(bDoubleZero, "00");
183 actionAdderForTextField(bZero, "0");
184 actionAdderForTextField(bOne, "1");
185 actionAdderForTextField(bTwo, "2");
186 actionAdderForTextField(bThree, "3");
187 actionAdderForTextField(bFour, "4");
188 actionAdderForTextField(bFive, "5");
189 actionAdderForTextField(bSix, "6");
190 actionAdderForTextField(bSeven, "7");
191 actionAdderForTextField(bEight, "8");
192 actionAdderForTextField(bNine, "9");
193
194 bClear.addActionListener(new ActionListener() {
195 public void actionPerformed(ActionEvent e) {
196 tf.setText("");
197 }
198 });
199
200 bClearHistory.addActionListener(new ActionListener() {
201 public void actionPerformed(ActionEvent e) {
202 histList.clear();
203 }
204 });
205
206 bCut.addActionListener(new ActionListener() {
207 public void actionPerformed(ActionEvent e) {
208 String text = tf.getText();
209 tf.setText(text.substring(0, text.length() - 1));
210 }
211 });
212
213 lHist.addMouseListener(new MouseAdapter() {
214 public void mouseClicked(MouseEvent evt) {
215 JList list = (JList) evt.getSource();
216 if (evt.getClickCount() == 2) {
217 // Double-click detected
218 int index = list.locationToIndex(evt.getPoint());
219 String expr = histList.get(index);
220 StringBuilder str = new StringBuilder("");
221 int endIndex = 0;
222 for (int i = 0; i < expr.length(); i++) {
223 if (expr.charAt(i) == '=') {
224 break;
225 }
226 endIndex++;
227 }
228 tf.setText(expr.substring(0, endIndex));
229 }
230 }
231 });
232
233 // bOne.addActionListener(new ActionListener() {
234 // public void actionPerformed(ActionEvent e) {
235 // tf.setText(tf.getText() + "1");
236 // }
237 // });
238 }
239}