We are about to switch to a new forum software. Until then we have removed the registration on this forum.
CalcButton[] numButtons = new CalcButton[10];
CalcButton[] opButtons = new CalcButton[6];
CalcButton[] spButtons = new CalcButton[1];
String displayValue = "0";
String valueToCompute = "";
String valueToCompute2 = "";
float valueToComputeI = 0;
float valueToComputeI2 = 0;
float result = 0;
char opValue;
boolean firstNum;
void setup() {
size(230,330);
background(110);
frameRate(10);
smooth();
// Populate number buttons
numButtons[0] = new CalcButton(true, 0, 65, 275);
numButtons[1] = new CalcButton(true, 1, 10, 220);
numButtons[2] = new CalcButton(true, 2, 65, 220);
numButtons[3] = new CalcButton(true, 3, 120, 220);
numButtons[4] = new CalcButton(true, 4, 10, 165);
numButtons[5] = new CalcButton(true, 5, 65, 165);
numButtons[6] = new CalcButton(true, 6, 120, 165);
numButtons[7] = new CalcButton(true, 7, 10, 110);
numButtons[8] = new CalcButton(true, 8, 65, 110);
numButtons[9] = new CalcButton(true, 9, 120, 110);
// Populate operators
opButtons[0] = new CalcButton("+", 175, 275);
opButtons[1] = new CalcButton("-", 175, 220);
opButtons[2] = new CalcButton("*", 175, 165);
opButtons[3] = new CalcButton("/", 175, 110);
opButtons[4] = new CalcButton("=", 120, 275);
opButtons[5] = new CalcButton("C", 10, 55);
// Populate special buttons
spButtons[0] = new CalcButton(true, ".", 10, 275);
// Set the initial value of first num to true
firstNum = true;
}
void draw() {
// Draw number buttons
for(int i=0; i<numButtons.length; i++) {
CalcButton inumButton = numButtons[i];
inumButton.display();
inumButton.clickButton();
if(mousePressed == true && numButtons[i].overBox && firstNum == true) {
valueToCompute = valueToCompute + Integer.toString(numButtons[i].numButtonValue);
displayValue = valueToCompute;
updateDisplay();
} else if (mousePressed == true && numButtons[i].overBox && firstNum == false) {
valueToCompute2 = valueToCompute2 + Integer.toString(numButtons[i].numButtonValue);
displayValue = valueToCompute2;
updateDisplay();
}
}
// Draw operator buttons
for(int i=0; i<opButtons.length; i++){
CalcButton iOpButton = opButtons[i];
iOpButton.display();
iOpButton.clickButton();
if(mousePressed == true && opButtons[i].overBox) {
if(opButtons[i].opButtonValue == "C") {
displayValue = "0";
opValue = 'C';
valueToCompute = "";
valueToCompute2 = "";
valueToComputeI = 0;
valueToComputeI = 0;
result = 0;
firstNum = true;
updateDisplay();
} else if(opButtons[i].opButtonValue == "=") {
// Perform calculation
//opValue = '=';
firstNum = true;
performCalculation();
} else if (opButtons[i].opButtonValue == "+"){
opValue = '+';
firstNum = false;
displayValue = "+";
updateDisplay();
} else if (opButtons[i].opButtonValue == "-"){
opValue = '-';
firstNum = false;
displayValue = "-";
updateDisplay();
} else if (opButtons[i].opButtonValue == "*"){
opValue = '*';
firstNum = false;
displayValue = "*";
updateDisplay();
} else if (opButtons[i].opButtonValue == "/"){
opValue = '/';
firstNum = false;
displayValue = "/";
updateDisplay();
}
}
}
// Draw special buttons
for (int i=0; i<spButtons.length; i++){
CalcButton iSpButton = spButtons[i];
iSpButton.display();
iSpButton.clickButton();
if(mousePressed == true && spButtons[i].overBox && firstNum == true) {
valueToCompute = valueToCompute + spButtons[i].spButtonValue;
displayValue = valueToCompute;
updateDisplay();
} else if (mousePressed == true && spButtons[i].overBox && firstNum == false) {
valueToCompute2 = valueToCompute2 + spButtons[i].spButtonValue;
displayValue = valueToCompute2;
updateDisplay();
}
}
updateDisplay();
}
void performCalculation() {
// set string values to integers
valueToComputeI = Float.valueOf(valueToCompute);
valueToComputeI2 = Float.valueOf(valueToCompute2);
// perform calculation based on the appropriate operator
if (opValue == '+') {
result = valueToComputeI+valueToComputeI2;
} else if (opValue == '-') {
result = valueToComputeI-valueToComputeI2;
} else if (opValue == '*') {
result = valueToComputeI*valueToComputeI2;
} else if (opValue == '/') {
result = valueToComputeI/valueToComputeI2;
}
// change result back to string
displayValue = Float.toString(result);
}
void updateDisplay() {
fill(230);
rect(10,10,210,35,7);
fill(0);
text(displayValue,20,35);
}
class CalcButton {
// Class variables
boolean isNumber;
boolean isSpecial;
int numButtonValue;
String opButtonValue;
String spButtonValue;
float xpos;
float ypos;
int boxSize = 45;
boolean overBox = false;
// Constructor for number buttons
CalcButton(boolean tempIsNumber, int tempNumButtonValue, float tempXpos, float tempYpos) {
isNumber = tempIsNumber;
numButtonValue = tempNumButtonValue;
xpos = tempXpos;
ypos = tempYpos;
}
// Constructor for operator buttons
CalcButton(String tempOpButtonValue, float tempXpos, float tempYpos) {
opButtonValue = tempOpButtonValue;
xpos = tempXpos;
ypos = tempYpos;
}
// Constructor for special buttons
CalcButton(boolean tempIsSpecial, String tempSpButtonValue, float tempXpos, float tempYpos) {
isSpecial = tempIsSpecial;
spButtonValue = tempSpButtonValue;
xpos = tempXpos;
ypos = tempYpos;
}
// Draw the button on the canvas
void display() {
// Draw rounded edged button on canvas
fill(150);
stroke(255);
rect(xpos, ypos, boxSize, boxSize, 10);
if(isNumber) {
fill(0);
textSize(20);
text(numButtonValue, xpos+17, ypos+30);
} else if (isSpecial) {
fill(0);
textSize(20);
text(spButtonValue, xpos+17, ypos+30);
} else {
fill(0);
textSize(20);
text(opButtonValue, xpos+17, ypos+30);
}
}
// Handle mouse actions
void clickButton() {
if (mouseX > xpos && mouseX < xpos+boxSize && mouseY > ypos && mouseY < ypos+boxSize) {
overBox = true;
} else {
overBox = false;
}
}
}
Answers
The error console of my browser notes a "ReferenceError: Can't find variable: Integer", which is caused by your Integer.toString construct on lines 55 and 59. JavaScript recognizes Integer as a variable, which was not defined.
Why not simply use the Processing function str() instead?
Are you sure it is the mousePressed that doesn't work? A way to know is to println() inside the test...
I am suspecting more code like
Integer.toString()
which is prone to fail in JS mode. I wonder why you used this convoluted method while Processing offers thestr()
function. Beside, neither are necessary, as adding an integer to a string automatically converts this integer to string.Funny, PhiLho. Our messages crossed within 1 minute!
Yes, that's what I get for opening a message, doing something else for a while, then replying... :-) At least, we agree on the source of the trouble! B-)