package polarDust.calculator; /* * Copyright (c) 1996 Sorin Lazareanu, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * */ import java.awt.*; /** * KeypadPanel02 is a button container for a calculator with * more operations. * *

* The source code. * * @version 0.9, 1996.06.04 * @author Sorin Lazareanu */ public class KeypadPanel02 extends Panel implements CalcConstants { double dFactorX, dFactorY; Label fillArea; Button btnClr, btnCE; Button btnInv, btnSqr, btnSwap, btnLn, btnLog, btnSqrt; Button btnSgn, btnPrc, btnMC; Button btnMul, btnDiv, btnMR; Button btnAdd, btnSub, btnMM; Button btnEqu, btnMP; CalcCPU cpu; CalculatorApplet parentApplet; NumKeypadPanel numKeypad; Dimension lastDimension = new Dimension(0,0); /** * Constructs a new panel. The original call is from an CalculatorApplet. * This information is used to send back sound related messages. * @param parentApplet the applet which constains sound methods. * @see CalculatorApplet */ public KeypadPanel02(CalcCPU aCPU, CalculatorApplet parentApplet) { cpu = aCPU; this.parentApplet = parentApplet; numKeypad = new NumKeypadPanel(aCPU, parentApplet); commonInit(); } /** * Constructs a new panel. * @param aCPU the class wich do the processing * @see CalcCPU */ public KeypadPanel02(CalcCPU aCPU) { cpu = aCPU; numKeypad = new NumKeypadPanel(aCPU); commonInit(); } private void commonInit() { fillArea = new Label(""); btnClr = new Button(STR_CLR); btnCE = new Button(STR_CE); btnInv = new Button(STR_INV); btnSqr = new Button(STR_SQR); btnSwap = new Button(STR_SWAP); btnLn = new Button(STR_LN); btnLog = new Button(STR_LOG); btnSqrt = new Button(STR_SQRT); btnSgn = new Button(STR_SGN); btnPrc = new Button(STR_PRC); btnMC = new Button(STR_MC); btnMul = new Button(STR_MUL); btnDiv = new Button(STR_DIV); btnMR = new Button(STR_MR); btnAdd = new Button(STR_ADD); btnSub = new Button(STR_SUB); btnMM = new Button(STR_MADD); btnEqu = new Button(STR_EQU); btnMP = new Button(STR_MSUB); } /** * Prepares the layout of the panel */ public void configure() { GridBagLayout gridBag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); GridBagLayoutHelper aHelper; setLayout(gridBag); c.fill = GridBagConstraints.BOTH; c.anchor = GridBagConstraints.CENTER; c.insets = new Insets(2, 2, 2, 2); aHelper = new GridBagLayoutHelper(this, gridBag, c, 5, 7); setFont(new Font(HELVETICA, Font.PLAIN, 14)); aHelper.addComponent(0, 0, 4, 1, fillArea); aHelper.addComponent(4, 0, 1, 1, btnClr); aHelper.addComponent(5, 0, 1, 1, btnCE); aHelper.addComponent(0, 1, 1, 1, btnInv); aHelper.addComponent(1, 1, 1, 1, btnSqr); aHelper.addComponent(2, 1, 1, 1, btnSwap); aHelper.addComponent(3, 1, 1, 1, btnLn); aHelper.addComponent(4, 1, 1, 1, btnLog); aHelper.addComponent(5, 1, 1, 1, btnSqrt); c.insets = new Insets(0, 0, 0, 0); numKeypad.configure(); aHelper.addComponent(0, 2, 3, 4, numKeypad); c.insets = new Insets(2, 2, 2, 2); aHelper.addComponent(3, 2, 1, 1, btnSgn); aHelper.addComponent(4, 2, 1, 1, btnPrc); aHelper.addComponent(5, 2, 1, 1, btnMC); aHelper.addComponent(3, 3, 1, 1, btnMul); aHelper.addComponent(4, 3, 1, 1, btnDiv); aHelper.addComponent(5, 3, 1, 1, btnMR); aHelper.addComponent(3, 4, 1, 1, btnAdd); aHelper.addComponent(4, 4, 1, 1, btnSub); aHelper.addComponent(5, 4, 1, 1, btnMM); aHelper.addComponent(3, 5, 2, 1, btnEqu); aHelper.addComponent(5, 5, 1, 1, btnMP); } private void op(int iOper) { if (parentApplet != null) { parentApplet.op(iOper); } } /** * Responds to button clicks. * @see Event * @see Object */ public boolean action(Event evt, Object arg) { if (evt.target == btnClr) { cpu.c(); return true; }; if (evt.target == btnCE) { cpu.ce(); return true; }; if (evt.target == btnAdd) { op(ADD); cpu.add(); return true; }; if (evt.target == btnSub) { op(SUB); cpu.sub(); return true; }; if (evt.target == btnMul) { op(MUL); cpu.mul(); return true; }; if (evt.target == btnDiv) { op(DIV); cpu.div(); return true; }; if (evt.target == btnPrc) { op(PRC); cpu.prc(); return true; }; if (evt.target == btnEqu) { op(EQU); cpu.equ(); return true; }; if (evt.target == btnMM) { cpu.msub(); return true; }; if (evt.target == btnMP) { cpu.madd(); return true; }; if (evt.target == btnSgn) { cpu.sgn(); return true; }; if (evt.target == btnMR) { cpu.mr(); return true; }; if (evt.target == btnMC) { cpu.mc(); return true; }; if (evt.target == btnSqrt) { cpu.sqrt(); return true; }; if (evt.target == btnInv) { cpu.inv(); return true; }; if (evt.target == btnSqr) { cpu.sqr(); return true; }; if (evt.target == btnSwap) { cpu.swap(); return true; }; if (evt.target == btnLn) { cpu.ln(); return true; }; if (evt.target == btnLog) { cpu.log(); return true; }; return false; } /** * Dispatches unintercepted key down events to the components. */ public boolean dispatchKeyDown(Event evt, int iKey) { boolean bDispatched; bDispatched = keyDown(evt, iKey); if (!bDispatched) bDispatched = numKeypad.dispatchKeyDown(evt, iKey); return bDispatched; } /** * Dispatches unintercepted key up events to the components. */ public boolean dispatchKeyUp(Event evt, int iKey) { boolean bDispatched; bDispatched = keyUp(evt, iKey); if (!bDispatched) bDispatched = numKeypad.dispatchKeyUp(evt, iKey); return bDispatched; } /** * Responds to key down events. */ public boolean keyDown(Event evt, int iKey) { if (evt.key == new Character('a').hashCode()) { cpu.c(); return true; }; if (evt.key == new Character('c').hashCode()) { cpu.ce(); return true; }; if (evt.key == new Character('+').hashCode()) { cpu.add(); return true; }; if (evt.key == new Character('-').hashCode()) { cpu.sub(); return true; }; if (evt.key == new Character('*').hashCode()) { cpu.mul(); return true; }; if (evt.key == new Character('/').hashCode()) { cpu.div(); return true; }; if (evt.key == new Character('%').hashCode()) { cpu.prc(); return true; }; if (evt.key == new Character('=').hashCode()) { cpu.equ(); return true; }; if (evt.key == new Character('s').hashCode()) { cpu.msub(); return true; }; if (evt.key == new Character('p').hashCode()) { cpu.madd(); return true; }; if (evt.key == new Character('n').hashCode()) { cpu.sgn(); return true; }; if (evt.key == new Character('r').hashCode()) { cpu.mr(); return true; }; if (evt.key == new Character('m').hashCode()) { cpu.mc(); return true; }; if (evt.key == new Character('q').hashCode()) { cpu.sqrt(); return true; }; return false; } /** * Paints on a Graphics object. * @see Graphics */ public void paint(Graphics aGraphics) { if (lastDimension.width != size().width || lastDimension.height != size().height) { int iFontHeight = Math.min( size().height / 12, size().width / 12 ); Font stdFont = new Font("Helvetica", Font.PLAIN, iFontHeight); btnClr.setFont(stdFont); btnCE.setFont(stdFont); btnMC.setFont(stdFont); btnMR.setFont(stdFont); btnMM.setFont(stdFont); btnMP.setFont(stdFont); btnSqrt.setFont(stdFont); btnSgn.setFont(stdFont); btnPrc.setFont(stdFont); btnMul.setFont(stdFont); btnDiv.setFont(stdFont); btnAdd.setFont(stdFont); btnSub.setFont(stdFont); btnEqu.setFont(stdFont); btnInv.setFont(stdFont); btnSqr.setFont(stdFont); btnSwap.setFont(stdFont); btnLn.setFont(stdFont); btnLog.setFont(stdFont); lastDimension = size(); } super.paint(aGraphics); } }