| 1 | /* |
| 2 | * TODO: |
| 3 | * Item positions are hard coded, make them dynamic |
| 4 | * so that window can be set to resizable. |
| 5 | */ |
| 6 | |
| 7 | import java.util.Vector; |
| 8 | import javax.swing.*; |
| 9 | import java.awt.Color; |
| 10 | import java.awt.Dimension; |
| 11 | import java.awt.Font; |
| 12 | import java.awt.Insets; |
| 13 | import java.awt.event.*; |
| 14 | import java.awt.*; |
| 15 | import javax.swing.border.Border; |
| 16 | import java.text.DecimalFormat; |
| 17 | |
| 18 | class 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 | |
| 39 | public 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, 290); |
| 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 bAdd = newButton("+", 250, 300, 100, 40); |
| 95 | JButton bSub = newButton("-", 250, 250, 100, 40); |
| 96 | JButton bMul = newButton("x", 250, 200, 100, 40); |
| 97 | JButton bDiv = newButton("%", 250, 150, 100, 40); |
| 98 | JButton bCut = newButton("", 180, 300, 60, 40); |
| 99 | bCut.setFont(new Font("JetBrainsMono Nerd Font", Font.PLAIN, 20)); |
| 100 | JButton bRightPar = newButton(")", 110, 100, 60, 40); |
| 101 | JButton bLeftPar = newButton("(", 40, 100, 60, 40); |
| 102 | JButton bDoubleZero = newButton("00", 40, 300, 60, 40); |
| 103 | JButton bZero = newButton("0", 110, 300, 60, 40); |
| 104 | JButton bOne = newButton("1", 40, 250, 60, 40); |
| 105 | JButton bTwo = newButton("2", 110, 250, 60, 40); |
| 106 | JButton bThree = newButton("3", 180, 250, 60, 40); |
| 107 | JButton bFour = newButton("4", 40, 200, 60, 40); |
| 108 | JButton bFive = newButton("5", 110, 200, 60, 40); |
| 109 | JButton bSix = newButton("6", 180, 200, 60, 40); |
| 110 | JButton bSeven = newButton("7", 40, 150, 60, 40); |
| 111 | JButton bEight = newButton("8", 110, 150, 60, 40); |
| 112 | JButton bNine = newButton("9", 180, 150, 60, 40); |
| 113 | |
| 114 | // Add the buttons to the Frame |
| 115 | this.add(bEval); |
| 116 | this.add(bClear); |
| 117 | this.add(bAdd); |
| 118 | this.add(bSub); |
| 119 | this.add(bMul); |
| 120 | this.add(bDiv); |
| 121 | this.add(bCut); |
| 122 | this.add(bRightPar); |
| 123 | this.add(bLeftPar); |
| 124 | this.add(bDoubleZero); |
| 125 | this.add(bZero); |
| 126 | this.add(bOne); |
| 127 | this.add(bTwo); |
| 128 | this.add(bThree); |
| 129 | this.add(bFour); |
| 130 | this.add(bFive); |
| 131 | this.add(bSix); |
| 132 | this.add(bSeven); |
| 133 | this.add(bEight); |
| 134 | this.add(bNine); |
| 135 | this.add(tf); |
| 136 | this.add(tHist); |
| 137 | this.setSize(600, 400); |
| 138 | this.setLayout(null); |
| 139 | this.setVisible(true); |
| 140 | |
| 141 | JLabel label = new JLabel("Enter Expression: "); |
| 142 | label.setFont((new Font("Times New Roman", Font.PLAIN, 20))); |
| 143 | label.setBounds(40, 20, 150, 40); |
| 144 | label.setForeground(Color.decode("#e5e5e5")); |
| 145 | this.add(label); |
| 146 | |
| 147 | // ActionsListeners |
| 148 | bEval.addActionListener(new ActionListener() { |
| 149 | public void actionPerformed(ActionEvent e) { |
| 150 | if (tf.getText().length() != 0) { |
| 151 | String expression = tf.getText(); |
| 152 | DecimalFormat format = new DecimalFormat(); |
| 153 | p = new Parser(tf.getText()); |
| 154 | String formattedResult = format.format(p.eval()); |
| 155 | tf.setText(formattedResult); |
| 156 | |
| 157 | tHist.setText(""); |
| 158 | Vector<String> s = history.getHistory(); |
| 159 | s.add(expression + " = " + formattedResult); |
| 160 | for (int i = 0; i < s.size(); i++) { |
| 161 | // tHist.setText(new String(s.get(i).concat(tHist.getText())).concat("\n")); |
| 162 | tHist.append(s.get(i)); |
| 163 | tHist.append("\n"); |
| 164 | } |
| 165 | |
| 166 | } else { |
| 167 | tf.setText("No input"); |
| 168 | } |
| 169 | } |
| 170 | }); |
| 171 | |
| 172 | // Common actions that just appends the symbols to the text field |
| 173 | actionAdderForTextField(bAdd, "+"); |
| 174 | actionAdderForTextField(bSub, "-"); |
| 175 | actionAdderForTextField(bMul, "*"); |
| 176 | actionAdderForTextField(bDiv, "/"); |
| 177 | actionAdderForTextField(bRightPar, ")"); |
| 178 | actionAdderForTextField(bLeftPar, "("); |
| 179 | actionAdderForTextField(bDoubleZero, "00"); |
| 180 | actionAdderForTextField(bZero, "0"); |
| 181 | actionAdderForTextField(bOne, "1"); |
| 182 | actionAdderForTextField(bTwo, "2"); |
| 183 | actionAdderForTextField(bThree, "3"); |
| 184 | actionAdderForTextField(bFour, "4"); |
| 185 | actionAdderForTextField(bFive, "5"); |
| 186 | actionAdderForTextField(bSix, "6"); |
| 187 | actionAdderForTextField(bSeven, "7"); |
| 188 | actionAdderForTextField(bEight, "8"); |
| 189 | actionAdderForTextField(bNine, "9"); |
| 190 | |
| 191 | bClear.addActionListener(new ActionListener() { |
| 192 | public void actionPerformed(ActionEvent e) { |
| 193 | tf.setText(""); |
| 194 | } |
| 195 | }); |
| 196 | |
| 197 | bCut.addActionListener(new ActionListener() { |
| 198 | public void actionPerformed(ActionEvent e) { |
| 199 | String text = tf.getText(); |
| 200 | tf.setText(text.substring(0, text.length() - 1)); |
| 201 | } |
| 202 | }); |
| 203 | |
| 204 | // bOne.addActionListener(new ActionListener() { |
| 205 | // public void actionPerformed(ActionEvent e) { |
| 206 | // tf.setText(tf.getText() + "1"); |
| 207 | // } |
| 208 | // }); |
| 209 | } |
| 210 | } |