All pastes #2072827 Raw Edit

Stuff

public text v1 · immutable
#2072827 ·published 2011-05-31 15:39 UTC
rendered paste body
import java.awt.*;
import java.awt.event.*;            // imported so action can be implemented
import java.awt.Color.*;            // imported so different colors can be set to JFrame components
import java.awt.image.*;            // imported so images can be added to JFrame components
import java.text.DecimalFormat;     // imported so formatting a string for 'Digit grouping' menu item is possible

import java.math.BigDecimal;        // imported so conversion is possible when formatting a string for 'Digit grouping' menu item

import javax.swing.*;
import javax.swing.border.*;
//import javax.swing.JMenu;          --> it is not necessary as it has already been imported through javax.swing.*
//import javax.swing.JMenuBar;       --> it is not necessary as it has already been imported through javax.swing.*
//import javax.swing.JMenuItem;      --> it is not necessary as it has already been imported through javax.swing.*

public class Calculator extends JFrame{

	final int MAX_INPUT_LENGTH = 31;
	final int INPUT_MODE = 0;
	final int RESULT_MODE = 1;
	final int ERROR_MODE = 2;
	int displayMode;

	static double m1, storeValueCopyPaste; // variables for memory buttons functionality

	int k = 1;

	double lastNumber;
	boolean clearOnNextDigit, percent;
	String lastOperator;

	JButton NumberedButtons[];
	JButton but10, but11, but12, but13, but14, but15, but16, but17, but18, but19, but20, but21, but22;
	JButton butM1, butM2, butM3, butM4;

	JMenu jmenuEdit, jmenuView, jmenuHelp;
	JMenuItem jmenuitemCopy, jmenuitemPaste, jmenuitemStandard, jmenuitemScientific, jmenuitemDigit, jmenuitemHelp, jmenuitemAbout;

    Border raisedbevel, loweredbevel, border;

	JLabel memoryTextfield, labelOutPut;

	public Calculator()
	{
 		border = LineBorder.createGrayLineBorder();
		raisedbevel = BorderFactory.createRaisedBevelBorder();
        loweredbevel = BorderFactory.createLoweredBevelBorder();

		Font f12 = new Font("MS Shell Dlg", 0, 11);
		Font f121 = new Font("MS Shell Dlg", 0, 12);

		/*Edit menu*/
		jmenuEdit = new JMenu("Edit");
		jmenuEdit.setFont(f12);
		jmenuEdit.setMnemonic(KeyEvent.VK_F);

		// Edit->Copy, C - Mnemonic, CTRL-C - Accelerator
		jmenuitemCopy = new JMenuItem("    Copy", KeyEvent.VK_C);

		ActionPerformedMenuBar MenuItemsAction = new ActionPerformedMenuBar();
		KeyStroke ctrlCKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_C,ActionEvent.CTRL_MASK);
		jmenuitemCopy.setAccelerator(ctrlCKeyStroke);
		jmenuitemCopy.setFont(f121);
		jmenuitemCopy.addActionListener(MenuItemsAction);


		// Edit->Paste, P - Mnemonic, CTRL-V - Accelerator
		jmenuitemPaste = new JMenuItem("    Paste");

		KeyStroke ctrlPKeyStroke = KeyStroke.getKeyStroke(KeyEvent.VK_V,ActionEvent.CTRL_MASK);
		jmenuitemPaste.setAccelerator(ctrlPKeyStroke);
		jmenuitemPaste.setFont(f121);
		jmenuitemPaste.addActionListener(MenuItemsAction);
		jmenuitemPaste.setEnabled(false);

		jmenuEdit.add(jmenuitemCopy);
		jmenuEdit.add(jmenuitemPaste);

		/*View menu*/
		jmenuView = new JMenu("View");
		jmenuView.setFont(f12);
		jmenuView.setMnemonic(KeyEvent.VK_H);

		JLabel menuImg = new JLabel(new ImageIcon("images/menuImg.png"));
		menuImg.setBorder(new EmptyBorder(0, 10, 0, 0));

		jmenuitemStandard = new JMenuItem("    Standard");
		jmenuitemStandard.setFont(f121);
		jmenuitemStandard.setPreferredSize(new Dimension(0, 23));
		jmenuitemStandard.setMargin(new Insets(0, 0, 0, 110));

