Krolyxon
Commit 0776e6e

Branches

Refs →

jcalsrcGFrame.java

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