|
| 1 | +package calculator; |
| 2 | + |
| 3 | +import java.awt.Color; |
| 4 | +import java.awt.Dimension; |
| 5 | +import java.awt.Font; |
| 6 | +import java.awt.event.ActionEvent; |
| 7 | +import java.awt.event.ActionListener; |
| 8 | + |
| 9 | +import javax.swing.BorderFactory; |
| 10 | +import javax.swing.ImageIcon; |
| 11 | +import javax.swing.JButton; |
| 12 | +import javax.swing.JFrame; |
| 13 | +import javax.swing.JPanel; |
| 14 | +import javax.swing.JTextArea; |
| 15 | +import javax.swing.border.Border; |
| 16 | + |
| 17 | +public class Calculator implements ActionListener{ |
| 18 | +JFrame frame = new JFrame(); |
| 19 | +JPanel panel = new JPanel(); |
| 20 | +JTextArea textarea = new JTextArea(2,10); |
| 21 | + |
| 22 | +//now add 17 buttons in calculator |
| 23 | +JButton button1 = new JButton(); |
| 24 | +JButton button2 = new JButton(); |
| 25 | +JButton button3 = new JButton(); |
| 26 | +JButton button4 = new JButton(); |
| 27 | +JButton button5 = new JButton(); |
| 28 | +JButton button6 = new JButton(); |
| 29 | +JButton button7 = new JButton(); |
| 30 | +JButton button8 = new JButton(); |
| 31 | +JButton button9 = new JButton(); |
| 32 | +JButton button0 = new JButton(); |
| 33 | +JButton buttonAdd = new JButton(); |
| 34 | +JButton buttonSub = new JButton(); |
| 35 | +JButton buttonMul = new JButton(); |
| 36 | +JButton buttonDiv = new JButton(); |
| 37 | +JButton buttonDot = new JButton(); |
| 38 | +JButton buttonEqual = new JButton(); |
| 39 | +JButton buttonClear = new JButton(); |
| 40 | + |
| 41 | +double num1,num2,result; |
| 42 | +int addc=0,subc=0,mulc=0,divc=0; |
| 43 | + |
| 44 | + public Calculator() |
| 45 | + { |
| 46 | + frame.setSize(350,450); |
| 47 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 48 | + frame.setVisible(true);//makes the frame visible |
| 49 | + frame.setTitle("NIRMAL CALCULATOR"); |
| 50 | + |
| 51 | + frame.add(panel); |
| 52 | + //we add the panel in the frame |
| 53 | + panel.setBackground(Color.LIGHT_GRAY); |
| 54 | + //LIGHT GRAY is the background color of the panel |
| 55 | + Border border = BorderFactory.createLineBorder(Color.RED,4); |
| 56 | + //RED is the color of the border and 4 is the size of the border; we make the object of the Border class |
| 57 | + panel.setBorder(border); |
| 58 | + |
| 59 | + panel.add(textarea); |
| 60 | + textarea.setBackground(Color.BLACK); |
| 61 | + Border tborder = BorderFactory.createLineBorder(Color.BLUE,3); |
| 62 | + textarea.setBorder(tborder); |
| 63 | + |
| 64 | + Font font = new Font("arial",Font.BOLD,35); |
| 65 | + textarea.setFont(font); |
| 66 | + textarea.setForeground(Color.WHITE); |
| 67 | + textarea.setPreferredSize(new Dimension(2,10)); |
| 68 | + textarea.setLineWrap(true); |
| 69 | + |
| 70 | + //now adjust the dimensions of the buttons |
| 71 | + //then add icons of 17 buttons from their respective locations |
| 72 | + |
| 73 | + button1.setPreferredSize(new Dimension(100,43)); |
| 74 | + button1.setIcon(new ImageIcon("E:\\java projects\\calcButn\\1.jpg")); |
| 75 | + |
| 76 | + button2.setPreferredSize(new Dimension(100,43)); |
| 77 | + button2.setIcon(new ImageIcon("E:\\java projects\\calcButn\\2.jpg")); |
| 78 | + |
| 79 | + button3.setPreferredSize(new Dimension(100,43)); |
| 80 | + button3.setIcon(new ImageIcon("E:\\java projects\\calcButn\\3.jpg")); |
| 81 | + |
| 82 | + button4.setPreferredSize(new Dimension(100,43)); |
| 83 | + button4.setIcon(new ImageIcon("E:\\java projects\\calcButn\\4.jpg")); |
| 84 | + |
| 85 | + button5.setPreferredSize(new Dimension(100,43)); |
| 86 | + button5.setIcon(new ImageIcon("E:\\java projects\\calcButn\\5.jpg")); |
| 87 | + |
| 88 | + button6.setPreferredSize(new Dimension(100,43)); |
| 89 | + button6.setIcon(new ImageIcon("E:\\java projects\\calcButn\\6.jpg")); |
| 90 | + |
| 91 | + button7.setPreferredSize(new Dimension(100,43)); |
| 92 | + button7.setIcon(new ImageIcon("E:\\java projects\\calcButn\\7.jpg")); |
| 93 | + |
| 94 | + button8.setPreferredSize(new Dimension(100,43)); |
| 95 | + button8.setIcon(new ImageIcon("E:\\java projects\\calcButn\\8.jpg")); |
| 96 | + |
| 97 | + button9.setPreferredSize(new Dimension(100,43)); |
| 98 | + button9.setIcon(new ImageIcon("E:\\java projects\\calcButn\\9.jpg")); |
| 99 | + |
| 100 | + button0.setPreferredSize(new Dimension(100,43)); |
| 101 | + button0.setIcon(new ImageIcon("E:\\java projects\\calcButn\\0.jpg")); |
| 102 | + |
| 103 | + buttonAdd.setPreferredSize(new Dimension(100,43)); |
| 104 | + buttonAdd.setIcon(new ImageIcon("E:\\java projects\\calcButn\\adds.jpg")); |
| 105 | + |
| 106 | + buttonSub.setPreferredSize(new Dimension(100,43)); |
| 107 | + buttonSub.setIcon(new ImageIcon("E:\\java projects\\calcButn\\sub.jpg")); |
| 108 | + |
| 109 | + buttonMul.setPreferredSize(new Dimension(100,43)); |
| 110 | + buttonMul.setIcon(new ImageIcon("E:\\java projects\\calcButn\\mul.jpg")); |
| 111 | + |
| 112 | + buttonDiv.setPreferredSize(new Dimension(100,43)); |
| 113 | + buttonDiv.setIcon(new ImageIcon("E:\\java projects\\calcButn\\div.jpg")); |
| 114 | + |
| 115 | + buttonDot.setPreferredSize(new Dimension(100,43)); |
| 116 | + buttonDot.setIcon(new ImageIcon("E:\\java projects\\calcButn\\dot.jpg")); |
| 117 | + |
| 118 | + buttonEqual.setPreferredSize(new Dimension(200,56)); |
| 119 | + buttonEqual.setIcon(new ImageIcon("E:\\java projects\\calcButn\\==.jpg")); |
| 120 | + |
| 121 | + buttonClear.setPreferredSize(new Dimension(100,56)); |
| 122 | + buttonClear.setIcon(new ImageIcon("E:\\java projects\\calcButn\\Clear.jpg")); |
| 123 | + |
| 124 | + //now add those 17 buttons in the panel of the JFrame |
| 125 | + |
| 126 | + panel.add(button1); |
| 127 | + panel.add(button2); |
| 128 | + panel.add(button3); |
| 129 | + panel.add(button4); |
| 130 | + panel.add(button5); |
| 131 | + panel.add(button6); |
| 132 | + panel.add(button7); |
| 133 | + panel.add(button8); |
| 134 | + panel.add(button9); |
| 135 | + panel.add(button0); |
| 136 | + panel.add(buttonAdd); |
| 137 | + panel.add(buttonSub); |
| 138 | + panel.add(buttonMul); |
| 139 | + panel.add(buttonDiv); |
| 140 | + panel.add(buttonDot); |
| 141 | + panel.add(buttonEqual); |
| 142 | + panel.add(buttonClear); |
| 143 | + |
| 144 | + //now add action listener for each buttons |
| 145 | + |
| 146 | + button1.addActionListener(this); |
| 147 | + button2.addActionListener(this); |
| 148 | + button3.addActionListener(this); |
| 149 | + button4.addActionListener(this); |
| 150 | + button5.addActionListener(this); |
| 151 | + button6.addActionListener(this); |
| 152 | + button7.addActionListener(this); |
| 153 | + button8.addActionListener(this); |
| 154 | + button9.addActionListener(this); |
| 155 | + button0.addActionListener(this); |
| 156 | + buttonAdd.addActionListener(this); |
| 157 | + buttonSub.addActionListener(this); |
| 158 | + buttonMul.addActionListener(this); |
| 159 | + buttonDiv.addActionListener(this); |
| 160 | + buttonDot.addActionListener(this); |
| 161 | + buttonEqual.addActionListener(this); |
| 162 | + buttonClear.addActionListener(this); |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | +@Override |
| 167 | +public void actionPerformed(ActionEvent e) |
| 168 | +{ |
| 169 | + //firstly make the object of Object class named source here |
| 170 | + Object source = e.getSource(); |
| 171 | + |
| 172 | + //now add respective actions of each buttons |
| 173 | + |
| 174 | + if(source==buttonClear) |
| 175 | + { |
| 176 | + //this will CLEAR the screen |
| 177 | + num1=0.0; |
| 178 | + num2=0.0; |
| 179 | + textarea.setText(""); |
| 180 | + } |
| 181 | + |
| 182 | + if(source==button1){ |
| 183 | + textarea.append("1"); |
| 184 | + } |
| 185 | + if(source==button2){ |
| 186 | + textarea.append("2"); |
| 187 | + } |
| 188 | + if(source==button3){ |
| 189 | + textarea.append("3"); |
| 190 | + } |
| 191 | + if(source==button4){ |
| 192 | + textarea.append("4"); |
| 193 | + } |
| 194 | + if(source==button5){ |
| 195 | + textarea.append("5"); |
| 196 | + } |
| 197 | + if(source==button6){ |
| 198 | + textarea.append("6"); |
| 199 | + } |
| 200 | + if(source==button7){ |
| 201 | + textarea.append("7"); |
| 202 | + } |
| 203 | + if(source==button8){ |
| 204 | + textarea.append("8"); |
| 205 | + } |
| 206 | + if(source==button9){ |
| 207 | + textarea.append("9"); |
| 208 | + } |
| 209 | + if(source==button0){ |
| 210 | + textarea.append("0"); |
| 211 | + } |
| 212 | + if(source==buttonDot){ |
| 213 | + textarea.append("."); |
| 214 | + } |
| 215 | + |
| 216 | + |
| 217 | + if(source==buttonAdd){ |
| 218 | + num1 = number_reader(); |
| 219 | + textarea.setText(""); |
| 220 | + //textarea.append("+"); |
| 221 | + addc=1; |
| 222 | + subc=0; |
| 223 | + mulc=0; |
| 224 | + divc=0; |
| 225 | + } |
| 226 | + if(source==buttonSub){ |
| 227 | + num1 = number_reader(); |
| 228 | + textarea.setText(""); |
| 229 | + //textarea.append("-"); |
| 230 | + subc=1; |
| 231 | + addc=0; |
| 232 | + mulc=0; |
| 233 | + divc=0; |
| 234 | + } |
| 235 | + if(source==buttonMul){ |
| 236 | + num1 = number_reader(); |
| 237 | + textarea.setText(""); |
| 238 | + //textarea.append("*"); |
| 239 | + mulc=1; |
| 240 | + addc=0; |
| 241 | + subc=0; |
| 242 | + divc=0; |
| 243 | + } |
| 244 | + if(source==buttonDiv){ |
| 245 | + num1 = number_reader(); |
| 246 | + textarea.setText(""); |
| 247 | + //textarea.append("/"); |
| 248 | + divc = 1; |
| 249 | + addc=0; |
| 250 | + subc=0; |
| 251 | + mulc=0; |
| 252 | + } |
| 253 | + |
| 254 | + //now if user press EQUAL SIGN(=) to get result perform following action |
| 255 | + |
| 256 | + if(source==buttonEqual) |
| 257 | + { |
| 258 | + //first read number2 before performing any operation |
| 259 | + num2 = number_reader(); |
| 260 | + |
| 261 | + if(addc>0){ |
| 262 | + //to do addition |
| 263 | + result = num1 + num2; |
| 264 | + textarea.setText(Double.toString(result)); |
| 265 | + } |
| 266 | + if(subc>0){ |
| 267 | + //to do subtraction |
| 268 | + result = num1 - num2; |
| 269 | + textarea.setText(Double.toString(result)); |
| 270 | + } |
| 271 | + if(mulc>0){ |
| 272 | + //to do multiplication |
| 273 | + result = num1 * num2; |
| 274 | + textarea.setText(Double.toString(result)); |
| 275 | + } |
| 276 | + if(divc>0){ |
| 277 | + //for performing division operation |
| 278 | + result = num1/num2; |
| 279 | + textarea.setText(Double.toString(result)); |
| 280 | + } |
| 281 | + |
| 282 | + } |
| 283 | +} |
| 284 | + |
| 285 | +//this number_reader method will convert the received string format input i.e 1,2... into number format |
| 286 | +//to display in screen and returns that number |
| 287 | + |
| 288 | +public double number_reader(){ |
| 289 | + double num1; |
| 290 | + String s; |
| 291 | + s = textarea.getText(); |
| 292 | + num1 = Double.valueOf(s); |
| 293 | + return num1; |
| 294 | +} |
| 295 | + |
| 296 | +} |
0 commit comments