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.*;
import java.applet.*;
import java.net.*;
/**
* NumKeypadPanel is a container for a NumKeypad component.
*
*
* The source code.
*
* @version 0.9, 1996.06.04
* @author Sorin Lazareanu
*/
public class NumKeypadPanel extends Panel implements CalcConstants {
Button btn7, btn8, btn9;
Button btn4, btn5, btn6;
Button btn1, btn2, btn3;
Button btn0, btnDot;
CalcCPU cpu;
Dimension lastDimension = new Dimension(0,0);
CalculatorApplet parentApplet = null;
/**
* Constructs a panel with the following buttons: 0, ..., 9, dot(.).
* @param aCPU the processor where the messages are sent.
* @param parentApplet used for sound related messages.
* @see Processor01
* @see CalculatorApplet
*/
public NumKeypadPanel(CalcCPU aCPU, CalculatorApplet parentApplet) {
cpu = aCPU;
this.parentApplet = parentApplet;
commonInit();
}
/**
* Constructs a panel with the following buttons: 0, ..., 9, dot(.).
* @param aCPU the processor where the messages are sent.
* @see Processor01
*/
public NumKeypadPanel(CalcCPU aCPU) {
cpu = aCPU;
commonInit();
}
private void commonInit() {
btn7= new Button(STR_7);
btn8= new Button(STR_8);
btn9= new Button(STR_9);
btn4= new Button(STR_4);
btn5= new Button(STR_5);
btn6= new Button(STR_6);
btn1 = new Button(STR_1);
btn2 = new Button(STR_2);
btn3 = new Button(STR_3);
btn0 = new Button(STR_0);
btnDot = new Button(STR_DOT);
}
/**
* Configures panel's layout.
*/
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, 3, 4);
setFont(new Font(HELVETICA, Font.PLAIN, 14));
aHelper.addComponent(0, 0, 1, 1, btn7);
aHelper.addComponent(1, 0, 1, 1, btn8);
aHelper.addComponent(2, 0, 1, 1, btn9);
aHelper.addComponent(0, 1, 1, 1, btn4);
aHelper.addComponent(1, 1, 1, 1, btn5);
aHelper.addComponent(2, 1, 1, 1, btn6);
aHelper.addComponent(0, 2, 1, 1, btn1);
aHelper.addComponent(1, 2, 1, 1, btn2);
aHelper.addComponent(2, 2, 1, 1, btn3);
aHelper.addComponent(0, 3, 2, 1, btn0);
aHelper.addComponent(2, 3, 1, 1, btnDot);
}
/**
* Responds to button actions.
* @see Event
* @see Object
*/
public boolean action(Event evt, Object arg) {
if (evt.target == btn0) { digit(0); return true; }
if (evt.target == btn1) { digit(1); return true; }
if (evt.target == btn2) { digit(2); return true; }
if (evt.target == btn3) { digit(3); return true; }
if (evt.target == btn4) { digit(4); return true; }
if (evt.target == btn5) { digit(5); return true; }
if (evt.target == btn6) { digit(6); return true; }
if (evt.target == btn7) { digit(7); return true; }
if (evt.target == btn8) { digit(8); return true; }
if (evt.target == btn9) { digit(9); return true; }
if (evt.target == btnDot) { dot(); return true; }
return false;
}
/**
* Dispatch unintercepted key down events.
*/
public boolean dispatchKeyDown(Event evt, int iKey) {
return keyDown(evt, iKey);
}
/**
* Dispatch unintercepted key down events.
*/
public boolean dispatchKeyUp(Event evt, int iKey) {
return keyUp(evt, iKey);
}
/**
* Responds to key actions.
* @see Event
*/
public boolean keyDown(Event evt, int iKey) {
if (evt.key == CALC_KEY_0) { digit(0); return true; };
if (evt.key == CALC_KEY_1) { digit(1); return true; };
if (evt.key == CALC_KEY_2) { digit(2); return true; };
if (evt.key == CALC_KEY_3) { digit(3); return true; };
if (evt.key == CALC_KEY_4) { digit(4); return true; };
if (evt.key == CALC_KEY_5) { digit(5); return true; };
if (evt.key == CALC_KEY_6) { digit(6); return true; };
if (evt.key == CALC_KEY_7) { digit(7); return true; };
if (evt.key == CALC_KEY_8) { digit(8); return true; };
if (evt.key == CALC_KEY_9) { digit(9); return true; };
return false;
}
/**
* Responds to key actions.
* @see Event
*/
public boolean keyUp(Event evt, int iKey) {
if (evt.key == CALC_KEY_DOT) { dot(); return true; };
return false;
}
private void digit(int iDigit) {
if (parentApplet != null) parentApplet.digit(iDigit);
cpu.digit(iDigit);
}
private void dot() {
if (parentApplet != null) parentApplet.dot();
cpu.dot();
}
/**
* Paints the panel.
* @see Graphics
*/
public void paint(Graphics aGraphics) {
if (lastDimension.width != size().width || lastDimension.height != size().height) {
int iFontHeight = Math.min( size().height / 8, size().width / 6 );
Font stdFont = new Font(HELVETICA, Font.PLAIN, iFontHeight);
btn1.setFont(stdFont);
btn2.setFont(stdFont);
btn3.setFont(stdFont);
btn4.setFont(stdFont);
btn5.setFont(stdFont);
btn6.setFont(stdFont);
btn7.setFont(stdFont);
btn8.setFont(stdFont);
btn9.setFont(stdFont);
btn0.setFont(stdFont);
btnDot.setFont(stdFont);
lastDimension = size();
}
super.paint(aGraphics);
}
}