/* keypad_partial.pde Written by Justin Hsia Implement a phone keypad. Clicking "C" will clear the entered numbers. Clear button and board implemented. Keys recognition and clear functionality not implemented. */ // variables related to keypad int boardX = 100; int boardY = 100; int keySize = 50; int border = 2; int rows = 4; int cols = 3; char[] keypad = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#'}; // variables related to the CLEAR button int clearX = 150; int clearY = 300; void setup() { size(500, 500); textAlign(CENTER); textSize(40); // make the text bigger } void draw() { // DRAW THE CLEAR BUTTON BACKGROUND fill(0); rect(clearX, clearY, keySize, keySize); // DRAW THE CLEAR BUTTON if ( overClear() ) { // hover detection over Clear button fill(255, 255, 98); // yellow button if hovering over } else { fill(255); // white button otherwise } rect(clearX+border, clearY+border, keySize-2*border, keySize-2*border); // DRAW THE CLEAR BUTTON LABEL fill(255, 0, 0); // red text text("C", 175, 340); // DRAW THE KEYPAD BACKGROUND fill(0); // black fill rect(boardX, boardY, cols*keySize, rows*keySize); // DRAW THE KEYPAD int j = 0; while (j < rows) { // # of rows int i = 0; while (i < cols) { // # of columns fill(255); // white keys rect(boardX+border+i*keySize, boardY+border+j*keySize, keySize-2*border, keySize-2*border); fill(0); // black text text(keypad[i+3*j], boardX+keySize/2+i*keySize, boardY+j*keySize+40); i = i + 1; } j = j + 1; } } // currently prints "Cleared!" to the console when CLEAR button is clicked void mousePressed() { if ( overClear() ) { println("Cleared!"); } } // returns true if the mouse is currently over the CLEAR button boolean overClear() { return (mouseX >= clearX) && (mouseX <= clearX+keySize) && (mouseY >= clearY) && (mouseY <= clearY+keySize); }