/* Name: Chris Johnson johnson@dcs.gla.ac.uk Last modified: 05/01/2000 Description: Part of a simple calculator applet based on a program in G.W. Rouse's An Introduction to Data Structures and Algorithms with Java, Prentice Hall, Hemel Hemstead, 1998. pp 65-78. I've updated it to AWT 1.1 the original used 1.0 */ package uk.ac.gla.dcs.johnson; import java.applet.Applet; import java.awt.*; import java.awt.event.*; //import NewGridBagLayout; public class Calculator extends Applet implements ActionListener{ private long operand1, operand2, answer; private String operation; private boolean newNumber; private Button button1 = new Button("1"); private Button button2 = new Button("2"); private Button button3 = new Button("3"); private Button button4 = new Button("4"); private Button button5 = new Button("5"); private Button button6 = new Button("6"); private Button button7 = new Button("7"); private Button button8 = new Button("8"); private Button button9 = new Button("9"); private Button button0 = new Button("0"); private TextField numberBox = new TextField("0", 12); private Button plusMinusButton = new Button("+/-"); private Button CEButton = new Button("CE"); private Button CButton = new Button("C"); private Button plusButton = new Button("+"); private Button minusButton = new Button("-"); private Button timesButton = new Button("*"); private Button divideButton = new Button("/"); private Button equalsButton = new Button("="); public static void main(String[] args) { Frame frame = new Frame(); Calculator applet = new Calculator(); frame.setSize(200, 300); frame.add(applet); applet.init(); frame.show(); applet.start(); } public void init() { Panel numberPanel = new Panel(); Panel signClearPanel = new Panel(); Panel arithmeticPanel = new Panel(); Panel masterPanel = new Panel(); NewGridBagLayout numberLayout = new NewGridBagLayout(); NewGridBagLayout masterLayout = new NewGridBagLayout(); operand1 = operand2 = answer = 0; operation = "+"; setFont(new Font("Helvetica", Font.BOLD, 14)); signClearPanel.setLayout(new GridLayout(1,3,2,2)); signClearPanel.add(plusMinusButton); signClearPanel.add(CEButton); signClearPanel.add(CButton); numberPanel.setLayout(numberLayout); numberLayout.setRareConstraints( GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(2,2,2,2), 0, 0, 1, 1); numberLayout.setPositionSizeAdd(button1, numberPanel, 0, 0, 1, 1); numberLayout.setPositionSizeAdd(button2, numberPanel, 1, 0, 1, 1); numberLayout.setPositionSizeAdd(button3, numberPanel, 2, 0, 1, 1); numberLayout.setPositionSizeAdd(button4, numberPanel, 0, 1, 1, 1); numberLayout.setPositionSizeAdd(button5, numberPanel, 1, 1, 1, 1); numberLayout.setPositionSizeAdd(button6, numberPanel, 2, 1, 1, 1); numberLayout.setPositionSizeAdd(button7, numberPanel, 0, 2, 1, 1); numberLayout.setPositionSizeAdd(button8, numberPanel, 1, 2, 1, 1); numberLayout.setPositionSizeAdd(button9, numberPanel, 2, 2, 1, 1); numberLayout.setPositionSizeAdd(button0, numberPanel, 1, 3, 1, 1); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); button0.addActionListener(this); arithmeticPanel.setLayout(new GridLayout(5,1,2,2)); arithmeticPanel.add(plusButton); arithmeticPanel.add(minusButton); arithmeticPanel.add(divideButton); arithmeticPanel.add(timesButton); arithmeticPanel.add(equalsButton); plusButton.addActionListener(this); minusButton.addActionListener(this); divideButton.addActionListener(this); timesButton.addActionListener(this); equalsButton.addActionListener(this); masterLayout.setRareConstraints( GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(2,2,2,2), 0, 0, 1, 1); masterPanel.setLayout(masterLayout); masterLayout.setPositionSizeAdd(numberBox, masterPanel, 0, 0, 2, 1); masterLayout.setPositionSizeAdd(signClearPanel, masterPanel, 0, 1, 2, 1); masterLayout.setPositionSizeAdd(numberPanel, masterPanel, 0, 2, 1, 1); masterLayout.setPositionSizeAdd(arithmeticPanel, masterPanel, 1, 2, 1, 1); add(masterPanel); } public void actionPerformed(ActionEvent evt) { String arg = evt.getActionCommand(); if (arg.equals("1")) addDigitToDisplay(1); else if(arg.equals("2")) addDigitToDisplay(2); else if(arg.equals("3")) addDigitToDisplay(3); else if(arg.equals("4")) addDigitToDisplay(4); else if(arg.equals("5")) addDigitToDisplay(5); else if(arg.equals("6")) addDigitToDisplay(6); else if(arg.equals("7")) addDigitToDisplay(7); else if(arg.equals("8")) addDigitToDisplay(8); else if(arg.equals("9")) addDigitToDisplay(9); else if(arg.equals("0")) addDigitToDisplay(0); else if(arg.equals("CE")) clearDisplay(); else if(arg.equals("+/-")) changeSign(); else if(arg.equals("C")) clearAll(); else if(arg.equals("=")) doEquals(); else if(arg.equals("+")) doOperation("+"); else if(arg.equals("-")) doOperation("-"); else if(arg.equals("*")) doOperation("*"); else if(arg.equals("/")) doOperation("/"); } private void addDigitToDisplay(int newDigit) { if(newNumber){ numberBox.setText("0"); newNumber = false; } long displayValue = Long.parseLong(numberBox.getText()); if (displayValue >= 0 && displayValue <=999999999) displayValue = displayValue*10 + newDigit; numberBox.setText(Long.toString(displayValue)); } private void changeSign() { long display = Long.parseLong(numberBox.getText()); display = -display; numberBox.setText(Long.toString(display)); } private void clearDisplay() { numberBox.setText("0"); } private void clearAll() { operand1 = operand2 = answer = 0; operation = "+"; newNumber = true; numberBox.setText("0"); } private void doEquals() { if(newNumber) return; operand2 = Long.parseLong(numberBox.getText()); if (operation == "+") answer = operand1 + operand2; else if (operation == "-") answer = operand1 - operand2; else if (operation == "*") answer = operand1 * operand2; else if (operation == "/") answer = operand1 / operand2; numberBox.setText(Long.toString(answer)); operand1 = operand2 = answer = 0; operation = "+"; } private void doOperation(String newOperation) { if(newNumber) return; operand2 = Long.parseLong(numberBox.getText()); if (operation=="+") answer = operand1 + operand2; else if (operation=="-") answer = operand1 - operand2; else if (operation=="*") answer = operand1 * operand2; else if (operation=="/") answer = operand1 / operand2; numberBox.setText(Long.toString(answer)); operand1 = answer; operand2 = answer = 0; operation = newOperation; newNumber = true; } }