		jmenuitemScientific = new JMenuItem("    Scientific");
		jmenuitemScientific.setFont(f121);
		jmenuitemScientific.setEnabled(false);

		jmenuitemDigit = new JMenuItem("    Digital grouping");
		jmenuitemDigit.setFont(f121);
		jmenuitemDigit.addActionListener(MenuItemsAction);

		jmenuitemStandard.add(menuImg);

		jmenuView.add(jmenuitemStandard);
		jmenuView.add(jmenuitemScientific);
		jmenuView.addSeparator();

		jmenuView.add(jmenuitemDigit);

		/*Help menu*/
		jmenuHelp = new JMenu("Help");
		jmenuHelp.setFont(f12);
		jmenuHelp.setMnemonic(KeyEvent.VK_H);

		jmenuitemHelp = new JMenuItem("    Help Topics");
		jmenuitemHelp.setFont(f121);
		jmenuitemHelp.addActionListener(MenuItemsAction);

		jmenuitemAbout = new JMenuItem("    About calculator");
		jmenuitemAbout.setFont(f121);
		jmenuitemAbout.addActionListener(MenuItemsAction);

		jmenuHelp.add(jmenuitemHelp);
		jmenuHelp.addSeparator();
		jmenuHelp.add(jmenuitemAbout);

		JMenuBar mb = new JMenuBar();
		mb.add(jmenuEdit);
		mb.add(jmenuView);
		mb.add(jmenuHelp);
		//setJMenuBar(mb);

		// Add components to frame
		add(mb,BorderLayout.NORTH);

		JPanel jplBackSpace, jplControl, jplButtons;

		jplButtons = new JPanel();
		// Set jplButton panel layout manager for a 4 by 5 grid
		jplButtons.setLayout(new GridLayout(4, 5, 4, 4));
		jplButtons.setBorder(new EmptyBorder(4, 12, 0, 0));

		// Create numeric Jbuttons
		NumberedButtons = new JButton[11];
		for (int i=0; i<=9; i++)
		{

			ActionPerformedNumbers numbersAction = new ActionPerformedNumbers();

			// set each Jbutton label to the value of index
			NumberedButtons[i] = new JButton(String.valueOf(i));
			NumberedButtons[i].setMargin(new Insets(0, 0, 0, 0));
			NumberedButtons[i].setFont(f12);
			NumberedButtons[i].setForeground(Color.blue);
			NumberedButtons[i].setBackground(new Color(242,243,238));

			NumberedButtons[i].setFocusable(false);
			NumberedButtons[i].addActionListener(numbersAction);
		}

		// Create operator Jbuttons
		but10 = new JButton("+/-");
		but10.setMargin(new Insets(0, 0, 0, 0));
		but10.setFont(f12);
		but10.setFocusable(false);
		but10.setForeground(Color.blue);
		but10.setBackground(new Color(242,243,238));
		ActionPerformedPlusSign pressedPlusMinusSign = new ActionPerformedPlusSign();
		but10.addActionListener(pressedPlusMinusSign);

		but11 = new JButton(".");
		but11.setMargin(new Insets(0, 0, 0, 0));
		but11.setFont(f12);
		but11.setFocusable(false);
		but11.setForeground(Color.blue);
		but11.setBackground(new Color(242,243,238));
		ActionPerformedDecimalPoint pressedPlusDecimalPoint = new ActionPerformedDecimalPoint();
		but11.addActionListener(pressedPlusDecimalPoint);

		but12 = new JButton("=");
		but12.setMargin(new Insets(0, 0, 0, 0));
		but12.setFont(f12);
		but12.setFocusable(false);
		but12.setForeground(Color.red);
		but12.setBackground(new Color(242,243,238));
		ActionPerformedEquals pressedEquals = new ActionPerformedEquals();
		but12.addActionListener(pressedEquals);

		but13 = new JButton("/");
		but13.setMargin(new Insets(0, 0, 0, 0));
		but13.setFont(f12);
		but13.setFocusable(false);
		but13.setForeground(Color.red);
		but13.setBackground(new Color(242,243,238));
		ActionPerformedDevide pressedDevide = new ActionPerformedDevide();
		but13.addActionListener(pressedDevide);

