Best way to do calculator style digit entry
in
Programming Questions
•
2 months ago
I'm working on my first project in Processing which is a calculator. I've reached a speed bump. The way I thought I could do digit entry isn't working. Anybody got any ideas?
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
NOTE:
Calculator entry is like this:
*Click 7 button*
Screen reads:7
*Click 2 button*
Screen reads:72
If It helps my code is:
Calculator.pde
- PFont f;
- import java.math.*;
- //All of the below values allow me to quickly change values code wide
- final int topRowYPos = 80;
- final int secondRowYPos = 110;
- final int thirdRowYPos = 140;
- final int fourthRowYPos = 170;
- final int fithRowYPos = 200;
- final int sixthRowYPos = 230;
- final int buttonWidth = 35;
- final int buttonHeight = 25;
- final int firstRowXPos = 45;
- final int secondRowXPos = 85;
- final int thirdRowXPos = 125;
- final int fourthRowXPos = 165;
- final int fithRowXPos = 205;
- final int maxB = 27;
- boolean wTillUpToRefresh; //Selectes wheather you will refresh automaticly or only on next button push
- int digitCount = 0; //Counts digits. Required in the process of adding digits.
- PFont sF = createFont("Georgia", 16, true); //Creates font used calculator wide
- boolean isDecimal; //Checks wheather I've pressed the decimal buttons. Determines wheather the number needs to be to number*10^1 or number*10^-1
- double numOneBuffer = 0; //Stores firstnumber;
- double numTwoBuffer = 0; //Stores second number;
- int numOn = 1; //Counts which position in the calculating process the user is in. 1 is first number, 2 is function(not needed but there) 3 is second number 4 is answer mode
- String functionBuffer = "N"; //Stores which function the calculation is using
- String screenBuffer = "0"; //Stores what is currently being displayed on the screen
- double answerBuffer = 0; //Stores answer
- double numMemory = 0; //Stores one varible in "Memory"
- int buttonPressed = -1; //Detects what button has been pressed
- boolean sign = true;
- Button[] buttons = new Button [maxB]; //Button array for all buttons
- void setup() {
- size(250, 280);
- //The following lines initilize the buttons
- buttons[0] = new Button(firstRowXPos, secondRowYPos, "MC", false);
- buttons[1] = new Button(firstRowXPos, thirdRowYPos, "%", false);
- buttons[2] = new Button(firstRowXPos, fourthRowYPos, "+/-", false);
- buttons[3] = new Button(firstRowXPos, fithRowYPos, "C", false);
- buttons[4] = new Button(firstRowXPos, sixthRowYPos, "AC", false);
- buttons[5] = new Button(secondRowXPos, secondRowYPos, "MR", false);
- buttons[6] = new Button(secondRowXPos, thirdRowYPos, "7", false);
- buttons[7] = new Button(secondRowXPos, fourthRowYPos, "4", false);
- buttons[8] = new Button(secondRowXPos, fithRowYPos, "1", false);
- buttons[9] = new Button(secondRowXPos, sixthRowYPos, "0", false);
- buttons[10] = new Button(thirdRowXPos, secondRowYPos, "M-", false);
- buttons[11] = new Button(thirdRowXPos, thirdRowYPos, "8", false);
- buttons[12] = new Button(thirdRowXPos, fourthRowYPos, "5", false);
- buttons[13] = new Button(thirdRowXPos, fithRowYPos, "2", false);
- buttons[14] = new Button(thirdRowXPos, sixthRowYPos, "=", false);
- buttons[15] = new Button(fourthRowXPos, topRowYPos, "√", false);
- buttons[16] = new Button(fourthRowXPos, secondRowYPos, "M+", false);
- buttons[17] = new Button(fourthRowXPos, thirdRowYPos, "9", false);
- buttons[18] = new Button(fourthRowXPos, fourthRowYPos, "6", false);
- buttons[19] = new Button(fourthRowXPos, fithRowYPos, "3", false);
- buttons[20] = new Button(fourthRowXPos, sixthRowYPos, ".", false);
- buttons[21] = new Button(fithRowXPos, topRowYPos, "off", false);
- buttons[22] = new Button(fithRowXPos, secondRowYPos, "÷", false);
- buttons[23] = new Button(fithRowXPos, thirdRowYPos, "*", false);
- buttons[24] = new Button(fithRowXPos, fourthRowYPos, "-", false);
- buttons[25] = new Button(fithRowXPos, fithRowYPos, "", true);
- buttons[26] = new Button(fithRowXPos, sixthRowYPos, "", true);
- }
- void draw() {
- rectMode(CENTER);
- fill(255);
- //Calculatorbody
- rect(125, 140, 240, 270);
- //"Screen"
- rect(125, 40, 220, 30);
- //Double button
- rect(fithRowXPos, 215, buttonWidth, 55, 7);
- fill(0);
- textAlign(CENTER);
- textFont(sF, 16);
- //text for double button
- text("+", fithRowXPos, 215);
- //updates and displays screenBuffer
- update();
- //displays buttons
- for (int i = 0; i < maxB; i++) {
- buttons[i].display();
- }
- }
- void mousePressed() {
- //Checks which button has been pressed
- wTillUpToRefresh = false;
- for (int i = 0; i < maxB; i++) {
- if (buttons[i].isOverButton()) {
- buttonPressed = i;
- break;
- }
- }
- println(buttonPressed);//Debug code
- if (buttonPressed == 0) {
- //Clears "Memory" if MC button is pressed
- numMemory = 0;
- buttonPressed = -1;
- }
- else if (buttonPressed == 1 && numOn != 1 && numOn != 2) {
- //Takes specified percent from numOneBuffer and sets numTwoBuffer to it
- numTwoBuffer = numOneBuffer*(numTwoBuffer/100);
- buttonPressed = -1;
- }
- else if (buttonPressed == 2) {
- //Sets sign of the number the "calculator" is on based on current sign and number;
- if (sign && numOn == 1) {
- numOneBuffer = numOneBuffer + (-2*numOneBuffer);
- sign = false;
- }
- else if (!sign && numOn == 1) {
- numOneBuffer = numOneBuffer + (-2*numOneBuffer);
- sign = true;
- }
- else if (sign && numOn == 3) {
- numTwoBuffer = numTwoBuffer + (-2*numTwoBuffer);
- sign = false;
- }
- else if (!sign && numOn == 3) {
- numTwoBuffer = numTwoBuffer + (-2*numTwoBuffer);
- sign = true;
- }
- buttonPressed = -1;
- }
- else if (buttonPressed == 3) {
- //Clears current number and the properties of that number
- if (numOn == 1) {
- numOneBuffer = 0;
- isDecimal = false;
- sign = true;
- digitCount = 0;
- answerBuffer = 0;
- }
- else if (numOn == 3) {
- numTwoBuffer = 0;
- isDecimal = false;
- sign = true;
- digitCount = 0;
- answerBuffer = 0;
- }
- else if (numOn == 4) {
- numOn = 1;
- answerBuffer = 0;
- }
- buttonPressed = -1;
- }
- else if (buttonPressed == 4) {
- //resets buffers and all numbers properties
- numOneBuffer = 0;
- numTwoBuffer = 0;
- isDecimal = false;
- digitCount = 0;
- answerBuffer = 0;
- screenBuffer = "";
- numOn = 1;
- sign = true;
- buttonPressed = -1;
- }
- else if (buttonPressed == 5) {
- //displays value in memory until next button is pressed
- screenBuffer = String.valueOf(numMemory);
- wTillUpToRefresh = true;
- update();
- buttonPressed = -1;
- }
- else if (buttonPressed == 6) {
- //Enters the digit 7
- buttonPressed = -1;
- }
- }
- double evaluate() {
- //insert code
- return 1;
- }
- void update() {
- if (!wTillUpToRefresh) {
- if (numOn == 1) {
- //writes numOneBuffer to screen if the user is on step one
- screenBuffer = String.valueOf(numOneBuffer);
- }
- else if (numOn == 3) {
- //writes numTwoBuffer to screen if the user is on step two
- screenBuffer = String.valueOf(numTwoBuffer);
- }
- }
- text(screenBuffer, 125, 40);
- }
- class Button {
- static final int BTN_DX = 35, BTN_DY = 25; // diameters x, y
- static final int BTN_RX = BTN_DX >> 1; // radius x
- static final int BTN_RY = BTN_DY >> 1; // radius
- final int left, right, top, bottom, centerX, centerY; // button edges and Center Coords
- final String text;
- final boolean isDouble;
- Button (int cx, int cy, String buttonText, boolean doubleButton) {
- left = cx - BTN_RX;
- right = cx + BTN_RX;
- top = cy - BTN_RY;
- bottom = cy + BTN_RY;
- centerX = cx;
- centerY = cy;
- text = buttonText;
- isDouble = doubleButton;
- }
- boolean isOverButton() {
- return mouseX > left & mouseX < right
- & mouseY > top & mouseY < bottom;
- }
- void display() {
- if (!isDouble) {
- PFont f = createFont("Georgia", 16, true);
- textFont(f, 16);
- textAlign(CENTER);
- fill(255);
- rect(centerX, centerY, BTN_DX, BTN_DY, 7);
- fill(0);
- text(text, centerX, centerY + 5);
- }
- }
- }
1