Krolyxon
Commit 71eeb0f

Branches

Refs →

jcalsrcGFrame.java

238 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
80 // History
81 DefaultListModel<String> histList = new DefaultListModel<>();
82 JList<String> lHist = new JList<>(histList);
83 lHist.setBackground(Color.decode("#2B2B2B"));
84 lHist.setForeground(Color.decode("#e5e5e5"));
85 lHist.setBounds(380, 50, 200, 240);
86 lHist.setFont(new Font("Times New Roman", Font.PLAIN, 20));
87 lHist.setBorder(new RoundBtn(5));
88 this.add(lHist);
89
90 // Buttons
91 JButton bEval = newButton("=", 290, 50, 60, 40);
92 JButton bExp = newButton("^", 250, 100, 100, 40);
93 JButton bClear = newButton("CL", 180, 100, 60, 40);
94 JButton bClearHistory = newButton("Clear History", 380, 300, 200, 40);
95 JButton bAdd = newButton("+", 250, 300, 100, 40);
96 JButton bSub = newButton("-", 250, 250, 100, 40);
97 JButton bMul = newButton("x", 250, 200, 100, 40);
98 JButton bDiv = newButton("%", 250, 150, 100, 40);
99 JButton bCut = newButton("", 180, 300, 60, 40);
100 bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20));
101 JButton bRightPar = newButton(")", 110, 100, 60, 40);
102 JButton bLeftPar = newButton("(", 40, 100, 60, 40);
103 JButton bDoubleZero = newButton("00", 40, 300, 60, 40);
104 JButton bZero = newButton("0", 110, 300, 60, 40);
105 JButton bOne = newButton("1", 40, 250, 60, 40);
106 JButton bTwo = newButton("2", 110, 250, 60, 40);
107 JButton bThree = newButton("3", 180, 250, 60, 40);
108 JButton bFour = newButton("4", 40, 200, 60, 40);
109 JButton bFive = newButton("5", 110, 200, 60, 40);
110 JButton bSix = newButton("6", 180, 200, 60, 40);
111 JButton bSeven = newButton("7", 40, 150, 60, 40);
112 JButton bEight = newButton("8", 110, 150, 60, 40);
113 JButton bNine = newButton("9", 180, 150, 60, 40);
114
115 // Add the buttons to the Frame
116 this.add(bExp);
117 this.add(bEval);
118 this.add(bClear);
119 this.add(bClearHistory);
120 this.add(bAdd);
121 this.add(bSub);
122 this.add(bMul);
123 this.add(bDiv);
124 this.add(bCut);
125 this.add(bRightPar);
126 this.add(bLeftPar);
127 this.add(bDoubleZero);
128 this.add(bZero);
129 this.add(bOne);
130 this.add(bTwo);
131 this.add(bThree);
132 this.add(bFour);
133 this.add(bFive);
134 this.add(bSix);
135 this.add(bSeven);
136 this.add(bEight);
137 this.add(bNine);
138 this.add(tf);
139 this.setSize(600, 400);
140 this.setLayout(null);
141 this.setVisible(true);
142
143 JLabel label = new JLabel("Enter Expression: ");
144 label.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
145 label.setBounds(40, 20, 150, 40);
146 label.setForeground(Color.decode("#e5e5e5"));
147 this.add(label);
148
149 // ActionsListeners
150 bEval.addActionListener(new ActionListener() {
151 public void actionPerformed(ActionEvent e) {
152 if (tf.getText().length() != 0) {
153 String expression = tf.getText();
154 DecimalFormat format = new DecimalFormat();
155 p = new Parser(tf.getText());
156 String formattedResult = format.format(p.eval());
157 tf.setText(formattedResult);
158 String formattedExpr = expression + " = " + formattedResult;
159
160 // Dont add to history if its already present in histList
161 for (int i = 0; i < histList.size(); i++) {
162 if (histList.get(i).equals(formattedExpr)) {
163 return;
164 }
165 }
166 histList.addElement(formattedExpr);
167 } else {
168 tf.setText("No input");
169 }
170 }
171 });
172
173 // Common actions that just appends the symbols to the text field
174 actionAdderForTextField(bExp, "^");
175 actionAdderForTextField(bAdd, "+");
176 actionAdderForTextField(bSub, "-");
177 actionAdderForTextField(bMul, "*");
178 actionAdderForTextField(bDiv, "/");
179 actionAdderForTextField(bRightPar, ")");
180 actionAdderForTextField(bLeftPar, "(");
181 actionAdderForTextField(bDoubleZero, "00");
182 actionAdderForTextField(bZero, "0");
183 actionAdderForTextField(bOne, "1");
184 actionAdderForTextField(bTwo, "2");
185 actionAdderForTextField(bThree, "3");
186 actionAdderForTextField(bFour, "4");
187 actionAdderForTextField(bFive, "5");
188 actionAdderForTextField(bSix, "6");
189 actionAdderForTextField(bSeven, "7");
190 actionAdderForTextField(bEight, "8");
191 actionAdderForTextField(bNine, "9");
192
193 bClear.addActionListener(new ActionListener() {
194 public void actionPerformed(ActionEvent e) {
195 tf.setText("");
196 }
197 });
198
199 bClearHistory.addActionListener(new ActionListener() {
200 public void actionPerformed(ActionEvent e) {
201 histList.clear();
202 }
203 });
204
205 bCut.addActionListener(new ActionListener() {
206 public void actionPerformed(ActionEvent e) {
207 String text = tf.getText();
208 tf.setText(text.substring(0, text.length() - 1));
209 }
210 });
211
212 lHist.addMouseListener(new MouseAdapter() {
213 public void mouseClicked(MouseEvent evt) {
214 JList list = (JList) evt.getSource();
215 if (evt.getClickCount() == 2) {
216 // Double-click detected
217 int index = list.locationToIndex(evt.getPoint());
218 String expr = histList.get(index);
219 StringBuilder str = new StringBuilder("");
220 int endIndex = 0;
221 for (int i = 0; i < expr.length(); i++) {
222 if (expr.charAt(i) == '=') {
223 break;
224 }
225 endIndex++;
226 }
227 tf.setText(expr.substring(0, endIndex));
228 }
229 }
230 });
231
232 // bOne.addActionListener(new ActionListener() {
233 // public void actionPerformed(ActionEvent e) {
234 // tf.setText(tf.getText() + "1");
235 // }
236 // });
237 }
238}