		but14 = new JButton("*");
		but14.setMargin(new Insets(0, 0, 0, 0));
		but14.setFont(f12);
		but14.setFocusable(false);
		but14.setForeground(Color.red);
		but14.setBackground(new Color(242,243,238));
		ActionPerformedMultiply pressedMultiply = new ActionPerformedMultiply();
		but14.addActionListener(pressedMultiply);

		but15 = new JButton("-");
		but15.setMargin(new Insets(0, 0, 0, 0));
		but15.setFont(f12);
		but15.setFocusable(false);
		but15.setForeground(Color.red);
		but15.setBackground(new Color(242,243,238));
		ActionPerformedMinus pressedMinus = new ActionPerformedMinus();
		but15.addActionListener(pressedMinus);

		but16 = new JButton("+");
		but16.setMargin(new Insets(0, 0, 0, 0));
		but16.setFont(f12);
		but16.setFocusable(false);
		but16.setForeground(Color.red);
		but16.setBackground(new Color(242,243,238));
		ActionPerformedPlus pressedPlus = new ActionPerformedPlus();
		but16.addActionListener(pressedPlus);

		but17 = new JButton("sqrt");
		but17.setMargin(new Insets(0, 0, 0, 0));
		but17.setFont(f12);
		but17.setFocusable(false);
		but17.setForeground(Color.blue);
		but17.setBackground(new Color(242,243,238));
		ActionPerformedSqrt pressedSqrt = new ActionPerformedSqrt();
		but17.addActionListener(pressedSqrt);

		but18 = new JButton("1/x");
		but18.setMargin(new Insets(0, 0, 0, 0));
		but18.setFont(f12);
		but18.setFocusable(false);
		but18.setForeground(Color.blue);
		but18.setBackground(new Color(242,243,238));
		ActionPerformedReciprocal pressedReciprocal = new ActionPerformedReciprocal();
		but18.addActionListener(pressedReciprocal);

		but19 = new JButton("%");
		but19.setMargin(new Insets(0, 0, 0, 0));
		but19.setFont(f12);
		but19.setFocusable(false);
		but19.setForeground(Color.blue);
		but19.setBackground(new Color(242,243,238));
		ActionPerformedPercentage pressedPercentage = new ActionPerformedPercentage();
		but19.addActionListener(pressedPercentage);

		//panel is the main Jpanel, so it contains jplControl, jplControl2, JButtons
		JPanel panel = new JPanel(new BorderLayout(0, 0));
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setBorder(new EmptyBorder(3, 9, 8, 5));
		// panel.setBackground(Color.yellow);

		//output numbers, result of calculations
		labelOutPut = new JLabel("0");

		labelOutPut.setHorizontalAlignment(JLabel.RIGHT);
		labelOutPut.setBackground(Color.WHITE);
		labelOutPut.setFont(f121);
		labelOutPut.setOpaque(true);
		labelOutPut.setPreferredSize(new Dimension(100,23));
		labelOutPut.setBorder(loweredbevel);

		panel.add(labelOutPut, BorderLayout.NORTH);

		//text field for MC, MR, MS, M+
		memoryTextfield = new JLabel("");
		memoryTextfield.setHorizontalAlignment(JLabel.CENTER);
		memoryTextfield.setBackground(new Color(237,237,237));
		memoryTextfield.setFont(f121);
		memoryTextfield.setOpaque(true);
		memoryTextfield.setPreferredSize(new Dimension(27,26));
		memoryTextfield.setBorder(loweredbevel);


