Krolyxon
Branch master

Branches

Refs →

jcalsrcGFrame.java

257 lines · 9.2 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", 1, 20)));
50 b.setBounds(d1, d2, d3, d4);
51 b.setBorder(new RoundBtn(15));
52 b.setText(text);
53 b.setBorderPainted(false);
54 return b;
55 }
56
57private JButton newButtonWhite(String text, int d1, int d2, int d3, int d4) {
58 JButton b = new JButton();
59 // b.setBackground(Color.decode("#F78361"));
60 b.setBackground(Color.decode("#e5e5e5"));
61 b.setForeground(Color.decode("#131313"));
62 b.setFont((new Font("Times New Roman", Font.BOLD, 20)));
63 b.setBounds(d1, d2, d3, d4);
64 b.setBorder(new RoundBtn(15));
65 b.setText(text);
66 b.setBorderPainted(false);
67 return b;
68 }
69
70 private void actionAdderForTextField(JButton b, String val) {
71 b.addActionListener(new ActionListener() {
72 public void actionPerformed(ActionEvent e) {
73 tf.setText(tf.getText() + val);
74 }
75 });
76
77 }
78
79 GFrame(String title) {
80 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
81 this.setTitle(title);
82 this.getContentPane().setBackground(Color.decode("#111111"));
83 this.setResizable(false);
84
85 // Text Field
86 tf = new JTextField();
87 tf.setBackground(Color.decode("#2B2B2B"));
88 tf.setForeground(Color.decode("#e5e5e5"));
89 tf.setMargin(new Insets(0, 10, 0, 10));
90 tf.setBounds(40, 50, 240, 40);
91 tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
92 tf.setBorder(new RoundBtn(5));
93 tf.setCaretColor(Color.decode("#e5e5e5"));
94
95 // History
96 DefaultListModel<String> histList = new DefaultListModel<>();
97 JList<String> lHist = new JList<>(histList);
98 lHist.setBackground(Color.decode("#2B2B2B"));
99 lHist.setForeground(Color.decode("#e5e5e5"));
100 lHist.setBounds(380, 50, 200, 240);
101 lHist.setFont(new Font("Times New Roman", Font.PLAIN, 20));
102 lHist.setSelectionBackground(Color.decode("#FEA00B"));
103 lHist.setSelectionForeground(Color.decode("#111111"));
104 // lHist.setBorder(new RoundBtn(0));
105 this.add(lHist);
106
107 // Buttons
108 JButton bEval = newButtonWhite("=", 290, 50, 60, 40);
109 JButton bExp = newButtonWhite("^", 250, 100, 100, 40);
110 JButton bClear = newButton("AC", 180, 100, 60, 40);
111 bClear.setBackground(Color.decode("#FEA00B"));
112 bClear.setForeground(Color.decode("#111111"));
113 JButton bClearHistory = newButton("Clear History", 380, 300, 200, 40);
114 JButton bAdd = newButtonWhite("+", 250, 300, 100, 40);
115 JButton bSub = newButtonWhite("-", 250, 250, 100, 40);
116 JButton bMul = newButtonWhite("x", 250, 200, 100, 40);
117 JButton bDiv = newButtonWhite("÷", 250, 150, 100, 40);
118 JButton bCut = newButton("", 180, 300, 60, 40);
119 bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20));
120 JButton bRightPar = newButton(")", 110, 100, 60, 40);
121 JButton bLeftPar = newButton("(", 40, 100, 60, 40);
122 JButton bDoubleZero = newButton("00", 40, 300, 60, 40);
123 JButton bZero = newButton("0", 110, 300, 60, 40);
124 JButton bOne = newButton("1", 40, 250, 60, 40);
125 JButton bTwo = newButton("2", 110, 250, 60, 40);
126 JButton bThree = newButton("3", 180, 250, 60, 40);
127 JButton bFour = newButton("4", 40, 200, 60, 40);
128 JButton bFive = newButton("5", 110, 200, 60, 40);
129 JButton bSix = newButton("6", 180, 200, 60, 40);
130 JButton bSeven = newButton("7", 40, 150, 60, 40);
131 JButton bEight = newButton("8", 110, 150, 60, 40);
132 JButton bNine = newButton("9", 180, 150, 60, 40);
133
134 // Add the buttons to the Frame
135 this.add(bExp);
136 this.add(bEval);
137 this.add(bClear);
138 this.add(bClearHistory);
139 this.add(bAdd);
140 this.add(bSub);
141 this.add(bMul);
142 this.add(bDiv);
143 this.add(bCut);
144 this.add(bRightPar);
145 this.add(bLeftPar);
146 this.add(bDoubleZero);
147 this.add(bZero);
148 this.add(bOne);
149 this.add(bTwo);
150 this.add(bThree);
151 this.add(bFour);
152 this.add(bFive);
153 this.add(bSix);
154 this.add(bSeven);
155 this.add(bEight);
156 this.add(bNine);
157 this.add(tf);
158 this.setSize(600, 400);
159 this.setLayout(null);
160 this.setVisible(true);
161
162 // JLabel label = new JLabel("Enter Expression: ");
163 // label.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
164 // label.setBounds(40, 20, 150, 40);
165 // label.setForeground(Color.decode("#e5e5e5"));
166 // this.add(label);
167
168 // ActionsListeners
169 bEval.addActionListener(new ActionListener() {
170 public void actionPerformed(ActionEvent e) {
171 String expr = tf.getText();
172 if (expr.length() == 0 || expr.equalsIgnoreCase("No input")) {
173 tf.setText("No input");
174 } else {
175 DecimalFormat format = new DecimalFormat();
176 p = new Parser(tf.getText());
177 String formattedResult = format.format(p.eval());
178 tf.setText(formattedResult);
179 String formattedExpr = expr + " = " + formattedResult;
180
181 // Dont add to history if its already present in histList
182 for (int i = 0; i < histList.size(); i++) {
183 if (histList.get(i).equals(formattedExpr)) {
184 return;
185 }
186 }
187 histList.addElement(formattedExpr);
188 }
189 }
190 });
191
192 // Common actions that just appends the symbols to the text field
193 actionAdderForTextField(bExp, "^");
194 actionAdderForTextField(bAdd, "+");
195 actionAdderForTextField(bSub, "-");
196 actionAdderForTextField(bMul, "*");
197 actionAdderForTextField(bDiv, "/");
198 actionAdderForTextField(bRightPar, ")");
199 actionAdderForTextField(bLeftPar, "(");
200 actionAdderForTextField(bDoubleZero, "00");
201 actionAdderForTextField(bZero, "0");
202 actionAdderForTextField(bOne, "1");
203 actionAdderForTextField(bTwo, "2");
204 actionAdderForTextField(bThree, "3");
205 actionAdderForTextField(bFour, "4");
206 actionAdderForTextField(bFive, "5");
207 actionAdderForTextField(bSix, "6");
208 actionAdderForTextField(bSeven, "7");
209 actionAdderForTextField(bEight, "8");
210 actionAdderForTextField(bNine, "9");
211
212 bClear.addActionListener(new ActionListener() {
213 public void actionPerformed(ActionEvent e) {
214 tf.setText("");
215 }
216 });
217
218 bClearHistory.addActionListener(new ActionListener() {
219 public void actionPerformed(ActionEvent e) {
220 histList.clear();
221 }
222 });
223
224 bCut.addActionListener(new ActionListener() {
225 public void actionPerformed(ActionEvent e) {
226 String text = tf.getText();
227 tf.setText(text.substring(0, text.length() - 1));
228 }
229 });
230
231 lHist.addMouseListener(new MouseAdapter() {
232 public void mouseClicked(MouseEvent evt) {
233 JList list = (JList) evt.getSource();
234 if (evt.getClickCount() == 2) {
235 // Double-click detected
236 int index = list.locationToIndex(evt.getPoint());
237 String expr = histList.get(index);
238 StringBuilder str = new StringBuilder("");
239 int endIndex = 0;
240 for (int i = 0; i < expr.length(); i++) {
241 if (expr.charAt(i) == '=') {
242 break;
243 }
244 endIndex++;
245 }
246 tf.setText(expr.substring(0, endIndex));
247 }
248 }
249 });
250
251 // bOne.addActionListener(new ActionListener() {
252 // public void actionPerformed(ActionEvent e) {
253 // tf.setText(tf.getText() + "1");
254 // }
255 // });
256 }
257}