Java / Calculator GUI / GUICalculator.java
GUICalculator.java
Raw
/**
 * Program Name: GUICalculator.java
 *       Author: Essam Fahmy
 *         Date: August 8, 2022
 *      Purpose: This class uses JFrame to build a GUI for a calculator which uses the calculations class to calculate inputs.
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;



public class GUICalculator extends JFrame {

	
	//Creating required variables
	
	private JFrame frame;
	private Calculator calculation;
	private JPanel buttonPanel;
	private JTextField textField;
	private JButton button1, button2, button3, button4, button5, button6, button7, button8, button9, button0, addButton,
			subtractButton, togglePMButton, percentButton, multiplyButton, clearButton, backButton, sqrtButton,
			divideButton, calcButton, decimalButton, squaredButton, extraButton1, extraButton2;
	private Font font1 = new Font("SansSerif", Font.PLAIN, 22);

	
	/**
	 * 	Method Name:  GUICalculator
	 *   	 Purpose:  Creates the calculator GUI and its Object
	 *       Accepts:  nothing
	 *       Returns:  nothing
	 */
	
	public GUICalculator() {

		super("Calculator");

		frame = new JFrame();

		//The usual code to create frame as needed
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());
		this.setSize(300, 365);
		this.setLocationRelativeTo(null);
		this.setResizable(false);

		//Creating Calculator Object
		
		calculation = new Calculator();
		textField = new JTextField("0.0");

		//Creating Textfield that will display the numbers
		
		textField.setHorizontalAlignment(SwingConstants.RIGHT);
		textField.setEditable(false);
		textField.setFont(font1);
		textField.setPreferredSize(new Dimension(285, 50));
		textField.setBorder(BorderFactory.createLineBorder(Color.black));

		//Calling and adding everything to the frame
		
		this.add(textField);
		buttonsPanelBuild();
		this.add(buttonPanel);
		menuBuild();

		//Displays Calculator GUI
		
		this.setVisible(true);
	}

	
	/**
	 *   Method Name:  menuBuild
	 *       Purpose:  Creates the meanu area of the calculator
	 *       Accepts:  nothing
	 *       Returns:  nothing
	 */
	
	private void menuBuild() {
		
		//Creating Variables
		
		ButtonClick detector = new ButtonClick();
		JMenuBar menuBar;
		JMenu file, convert, help;
		JMenuItem bin, hex, exit, dec, oct, howTo, about;

		//Creating menu bar and menus
		
		menuBar = new JMenuBar();
		file = new JMenu("File");
		convert = new JMenu("Convert");
		help = new JMenu("Help");

		//Creating Menu items
		
		exit = new JMenuItem("Exit");
		hex = new JMenuItem("Hex");
		dec = new JMenuItem("Dec");
		oct = new JMenuItem("Oct");
		bin = new JMenuItem("Bin");
		howTo = new JMenuItem("How To Use");
		about = new JMenuItem("About");
		
		//Adding listeners

		exit.addActionListener(detector);
		hex.addActionListener(detector);
		dec.addActionListener(detector);
		oct.addActionListener(detector);
		bin.addActionListener(detector);
		howTo.addActionListener(detector);
		about.addActionListener(detector);

		//Adding the menu items to the menu and adding the menus to the menu bar
		
		file.add(exit);
		convert.add(hex);
		convert.add(dec);
		convert.add(oct);
		convert.add(bin);
		help.add(howTo);
		help.add(about);
		menuBar.add(file);
		menuBar.add(convert);
		menuBar.add(help);

		this.setJMenuBar(menuBar);
	}

	/**
	 *   Method Name:  buttonsPanelBuild
	 *       Purpose:  Creates the buttons for the calculator
	 *       Accepts:  nothing
	 *       Returns:  nothing
	 */
	
	private void buttonsPanelBuild() {
		
		//Creating Variables
		
		ButtonClick detector = new ButtonClick();

		Dimension size = new Dimension(67, 37); // 70, 40

		ImageIcon buttonImg = new ImageIcon("back.png");

		//Creating buttons
		
		button1 = new JButton("1");
		button2 = new JButton("2");
		button3 = new JButton("3");
		button4 = new JButton("4");
		button5 = new JButton("5");
		button6 = new JButton("6");
		button7 = new JButton("7");
		button8 = new JButton("8");
		button9 = new JButton("9");
		button0 = new JButton("0");
		addButton = new JButton("+");
		subtractButton = new JButton("-");
		togglePMButton = new JButton("+/-");
		percentButton = new JButton("%");
		multiplyButton = new JButton("X");
		clearButton = new JButton("C");
		backButton = new JButton();
		sqrtButton = new JButton();
		divideButton = new JButton("/");
		calcButton = new JButton("=");
		decimalButton = new JButton(".");
		squaredButton = new JButton();
		extraButton1 = new JButton();
		extraButton2 = new JButton();

		backButton.setActionCommand("back");
		squaredButton.setActionCommand("squared");
		sqrtButton.setActionCommand("sqrt");

		//Setting icons
		
		backButton.setIcon(buttonImg);
		squaredButton.setIcon(new ImageIcon("squared.png"));
		sqrtButton.setIcon(new ImageIcon("squareroot.png"));

		//Setting button size
		
		button1.setPreferredSize(size);
		button2.setPreferredSize(size);
		button3.setPreferredSize(size);
		button4.setPreferredSize(size);
		button5.setPreferredSize(size);
		button6.setPreferredSize(size);
		button7.setPreferredSize(size);
		button8.setPreferredSize(size);
		button9.setPreferredSize(size);
		button0.setPreferredSize(size);
		addButton.setPreferredSize(size);
		subtractButton.setPreferredSize(size);
		togglePMButton.setPreferredSize(size);
		percentButton.setPreferredSize(size);
		multiplyButton.setPreferredSize(size);
		clearButton.setPreferredSize(size);
		backButton.setPreferredSize(size);
		sqrtButton.setPreferredSize(size);
		divideButton.setPreferredSize(size);
		calcButton.setPreferredSize(size);
		decimalButton.setPreferredSize(size);
		squaredButton.setPreferredSize(size);
		extraButton1.setPreferredSize(size);
		extraButton2.setPreferredSize(size);

		//Setting button font
		
		button1.setFont(font1);
		button2.setFont(font1);
		button3.setFont(font1);
		button4.setFont(font1);
		button5.setFont(font1);
		button6.setFont(font1);
		button7.setFont(font1);
		button8.setFont(font1);
		button9.setFont(font1);
		button0.setFont(font1);
		addButton.setFont(font1);
		subtractButton.setFont(font1);
		togglePMButton.setFont(font1);
		percentButton.setFont(font1);
		multiplyButton.setFont(font1);
		clearButton.setFont(font1);
		backButton.setFont(font1);
		sqrtButton.setFont(font1);
		divideButton.setFont(font1);
		calcButton.setFont(font1);
		decimalButton.setFont(font1);
		squaredButton.setFont(font1);
		extraButton1.setFont(font1);
		extraButton2.setFont(font1);

		//Setting button border
		
		button1.setBorder(BorderFactory.createLineBorder(Color.black));
		button2.setBorder(BorderFactory.createLineBorder(Color.black));
		button3.setBorder(BorderFactory.createLineBorder(Color.black));
		button4.setBorder(BorderFactory.createLineBorder(Color.black));
		button5.setBorder(BorderFactory.createLineBorder(Color.black));
		button6.setBorder(BorderFactory.createLineBorder(Color.black));
		button7.setBorder(BorderFactory.createLineBorder(Color.black));
		button8.setBorder(BorderFactory.createLineBorder(Color.black));
		button9.setBorder(BorderFactory.createLineBorder(Color.black));
		button0.setBorder(BorderFactory.createLineBorder(Color.black));
		addButton.setBorder(BorderFactory.createLineBorder(Color.black));
		subtractButton.setBorder(BorderFactory.createLineBorder(Color.black));
		togglePMButton.setBorder(BorderFactory.createLineBorder(Color.black));
		percentButton.setBorder(BorderFactory.createLineBorder(Color.black));
		multiplyButton.setBorder(BorderFactory.createLineBorder(Color.black));
		clearButton.setBorder(BorderFactory.createLineBorder(Color.black));
		backButton.setBorder(BorderFactory.createLineBorder(Color.black));
		sqrtButton.setBorder(BorderFactory.createLineBorder(Color.black));
		divideButton.setBorder(BorderFactory.createLineBorder(Color.black));
		calcButton.setBorder(BorderFactory.createLineBorder(Color.black));
		decimalButton.setBorder(BorderFactory.createLineBorder(Color.black));
		squaredButton.setBorder(BorderFactory.createLineBorder(Color.black));
		extraButton1.setBorder(BorderFactory.createLineBorder(Color.black));
		extraButton2.setBorder(BorderFactory.createLineBorder(Color.black));

		//Adding button listeners
		
		button1.addActionListener(detector);
		button2.addActionListener(detector);
		button3.addActionListener(detector);
		button4.addActionListener(detector);
		button5.addActionListener(detector);
		button6.addActionListener(detector);
		button7.addActionListener(detector);
		button8.addActionListener(detector);
		button9.addActionListener(detector);
		button0.addActionListener(detector);
		addButton.addActionListener(detector);
		subtractButton.addActionListener(detector);
		togglePMButton.addActionListener(detector);
		percentButton.addActionListener(detector);
		multiplyButton.addActionListener(detector);
		clearButton.addActionListener(detector);
		backButton.addActionListener(detector);
		sqrtButton.addActionListener(detector);
		divideButton.addActionListener(detector);
		calcButton.addActionListener(detector);
		decimalButton.addActionListener(detector);
		squaredButton.addActionListener(detector);
		extraButton1.addActionListener(detector);
		extraButton2.addActionListener(detector);

		//Creating the button panel and adding all the buttons
		
		buttonPanel = new JPanel();

		buttonPanel.setLayout(new GridLayout(6, 4, 4, 4));

		buttonPanel.add(clearButton);
		buttonPanel.add(backButton);
		buttonPanel.add(percentButton);
		buttonPanel.add(togglePMButton);
		buttonPanel.add(squaredButton);
		buttonPanel.add(sqrtButton);
		buttonPanel.add(extraButton1);
		buttonPanel.add(divideButton);
		buttonPanel.add(button7);
		buttonPanel.add(button8);
		buttonPanel.add(button9);
		buttonPanel.add(multiplyButton);
		buttonPanel.add(button4);
		buttonPanel.add(button5);
		buttonPanel.add(button6);
		buttonPanel.add(subtractButton);
		buttonPanel.add(button1);
		buttonPanel.add(button2);
		buttonPanel.add(button3);
		buttonPanel.add(addButton);
		buttonPanel.add(extraButton2);
		buttonPanel.add(button0);
		buttonPanel.add(decimalButton);
		buttonPanel.add(calcButton);

	}
	
	/**
	 *   Method Name:  ButtonClick
	 *       Purpose:  Listens to button clicks and calls the correct methods to handle them
	 *       Accepts:  nothing
	 *       Returns:  nothing
	 */

	private class ButtonClick implements ActionListener {

		//Creating variables
		
		boolean operatorOn = false;
		boolean tracker = false;

		@Override
		public void actionPerformed(ActionEvent ev) {
			
			//The 1 button
			
			if (ev.getActionCommand().equals("1")) {
				try {
					calculation.buildOperand("1");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
			} 
			
			//The 2 button
			
			else if (ev.getActionCommand().equals("2")) {
				try {
					calculation.buildOperand("2");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
				
			//The 3 button

			} else if (ev.getActionCommand().equals("3")) {
				try {
					calculation.buildOperand("3");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
				
			//The 4 button
				
			} else if (ev.getActionCommand().equals("4")) {
				try {
					calculation.buildOperand("4");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
				
			//The 5 button
				
			} else if (ev.getActionCommand().equals("5")) {
				try {
					calculation.buildOperand("5");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
			} 
			
			//The 6 button
			
			else if (ev.getActionCommand().equals("6")) {
				try {
					calculation.buildOperand("6");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
				
			//The 7 button	
				
			} else if (ev.getActionCommand().equals("7")) {
				try {
					calculation.buildOperand("7");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
				
			//The 8 button
				
			} else if (ev.getActionCommand().equals("8")) {
				try {
					calculation.buildOperand("8");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
				
			//The 9 button
				
			} else if (ev.getActionCommand().equals("9")) {
				try {
					calculation.buildOperand("9");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
				
			//The 0 button
				
			} else if (ev.getActionCommand().equals("0")) {
				try {
					calculation.buildOperand("0");
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
					calculation.setOperand("");
					textField.setText("0.0");
				}
			
			//The toggle +/- button
				
			} else if (ev.getActionCommand().equals("+/-")) {
				if (tracker == false) {
					tracker = true;

					try {
						calculation.togglePlusMinus(true);
					} catch (EmptyOperandException e) {

						tracker = false;
						JOptionPane.showMessageDialog(frame, e.getMessage());
					}
				} else if (tracker == true) {
					tracker = false;
					try {
						calculation.togglePlusMinus(false);
					} catch (EmptyOperandException e) {

						tracker = true;
						JOptionPane.showMessageDialog(frame, e.getMessage());
					}

				}
				textField.setText(calculation.getOperand());
				
			//Division Button
				
			} else if (ev.getActionCommand().equals("/")) {
				if (operatorOn == false) {
					calculation.setOperator("/");

					try {
						calculation.buildExpression("/");
						operatorOn = true;
					} catch (EmptyOperandException e) {
						JOptionPane.showMessageDialog(frame, e.getMessage());
					}
					textField.setText(calculation.getOperand());
				}
				
			//Multiplication Button
				
			} else if (ev.getActionCommand().equals("X")) {
				if (operatorOn == false) {
					calculation.setOperator("*");
					try {
						calculation.buildExpression("*");
						operatorOn = true;
					} catch (EmptyOperandException e) {
						JOptionPane.showMessageDialog(frame, e.getMessage());
					}
					textField.setText(calculation.getOperand());

				}
			} 
			
			//Subtraction button
			
			else if (ev.getActionCommand().equals("-")) {
				if (operatorOn == false) {
					calculation.setOperator("-");

					try {
						calculation.buildExpression("-");
						operatorOn = true;
					} catch (EmptyOperandException e) {
						JOptionPane.showMessageDialog(frame, e.getMessage());
					}
					textField.setText(calculation.getOperand());
				}
			} 
			
			//Addition button
			
			else if (ev.getActionCommand().equals("+")) {
				if (operatorOn == false) {
					calculation.setOperator("+");

					try {
						calculation.buildExpression("+");
						operatorOn = true;
					} catch (EmptyOperandException e) {
						JOptionPane.showMessageDialog(frame, e.getMessage());
					}
					textField.setText(calculation.getOperand());
				}
			}
			
			//Percentage Button
			
			else if (ev.getActionCommand().equals("%")) {
				try {
					calculation.setOperand(calculation.findPercentage(calculation.getOperand()));
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (EmptyOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			} 
			
			//Back button
			
			else if (ev.getActionCommand().equals("back")) {
				try {
					calculation.backspace(calculation.getOperand());
					if (calculation.getOperand() == "") {
						textField.setText("0.0");
					} else {
						textField.setText(calculation.getOperand());
					}
				} catch (EmptyOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			} 
			
			//Squared button
			
			else if (ev.getActionCommand().equals("squared")) {
				try {
					calculation.setOperand(calculation.findSquared(calculation.getOperand()));
					textField.setText(calculation.getOperand());
					operatorOn = false;

				} catch (EmptyOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			} 
			
			//Squareroot Button
			
			else if (ev.getActionCommand().equals("sqrt")) {
				try {
					calculation.setOperand(calculation.findSquareRoot(calculation.getOperand()));
					textField.setText(calculation.getOperand());
					operatorOn = false;
				} catch (EmptyOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			} 
			
			//Decimal Button
			
			else if (ev.getActionCommand().equals(".")) {
				if (calculation.getDecimalPressed() == false) {
					try {
						calculation.buildOperand(".");
						calculation.setDecimalPressed(true);
						operatorOn = false;
					} catch (LongOperandException e) {

						JOptionPane.showMessageDialog(frame, e.getMessage());
						calculation.setOperand("");
						textField.setText("0.0");
					}
				} 	else if (calculation.getDecimalPressed() == true) {
					JOptionPane.showMessageDialog(frame, "There is already a decimal in place.");
				}
				textField.setText(calculation.getOperand());
			} 
			
			//Clear button
			
			else if (ev.getActionCommand().equals("C")) {
				calculation.clear();
				textField.setText("0.0");
			} 
			
			//Calculate Button
			
			else if (ev.getActionCommand().equals("=")) {
				try {
					textField.setText(Double.toString(calculation.calculate()));
					operatorOn = false;
				} catch (ArithmeticException | EmptyOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}

			}
			
			//Exit button

			else if (ev.getActionCommand().equals("Exit")) {
				System.exit(0);
			}

			//How to use button
			
			else if (ev.getActionCommand().equals("How To Use")) {
				JOptionPane.showMessageDialog(frame, "The calculator is pretty simple to use. Here are a few tips:\n"
						+ "\n1. Just plug in your operand and select the operator you would like to use and then plug in a second number and so on."
						+ "\n2.You can also change your current screen value to hexadecimal, binary, octal, and back to decimal through the convert button in the menu."
						+ "\n3.You can also find the square, root, and percentage of the value on screen."
						+ "\n4.Feel free to also use the clear and erase buttons as well as the toggle +/- as needed when calculating");
			}

			//About button
			
			else if (ev.getActionCommand().equals("About")) {
				JOptionPane.showMessageDialog(frame, calculation.toString());
			}

			//Hexadecimal, Octal, Decimal, and Binary Buttons
			
			else if (ev.getActionCommand().equals("Hex")) {
				try {
					textField.setText(calculation.convertHex(calculation.getOperand()));
				} catch (EmptyOperandException | LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			} else if (ev.getActionCommand().equals("Oct")) {
				try {
					textField.setText(calculation.convertOct(calculation.getOperand()));
				} catch (EmptyOperandException | LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			} else if (ev.getActionCommand().equals("Dec")) {
				try {
					textField.setText(calculation.convertDec(calculation.getOperand()));
				} catch (EmptyOperandException | LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			} else if (ev.getActionCommand().equals("Bin")) {
				try {
					textField.setText(calculation.convertBin(calculation.getOperand()));
				} catch (EmptyOperandException | LongOperandException e) {
					JOptionPane.showMessageDialog(frame, e.getMessage());
				}
			}
		}
	}

	//Main Method
	
	public static void main(String args[]) {
		new GUICalculator();
	}

}