Krolyxon
Commit 4416101

Branches

Refs →

jcalsrcGFrame.java

219 lines · 7.5 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;
17
18class 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}
38
39public class GFrame extends JFrame {
40 Parser p;
41 JTextField tf;
42
43 private JButton newButton(String text, int d1, int d2, int d3, int d4) {
44 JButton b = new JButton();
45 // b.setBackground(Color.decode("#F78361"));
46 b.setBackground(Color.decode("#2B2B2B"));
47 b.setForeground(Color.decode("#e5e5e5"));
48 b.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
49 b.setBounds(d1, d2, d3, d4);
50 b.setBorder(new RoundBtn(15));
51 b.setText(text);
52 return b;
53 }
54
55 private void actionAdderForTextField(JButton b, String val) {
56 b.addActionListener(new ActionListener() {
57 public void actionPerformed(ActionEvent e) {
58 tf.setText(tf.getText() + val);
59 }
60 });
61
62 }
63
64 GFrame(String title) {
65 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
66 this.setTitle(title);
67 this.getContentPane().setBackground(Color.decode("#111111"));
68 this.setResizable(false);
69
70 // Text Field
71 tf = new JTextField();
72 tf.setBackground(Color.decode("#2B2B2B"));
73 tf.setForeground(Color.decode("#e5e5e5"));
74 tf.setMargin(new Insets(0, 10, 0, 10));
75 tf.setBounds(40, 50, 310, 40);
76 tf.setFont((new Font("Times New Roman", Font.PLAIN, 20)));
77 tf.setBorder(new RoundBtn(5));
78
79 // History Text Field
80 History history = new History();
81 JTextArea tHist = new JTextArea();
82 tHist.setBounds(380, 50, 200, 240);
83 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));
90
91 // Buttons
92 JButton bEval = 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(bEval);
117 this.add(bClear);
118 this.add(bClearHistory);
119 this.add(bAdd);
120 this.add(bSub);
121 this.add(bMul);
122 this.add(bDiv);
123 this.add(bCut);
124 this.add(bRightPar);
125 this.add(bLeftPar);
126 this.add(bDoubleZero);
127 this.add(bZero);
128 this.add(bOne);
129 this.add(bTwo);
130 this.add(bThree);
131 this.add(bFour);
132 this.add(bFive);
133 this.add(bSix);
134 this.add(bSeven);
135 this.add(bEight);
136 this.add(bNine);
137 this.add(tf);
138 this.add(tHist);
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
159 tHist.setText("");
160 Vector<String> s = history.getHistory();
161 s.add(expression + " = " + formattedResult);
162 for (int i = 0; i < s.size(); i++) {
163 // tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n"));
164 tHist.append(s.get(i));
165 tHist.append("\n");
166 }
167
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(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 history.clearHistory();
202 tHist.setText("");
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 // bOne.addActionListener(new ActionListener() {
214 // public void actionPerformed(ActionEvent e) {
215 // tf.setText(tf.getText() + "1");
216 // }
217 // });
218 }
219}