		panel.add(labelOutPut, BorderLayout.NORTH);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EmptyBorder(0, 0, 0, 0));
		panel.add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new BorderLayout(0, 0));


		//Add buttons to keypad panel
		// First row

		jplButtons.add(NumberedButtons[7]);
		jplButtons.add(NumberedButtons[8]);
		jplButtons.add(NumberedButtons[9]);

		// add button / and sqrt
		jplButtons.add(but13);
		jplButtons.add(but17);

		// Second row

		jplButtons.add(NumberedButtons[4]);
		jplButtons.add(NumberedButtons[5]);
		jplButtons.add(NumberedButtons[6]);

		// add button * and x^2
		jplButtons.add(but14);
		jplButtons.add(but19);

		// Third row
		jplButtons.add(NumberedButtons[1]);
		jplButtons.add(NumberedButtons[2]);
		jplButtons.add(NumberedButtons[3]);


		//adds button - and %
		jplButtons.add(but15);
		jplButtons.add(but18);

		//Fourth Row
		// add 0, +/-, ., +, and =
		jplButtons.add(NumberedButtons[0]);
		jplButtons.add(but10);
		jplButtons.add(but11);
		jplButtons.add(but16);
		jplButtons.add(but12);

		//Add jPanel jplButtons to jPanel panel_1
		panel_1.add(jplButtons, BorderLayout.CENTER);




		//jplControl is responsible for Backspace, CE, C

		jplControl = new JPanel();
		FlowLayout flowLayout = (FlowLayout) jplControl.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		jplControl.setBorder(new EmptyBorder(6, 0, 0, 0));

		//jplControl.setBackground(Color.red);

		panel_1.add(jplControl, BorderLayout.NORTH);

			//buttons inside the jplControl
		but20 = new JButton("Backspace");
		but20.setMargin(new Insets(5, 2, 5, 2));
		but20.setFont(f12);
		but20.setFocusable(false);
		but20.setForeground(Color.red);
		but20.setBackground(new Color(242,243,238));
		ActionPerformedBackspace pressedBackspace = new ActionPerformedBackspace();
		but20.addActionListener(pressedBackspace);

		but21 = new JButton("CE");
		but21.setMargin(new Insets(4, 21, 4, 21));
		but21.setFont(f121);
		but21.setFocusable(false);
		but21.setForeground(Color.red);
		but21.setBackground(new Color(242,243,238));
		ActionPerformedClearExisting pressedClearExisting = new ActionPerformedClearExisting();
		but21.addActionListener(pressedClearExisting);

		but22 = new JButton("C");
		but22.setMargin(new Insets(4, 23, 4, 23));
		but22.setFont(f121);
		but22.setFocusable(false);
		but22.setForeground(Color.red);
		but22.setBackground(new Color(242,243,238));
		ActionPerformedClearAll pressedClearAll = new ActionPerformedClearAll();
		but22.addActionListener(pressedClearAll);

		JLabel emptySpace = new JLabel("   ");


		jplControl.add(memoryTextfield);

		// jplControl.add(emptySpace);

		jplControl.add(emptySpace);

		jplControl.add(but20);
		jplControl.add(but21);
		jplControl.add(but22);

		//jplControl2 is responsible for MC, MR, MS, M+

		JPanel memoryPanel = new JPanel();

		memoryPanel.setBorder(new EmptyBorder(2, 0, 0, 0));

		JPanel jplControl2 = new JPanel(new GridLayout(4, 1, 0, 4));
		panel_1.add(jplControl2, BorderLayout.WEST);
		jplControl2.setBorder(new EmptyBorder(3, 0, 0, 0));

		butM1 = new JButton("MC");
		butM1.setMargin(new Insets(10, 6, 9, 6));
		butM1.setFont(f121);
		butM1.setFocusable(false);
		butM1.setForeground(Color.red);
		butM1.setBackground(new Color(242,243,238));
		ActionPerformedMemoryClear pressedMC = new ActionPerformedMemoryClear();
		butM1.addActionListener(pressedMC);

		butM2 = new JButton("MR");
		butM2.setMargin(new Insets(10, 6, 9, 6));
		butM2.setFont(f121);
		butM2.setFocusable(false);
		butM2.setForeground(Color.red);
		butM2.setBackground(new Color(242,243,238));
		ActionPerformedMemoryRemind pressedMR = new ActionPerformedMemoryRemind();
		butM2.addActionListener(pressedMR);

		butM3 = new JButton("MS");
		butM3.setMargin(new Insets(10, 6, 9, 6));
		butM3.setFont(f121);
		butM3.setFocusable(false);
		butM3.setForeground(Color.red);
		butM3.setBackground(new Color(242,243,238));
		ActionPerformedMemorySave pressedMS = new ActionPerformedMemorySave();
		butM3.addActionListener(pressedMS);

		butM4 = new JButton("M+");
		butM4.setMargin(new Insets(10, 6, 9, 6));
		butM4.setFont(f121);
		butM4.setFocusable(false);
		butM4.setForeground(Color.red);
		butM4.setBackground(new Color(242,243,238));
		ActionPerformedMemoryAdd pressedMemoryAdd = new ActionPerformedMemoryAdd();
		butM4.addActionListener(pressedMemoryAdd);

		jplControl2.add(butM1);
		jplControl2.add(butM2);
		jplControl2.add(butM3);
		jplControl2.add(butM4);
	}


	class ActionPerformedMenuBar implements ActionListener{
		public void actionPerformed(ActionEvent e){

			if(e.getSource() == jmenuitemAbout){
				JDialog dlgAbout = new AboutDialog(Calculator.this, "About Calculator", true);
				dlgAbout.setVisible(true);
			}
			else if(e.getSource() == jmenuitemHelp){
				JDialog dlgHelp = new HelpDialog(Calculator.this, "Calculator - Help", true);
				dlgHelp.setVisible(true);
			}
			else if(e.getSource() == jmenuitemDigit){
			    DecimalFormat df = new DecimalFormat();

				String inputString = getDisplayString();
				if (inputString.contains(",")){
					String removeCommas = getDisplayString().replaceAll(",","");
					setDisplayString(removeCommas);
				}
				else if (!inputString.contains(",")){
					df.setGroupingSize(3);
					BigDecimal bd= new BigDecimal (labelOutPut.getText());

		        	String bytesString = df.format(bd);
					setDisplayString(bytesString);
					setDisplayString(bytesString);
				}

			}

			// Edit Menu Items - Copy and Paste
			if(e.getSource() == jmenuitemCopy){
				storeValueCopyPaste = 0;
				storeValueCopyPaste = Double.parseDouble(labelOutPut.getText());

				jmenuitemPaste.setEnabled(true);
				clearOnNextDigit = true;
			}

			if(e.getSource() == jmenuitemPaste){

			//labelOutPut.setText(Double.toString(storeValueCopyPaste));
			setDisplayString(Double.toString(storeValueCopyPaste));
			clearOnNextDigit = true;
			}
		}

		//starts subclass for menu items
			// Help Menu Item
		class AboutDialog extends JDialog implements ActionListener {
			JButton jbnOk;

			Font f121 = new Font("MS Shell Dlg", 0, 12);

			AboutDialog(JFrame parent, String title, boolean modal){
				super(parent, title, modal);
				setBackground(new Color(242,243,238));

				JPanel p1 = new JPanel(new BorderLayout(0, 0));
				p1.setBorder(new EmptyBorder(0, 0, 0, 0));


				StringBuffer text = new StringBuffer();
				text.append("Java Swing Calculator\n\n");
				text.append("This calculator has been developed for a college course \nassignment and is an open source software.\n\nIts source code is licensed for copy and/or modification.\nSo feel free to adapt it to your own needs.\n\n");
				text.append("Version:              1.0\n");
				text.append("Developer:	Miguel\n");
				text.append("Contact:	miguel@procopio.co.uk\n");
				text.append("______________________________________________\n");
				text.append("Procopio Web Solutions - 0843 289 2986\n");


		      	JLabel calcImg = new JLabel(new ImageIcon("images/calc.png"));
				calcImg.setBorder(new EmptyBorder(0, 15, 0, 0));

		      	JLabel AboutImg = new JLabel(new ImageIcon("images/AboutIMG.png"));

				JTextArea jtAreaAbout = new JTextArea(2, 21);
				jtAreaAbout.setText(text.toString());
				jtAreaAbout.setFont(f121);

				jtAreaAbout.setEditable(false);
				jtAreaAbout.setBackground(null);
				jtAreaAbout.setBorder(new EmptyBorder(15, 15, 0, 0));

				p1.add(AboutImg, BorderLayout.NORTH);
				p1.add(calcImg, BorderLayout.WEST);
				p1.add(jtAreaAbout);

				getContentPane().add(p1, BorderLayout.CENTER);

				JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
				jbnOk = new JButton(" OK ");
				jbnOk.addActionListener(this);


				p2.add(jbnOk);
				getContentPane().add(p2, BorderLayout.SOUTH);


				setLocation(300, 200);
				setResizable(false);

				addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e)
						{
							Window aboutDialogWindow = e.getWindow();
							aboutDialogWindow.dispose();
						}
					}
				);

				pack();
			}
			public void actionPerformed(ActionEvent e)
			{
				if(e.getSource() == jbnOk)	{
					this.dispose();
				}
			}
		}


		// Help Menu Item
		class HelpDialog extends JDialog implements ActionListener {
			JButton jbnOk;

			Font f121 = new Font("MS Shell Dlg", 0, 12);

			HelpDialog(JFrame parent, String title, boolean modal){
				super(parent, title, modal);
				setBackground(new Color(242,243,238));

				JPanel p1 = new JPanel(new BorderLayout(0, 0));
				p1.setBorder(new EmptyBorder(0, 0, 0, 0));


				StringBuffer text = new StringBuffer();
				text.append("Java Swing Calculator Overview\n\n");
				text.append("You can use this calculator to perform any of the standard \noperations for which you would normally use a handheld \ncalculator. \n\nThis calculator performs basic arithmetic, such as addition, \nsubtraction, multiplication and division as well as functions. \n\nButtons definitions:\n\n");

				text.append("/    Divide                                          MC    Memory Clear\n");
				text.append("*    Multiply                                       MR    Memory Remind\n");
				text.append("-    Minus                                          MS    Memory Save\n");
				text.append("+    Addition                                      M+    Memory Add\n\n");

				text.append("sqrt = Square Root                         C       Clear\n");
				text.append("% = Percentage                               CE    Clear Existing\n");
				text.append("1/x - Reciprocal\n");

				text.append("______________________________________________\n");
				text.append("Help Line - 0843 289 2986\n");


		      	JLabel calcImg = new JLabel(new ImageIcon("images/calc.png"));
				calcImg.setBorder(new EmptyBorder(0, 15, 0, 0));

		      	JLabel AboutImg = new JLabel(new ImageIcon("images/AboutIMG.png"));

				JTextArea jtAreaHelp = new JTextArea(2, 21);
				jtAreaHelp.setText(text.toString());
				jtAreaHelp.setFont(f121);

				jtAreaHelp.setEditable(false);
				jtAreaHelp.setBackground(null);
				jtAreaHelp.setBorder(new EmptyBorder(15, 15, 0, 0));

				p1.add(AboutImg, BorderLayout.NORTH);
				p1.add(calcImg, BorderLayout.WEST);
				p1.add(jtAreaHelp);

				getContentPane().add(p1, BorderLayout.CENTER);

				JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
				jbnOk = new JButton(" OK ");
				jbnOk.addActionListener(this);


				p2.add(jbnOk);
				getContentPane().add(p2, BorderLayout.SOUTH);


				setLocation(300, 100);
				setResizable(false);

				addWindowListener(new WindowAdapter() {
						public void windowClosing(WindowEvent e)
						{
							Window aboutDialogWindow = e.getWindow();
							aboutDialogWindow.dispose();
						}
					}
				);

				pack();
			}

			public void actionPerformed(ActionEvent e)
			{
				if(e.getSource() == jbnOk)	{
					this.dispose();
				}
			}
		}
	}

	//numbers
	class ActionPerformedNumbers implements ActionListener{
		public void actionPerformed(ActionEvent e){

			for (int i=0; i<NumberedButtons.length; i++){
				if(e.getSource() == NumberedButtons[i]){
					addDigitToDisplay(i);
				}
			}
		}
	}

	// +/-
	class ActionPerformedPlusSign implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but10)
				{
					processSignChange();
				}
		}
	}

	// decimal point
	class ActionPerformedDecimalPoint implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;


				if(e.getSource() == but11)
				{
					addDecimalPoint();
				}


		}

	}

	// =
	class ActionPerformedEquals implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but12)
				{
					processEquals();
				}
		}
	}

	// divide
	class ActionPerformedDevide implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but13)
				{
					processOperator("/");
				}
		}
	}

	// *
	class ActionPerformedMultiply implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but14)
				{
					processOperator("*");
				}
		}
	}

	// -
	class ActionPerformedMinus implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but15)
				{
					processOperator("-");
				}
		}
	}

	// +
	class ActionPerformedPlus implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but16)
				{
					processOperator("+");
				}
		}
	}

	// sqrt
	class ActionPerformedSqrt implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but17)
				{
					if (displayMode != ERROR_MODE)
						{
							try
							{
								if (getDisplayString().indexOf("-") == 0)
									displayError("Invalid input for function!");

								result = Math.sqrt(getNumberInDisplay());
								displayResult(result);
							}

							catch(Exception ex)
							{
								displayError("Invalid input for function!");
								displayMode = ERROR_MODE;
							}
						}
				}
		}
	}

	// 1/x
	class ActionPerformedReciprocal implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but18)
				{
						if (displayMode != ERROR_MODE){
							try
							{
								if (getNumberInDisplay() == 0)
									displayError("Cannot divide by zero!");

								result = 1 / getNumberInDisplay();
								displayResult(result);
							}

							catch(Exception ex)	{
								displayError("Cannot divide by zero!");
								displayMode = ERROR_MODE;
							}
						}
				}
		}
	}

	// %
	class ActionPerformedPercentage implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but19)
				{
						if (displayMode != ERROR_MODE){
							try	{
								result = getNumberInDisplay() / 100;
								displayResult(result);
							}

							catch(Exception ex)	{
								displayError("Invalid input for function!");
								displayMode = ERROR_MODE;
							}
						}

				}
		}
	}

	// backspace
	class ActionPerformedBackspace implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but20)
				{
						if (displayMode != ERROR_MODE){
							setDisplayString(getDisplayString().substring(0,
										getDisplayString().length() - 1));

							if (getDisplayString().length() < 1)
								setDisplayString("0");
						}

				}
		}
	}

	// CE
	class ActionPerformedClearExisting implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but21)
				{
						clearExisting();
				}
		}
	}

	// C
	class ActionPerformedClearAll implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == but22)
				{
						clearAll();

				}
		}
	}

	// MC
	class ActionPerformedMemoryClear implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

                if(e.getSource() == butM1) {
                        m1 = 0;
						memoryTextfield.setText("");
						labelOutPut.getText();
                }

		}
	}

	// MR
	class ActionPerformedMemoryRemind implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == butM2) {
                        labelOutPut.setText("");
                        labelOutPut.setText(labelOutPut.getText() + m1);
                		labelOutPut.getText();
						clearOnNextDigit = true;
                }

		}
	}

	// MS
	class ActionPerformedMemorySave implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == butM3) {
						m1 = Double.parseDouble(labelOutPut.getText());

						labelOutPut.setText("");
						labelOutPut.setText(labelOutPut.getText() + m1);

						memoryTextfield.setText("M");

						labelOutPut.getText();
						clearOnNextDigit = true;
                }
		}
	}

	// M+
	class ActionPerformedMemoryAdd implements ActionListener{
		public void actionPerformed(ActionEvent e){
				double result = 0;

				if(e.getSource() == butM4) {
                        if (k == 1) {
                                m1 = Double.parseDouble(labelOutPut.getText());
                                k++;
                                memoryTextfield.setText("M");
                        } else {
                                m1 += Double.parseDouble(labelOutPut.getText());
                                labelOutPut.setText("" + m1);

                        }
                }
		}
	}



	void setDisplayString(String s){
		labelOutPut.setText(s);
	}

	String getDisplayString (){
		return labelOutPut.getText();
	}

	void addDigitToDisplay(int digit){
		if (clearOnNextDigit)
			setDisplayString("");

		String inputString = getDisplayString();

		if (inputString.indexOf("0") == 0){
			inputString = inputString.substring(1);
		}

		if ((!inputString.equals("0") || digit > 0)  && inputString.length() < MAX_INPUT_LENGTH){
			setDisplayString(inputString + digit);
		}


		displayMode = INPUT_MODE;
		clearOnNextDigit = false;
	}

	void addDecimalPoint(){
		displayMode = INPUT_MODE;

		if (clearOnNextDigit)
			setDisplayString("");

		String inputString = getDisplayString();

		// If the input string already contains a decimal point, don't
		//  do anything to it.
		if (inputString.indexOf(".") < 0)
			{
				setDisplayString(new String(inputString + "."));
				clearOnNextDigit = false;
			}
	}

	void processSignChange(){
		if (displayMode == INPUT_MODE)
		{
			String input = getDisplayString();

			if (input.length() > 0 && !input.equals("0"))
			{
				if (input.indexOf("-") == 0)
					setDisplayString(input.substring(1));

				else
					setDisplayString("-" + input);
			}

		}

		else if (displayMode == RESULT_MODE)
		{
			double numberInDisplay = getNumberInDisplay();

			if (numberInDisplay != 0)
				displayResult(-numberInDisplay);
		}
	}


	void clearAll()	{
		setDisplayString("0");
		lastOperator = "0";
		lastNumber = 0;
		displayMode = INPUT_MODE;
		clearOnNextDigit = true;
	}

	void clearExisting(){
		setDisplayString("0");
		clearOnNextDigit = true;
		displayMode = INPUT_MODE;
	}



	double getNumberInDisplay()	{
		String input = labelOutPut.getText();
		return Double.parseDouble(input);
	}

	void processOperator(String op) {
		if (displayMode != ERROR_MODE)
		{
			double numberInDisplay = getNumberInDisplay();

			if (!lastOperator.equals("0"))
			{
				try
				{
					double result = processLastOperator();
					displayResult(result);
					lastNumber = result;
				}

				catch (DivideByZeroException e)
				{
				}
			}

			else
			{
				lastNumber = numberInDisplay;
			}

			clearOnNextDigit = true;
			lastOperator = op;
		}
	}






	void displayResult(double result){
		setDisplayString(Double.toString(result));
		lastNumber = result;
		displayMode = RESULT_MODE;
		clearOnNextDigit = true;
	}



	void processEquals(){
		double result = 0;

		if (displayMode != ERROR_MODE){
			try
			{
				result = processLastOperator();
				displayResult(result);
			}

			catch (DivideByZeroException e)	{
				displayError("Cannot divide by zero!");
			}

			lastOperator = "0";
		}
	}

	double processLastOperator() throws DivideByZeroException {
		double result = 0;
		double numberInDisplay = getNumberInDisplay();

		if (lastOperator.equals("/"))
		{
			if (numberInDisplay == 0)
				throw (new DivideByZeroException());

			result = lastNumber / numberInDisplay;
		}

		if (lastOperator.equals("*"))
			result = lastNumber * numberInDisplay;

		if (lastOperator.equals("-"))
			result = lastNumber - numberInDisplay;

		if (lastOperator.equals("+"))
			result = lastNumber + numberInDisplay;

		return result;
	}


	void displayError(String errorMessage){
		setDisplayString(errorMessage);
		lastNumber = 0;
		displayMode = ERROR_MODE;
		clearOnNextDigit = true;
	}




	  public static void setNativeLookAndFeel() {
	    try {
	      UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
	    } catch(Exception e) {
	      System.out.println("Error setting native LAF: " + e);
	    }
	  }

	public static void main(String Args[]){
		//notice that the method setNativeLookAndFeel() is called before an instance of Calculator is created.
		//setNativeLookAndFeel method is called so the look of calculator is adapted to the platform that is executed on.
		setNativeLookAndFeel();

		Calculator calc = new Calculator();
		calc.setTitle("Calculator");
		calc.setBackground(new Color(236,233,216));
		calc.setIconImage(new ImageIcon("images/calc.png").getImage());
		calc.pack();
		calc.setLocation(400, 250);
		calc.setVisible(true);
		calc.setSize(270,265);
		calc.setResizable(false);
		calc.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		calc.clearAll();
	}
}


class DivideByZeroException extends Exception{
	public DivideByZeroException()
	{
		super();
	}

	public DivideByZeroException(String s)
	{
		super(s);
	}
}