import java.util.StringTokenizer; import java.util.Hashtable; import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Dimension; import java.awt.Event; import java.awt.GridLayout; import java.awt.Label; import java.awt.Panel; import java.awt.Frame; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowEvent; import java.awt.event.WindowAdapter; import java.applet.Applet; // Converts the Calculator application to run either as an application // or as an applet by creating a "shared" panel -- a panel that will // be shared by the application's Frame and by the Applet. The panel // will be the first component placed on each. All other components are // placed on the shared panel. public class CalculatorE extends Applet { private boolean isApplet = false; private boolean lastKeyWasFunction = true; private double displayHold = 0; private Frame f; private Hashtable theKeys = new Hashtable(); private Label display; private Panel pMain; private String lastFunction = ""; private Toolkit tk = null; private KeyListener keyListener = new KeyListener(); public static void main(String[] args) { CalculatorE calc = new CalculatorE(); calc.isApplet = false; calc.tk = Toolkit.getDefaultToolkit(); calc.f = new Frame("Calculator"); // add Window closer calc.f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } // close anon inner class }); // add Key Listener to frame calc.f.addKeyListener(calc.keyListener); // create panel which will be shared by Frame and Applet calc.pMain = new Panel(); calc.pMain.setLayout(new BorderLayout()); calc.pMain.setBackground(Color.lightGray); calc.pMain.addKeyListener(calc.keyListener); // add shared panel to Frame calc.f.add(calc.pMain, BorderLayout.CENTER); calc.makeGUI(); } public void init() { isApplet = true; setLayout(new BorderLayout()); setBackground(Color.lightGray); this.addKeyListener(keyListener); // create the shared panel pMain = new Panel(); pMain.setLayout(new BorderLayout()); pMain.setBackground(Color.lightGray); pMain.addKeyListener(keyListener); // add the shared panel to the applet add(pMain, BorderLayout.CENTER); makeGUI(); } private void makeGUI() { // add display display = new Label("0", Label.RIGHT); pMain.add(display, BorderLayout.NORTH); Panel numPanel = new Panel(); numPanel.setBackground(pMain.getBackground()); numPanel.addKeyListener(keyListener); numPanel.setLayout(new GridLayout(4, 3)); Panel funcPanel = new Panel(); funcPanel.setBackground(numPanel.getBackground()); funcPanel.addKeyListener(keyListener); funcPanel.setLayout(new GridLayout(4, 2)); // create number buttons if (true) { ButtonListener bl = new ButtonListener(0); for (int i = 0; i < 12; i++) { Button b = new Button("" + i); b.setForeground(Color.blue); b.addActionListener(bl); b.addKeyListener(keyListener); // handle blank and '.' buttons if (i == 10) { b.setLabel("+/-"); } if (i == 11) { b.setLabel("."); } theKeys.put(b.getLabel(), b); } } // create function keys if (true) { ButtonListener bl = new ButtonListener(1); StringTokenizer st = new StringTokenizer("/ C * CE + none - =", " "); while (st.hasMoreTokens()) { Button b = new Button(st.nextToken()); b.setForeground(Color.red); b.addActionListener(bl); b.addKeyListener(keyListener); // disable unneded 'b' key if (b.getLabel().equals("none")) { b.setVisible(false); } // add the key to theKeys array theKeys.put(b.getLabel(), b); // add the key to the function panel funcPanel.add(b); } } // place number buttons on numPanel for (int i = 1; i <= 9; i++) { numPanel.add((Button)theKeys.get("" + i)); } numPanel.add((Button)theKeys.get("+/-")); numPanel.add((Button)theKeys.get("0")); numPanel.add((Button)theKeys.get(".")); pMain.add(numPanel, BorderLayout.CENTER); pMain.add(funcPanel, BorderLayout.EAST); if (!isApplet) { f.pack(); Dimension screensize = tk.getScreenSize(); // set size f.setSize(screensize.width / 5, screensize.height / 4); // center form f.setLocation((screensize.width - f.getWidth()) / 2, (screensize.height - f.getHeight()) / 2); f.setResizable(false); } pMain.setVisible(true); setVisible(true); if (isApplet == false) { f.setVisible(true); } } private double getDisplayValue() { double d = 0.0; String s = display.getText(); try { // Double.parseDouble doesn't work in 1.1v applets d = Double.valueOf(s).doubleValue(); } catch (NumberFormatException e) { d = 0.0; } return d; } private void handleFuncKey(Button b) { String lbl = b.getLabel(); if ("=CCE".indexOf(lbl) > -1) { if (lbl.equals("C")) { displayHold = 0d; lastFunction = ""; display.setText("0"); } if (lbl.equals("CE")) { display.setText("0"); lastKeyWasFunction = true; return; } if (lbl.equals("=")) {} } if (lastFunction.equals("+")) { display.setText("" + (displayHold + getDisplayValue())); } if (lastFunction.equals("-")) { display.setText("" + (displayHold - getDisplayValue())); } if (lastFunction.equals("*")) { display.setText("" + (displayHold * getDisplayValue())); } if (lastFunction.equals("/")) { String r; try { r = "" + (displayHold / getDisplayValue()); } catch (ArithmeticException ae) { r = "divison error"; displayHold = 0d; } display.setText(r); } lastKeyWasFunction = true; lastFunction = lbl; displayHold = getDisplayValue(); } private void handleNumKey(Button b) { String lbl = b.getLabel(); if (lastKeyWasFunction) { display.setText(""); } if (lbl.equals(".") || lbl.equals("+/-")) { if (lbl.equals(".")) { if (display.getText().indexOf(".") == -1) { display.setText(display.getText() + "."); } } else { display.setText("" + getDisplayValue() * -1); } } else { display.setText(display.getText() + lbl); } lastKeyWasFunction = false; } private class ButtonListener implements ActionListener { // buttonType == 0 : number key listener // buttonType == 1 : function key listener private int listenerType = 0; ButtonListener(int listenerType) { this.listenerType = listenerType; } public void actionPerformed(ActionEvent e) { Button b = (Button)e.getSource(); if (listenerType == 0) { handleNumKey(b); } else { handleFuncKey(b); } } } private class KeyListener extends KeyAdapter { // better to use Keystroke class, but it's not supported in v1.1 private String lastKey = ""; public void keyPressed(KeyEvent e) { Button b = null; String targ = "NUMPAD"; String key = e.getKeyText(e.getKeyCode()); key = key.toUpperCase(); if (key.indexOf(targ) > -1) { key = key.substring(key.indexOf(targ) + 1 + targ.length()); } // handle SHIFTed keys if (lastKey.equals("SHIFT")) { if (key.equals("8")) { key = "*"; } if (key.equals("=")) { key = "+"; } } if (key.equals("ENTER")) { key = "="; } if (key.equals("MINUS")) { key = "-"; } b = (Button)theKeys.get(key); if (b != null) { if ("0123456789.".indexOf(key) > -1) { handleNumKey(b); } else { handleFuncKey(b); } } lastKey = key; } } }