home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-12-14 | 9.7 KB | 465 lines |
- import java.util.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.applet.*;
-
- /**
- * A simple calculator
- */
- public class Calc extends Applet
- {
-
- Display display = new Display();
-
- /**
- * Initialize the Calc applet
- */
- public void init () {
-
- setLayout(new BorderLayout());
- Keypad keypad = new Keypad();
-
- add ("North", display);
- add ("Center", keypad);
- }
-
- /**
- * This allows the class to be used either as an applet
- * or as a standalone application.
- */
- public static void main (String args[]) {
-
- Frame f = new Frame ("Calculator");
- Calc calc = new Calc ();
-
- calc.init ();
-
- f.setSize (210, 200);
- f.add ("Center", calc);
- f.show ();
- }
-
- class Keypad extends Panel
- implements ActionListener
- {
- Button b7;
- Button b8;
- Button b9;
- Button bDiv;
- Button b4;
- Button b5;
- Button b6;
- Button bMult;
- Button b1;
- Button b2;
- Button b3;
- Button bMin;
- Button bDec;
- Button b0;
- Button bSign;
- Button bPlus;
- Button bC;
- Button bEqu;
-
- /**
- * Initialize the keypad, add buttons, set colors, etc.
- */
- Keypad (){
-
- Font font = new Font ("Times", Font.BOLD, 14);
- Color functionColor = new Color (255, 255, 0);
- Color numberColor = new Color (0, 255, 255);
- Color equalsColor = new Color (0, 255, 0);
- setFont (font);
-
- b7 = new Button ("7");
- add (b7);
- b7.setBackground (numberColor);
- b7.addActionListener(this);
-
- b8 = new Button ("8");
- add (b8);
- b8.setBackground (numberColor);
- b8.addActionListener(this);
-
- b9 = new Button ("9");
- add (b9);
- b9.setBackground (numberColor);
- b9.addActionListener(this);
-
- bDiv = new Button ("/");
- add (bDiv);
- bDiv.setBackground (numberColor);
- bDiv.addActionListener(this);
-
- b4 = new Button ("4");
- add (b4);
- b4.setBackground (numberColor);
- b4.addActionListener(this);
-
- b5 = new Button ("5");
- add (b5);
- b5.setBackground (numberColor);
- b5.addActionListener(this);
-
- b6 = new Button ("6");
- add (b6);
- b6.setBackground (numberColor);
- b6.addActionListener(this);
-
- bMult = new Button ("x");
- add (bMult);
- bMult.setBackground (numberColor);
- bMult.addActionListener(this);
-
- b1 = new Button ("1");
- add (b1);
- b1.setBackground (numberColor);
- b1.addActionListener(this);
-
- b2 = new Button ("2");
- add (b2);
- b2.setBackground (numberColor);
- b2.addActionListener(this);
-
- b3 = new Button ("3");
- add (b3);
- b3.setBackground (numberColor);
- b3.addActionListener(this);
-
- bMin = new Button ("-");
- add (bMin);
- bMin.setBackground (numberColor);
- bMin.addActionListener(this);
-
- bDec = new Button (".");
- add (bDec);
- bDec.setBackground (numberColor);
- bDec.addActionListener(this);
-
- b0 = new Button ("0");
- add (b0);
- b0.setBackground (numberColor);
- b0.addActionListener(this);
-
- bSign = new Button ("+/-");
- add (bSign);
- bSign.setBackground (numberColor);
- bSign.addActionListener(this);
-
- bPlus = new Button ("+");
- add (bPlus);
- bPlus.setBackground (numberColor);
- bPlus.addActionListener(this);
-
- bC = new Button ("C");
- add (bC);
- bC.setBackground (functionColor);
- bC.addActionListener(this);
-
- add (new Label (""));
- add (new Label (""));
-
- bEqu = new Button ("=");
- add (bEqu);
- bEqu.setBackground (equalsColor);
- bEqu.addActionListener(this);
-
- setLayout (new GridLayout (5, 4, 4, 4));
- }
-
- public void actionPerformed(ActionEvent event)
- {
- Object object = event.getSource();
-
- if(object == bC)
- {
- display.Clear ();
- }
-
- if(object == bDec)
- {
- display.Dot ();
- }
-
- if(object == bPlus)
- {
- display.Plus ();
- }
-
- if(object == bMin)
- {
- display.Minus ();
- }
-
- if(object == bMult)
- {
- display.Mul ();
- }
-
- if(object == bDiv)
- {
- display.Div ();
- }
-
- if(object == bSign)
- {
- display.Chs ();
- }
-
- if(object == bEqu)
- {
- display.Equals ();
- }
-
- if(object == b0)
- {
- display.Digit ("0");
- }
-
- if(object == b1)
- {
- display.Digit ("1");
- }
-
- if(object == b2)
- {
- display.Digit ("2");
- }
-
- if(object == b3)
- {
- display.Digit ("3");
- }
-
- if(object == b4)
- {
- display.Digit ("4");
- }
-
- if(object == b5)
- {
- display.Digit ("5");
- }
-
- if(object == b6)
- {
- display.Digit ("6");
- }
-
- if(object == b7)
- {
- display.Digit ("7");
- }
-
- if(object == b8)
- {
- display.Digit ("8");
- }
-
- if(object == b9)
- {
- display.Digit ("9");
- }
-
- }
- }
- }
-
- /* -------------------------------------------------- */
-
- /**
- * The Keypad handles the input for the calculator
- * and writes to the display.
- */
-
- /* -------------------------------------------------- */
-
- /**
- * The Display class manages displaying the calculated result
- * as well as implementing the calculator function keys.
- */
- class Display extends Panel{
-
- double last = 0;
- int op = 0;
- boolean equals = false;
- int maxlen = 10;
- String s;
- Label readout = new Label("");
-
- /**
- * Initialize the display
- */
- Display () {
-
- setLayout(new BorderLayout());
- setBackground (Color.red);
- setFont (new Font ("Courier", Font.BOLD + Font.ITALIC, 30));
- readout.setAlignment(1);
- add ("Center",readout);
- repaint();
- Clear ();
- }
-
- /**
- * Handle clicking a digit.
- */
- void Digit (String digit) {
- checkEquals ();
-
- /*
- * Strip leading zeros
- */
- if (s.length () == 1 && s.charAt (0) == '0' && digit.charAt (0) != '.')
- s = s.substring (1);
-
- if (s.length () < maxlen)
- s = s + digit;
- showacc ();
- }
-
- /**
- * Handle a decimal point.
- */
- void Dot () {
- checkEquals ();
-
- /*
- * Already have '.'
- */
- if (s.indexOf ('.') != -1)
- return;
-
- if (s.length () < maxlen)
- s = s + ".";
- showacc ();
- }
-
- /**
- If the user clicks equals without
- clicking an operator
- * key first (+,-,x,/), zero the display.
- */
- private void checkEquals () {
- if (equals == true) {
- equals = false;
- s = "0";
- }
- }
-
- /**
- * Stack the addition operator for later use.
- */
- void Plus () {
- op = 1;
- operation ();
- }
-
- /**
- * Stack the subtraction operator for later use.
- */
- void Minus () {
- op = 2;
- operation ();
- }
-
- /**
- * Stack the multiplication operator for later use.
- */
- void Mul () {
- op = 3;
- operation ();
- }
-
- /**
- * Stack the division operator for later use.
- */
- void Div () {
- op = 4;
- operation ();
- }
-
- /**
- * Interpret the display value as a double, and store it
- * for later use (by Equals).
- */
- private void operation () {
- if (s.length () == 0) return;
-
- Double xyz = Double.valueOf (s);
- last = xyz.doubleValue ();
-
- equals = false;
- s = "0";
- }
- /**
- * Negate the current value and redisplay.
- */
- void Chs () {
- if (s.length () == 0) return;
-
- if (s.charAt (0) == '-') s = s.substring (1);
- else s = "-" + s;
-
- showacc ();
- }
-
- /**
- * Finish the last calculation and display the result.
- */
- void Equals () {
- double acc;
-
- if (s.length () == 0) return;
- Double xyz = Double.valueOf (s);
- switch (op) {
- case 1:
- acc = last + xyz.doubleValue ();
- break;
-
- case 2:
- acc = last - xyz.doubleValue ();
- break;
-
- case 3:
- acc = last * xyz.doubleValue ();
- break;
-
- case 4:
- acc = last / xyz.doubleValue ();
- break;
-
- default:
- acc = 0;
- break;
- }
-
- s = new Double (acc).toString ();
- showacc ();
- equals = true;
- last = 0;
- op = 0;
- }
-
- /**
- * Clear the display and the internal last value.
- */
- void Clear () {
- last = 0;
- op = 0;
- s = "0";
- equals = false;
- showacc ();
- }
-
- /**
- * Demand that the display be repainted.
- */
- private void showacc () {
- readout.setText(s);
- repaint ();
- }
-
- }
-