Hi all. First post here. I have to make a calculator as a homework assignment. What I have so far is working buttons with results displayed in the terminal using a println. What I am trying to achieve is to display the results (that are currently being shown in the terminal) within the program itself, in the white box drawn on the top.
To achieve that, I tried adding the following code:
String entrytext ="";
and
void setup(){
size(300,250);
fill(0);
textSize(15);
textAlign(LEFT,CENTER);
text(acc, num, 15,25);
this however hasn’t worked. When i run the program, I see a few 0’s in the box for a split second, but they disappear super fast and i am met with the following message in the terminal: “translate(), or this particular variation of it, is not available with this renderer.”
I am a complete beginner, so I have no idea what to do with this information, much less what it means. Can anyone shed light on this for me, and help me find a way to display my results within my program?
I will attach all my code below.
String[] bLabel = {"0","1","2","3","4","5","6","7","8","9",".","+","-","*","/","=","C"};
int[] bX = { 80, 10, 80, 150, 10, 80,150, 10, 80,150,220,220,255,220,255,255,220},
bY = {190, 70, 70, 70, 110, 110, 110,150,150,150,180, 110, 110,145,145,180, 70},
bW = { 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 30, 30, 30, 30, 30, 30, 65},
bH = { 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 30, 30, 30, 30, 30, 30, 25};
int bID,
prevOp;
float acc, num, div;
boolean decimalClicked;
String entrytext ="";
void setup(){
size(300,250);
fill(0);
textSize(15);
textAlign(LEFT,CENTER);
text(acc, num, 15,25);
}
void draw(){
background(184,219,209);
size(300,250);
fill(255,255,255);
rect(10,5,280,40);
fill(255,255,255);
for (int i=0; i<17; i++){
fill(184,203,219);
rect(bX[i],bY[i],bW[i],bH[i]);
fill(0);
textAlign(CENTER,CENTER);
textSize(20);
text(bLabel[i],bX[i]+bW[i]/2,bY[i]+bH[i]/2);
}
}
void mousePressed(){
bID = -1;
for(int i=0; i<17; i++){
if((mouseX >= bX[i])&(mouseX <= bX[i]+bW[i])&
(mouseY >= bY[i])&(mouseY <= bY[i]+bH[i]))
bID = i;
}
if(bID != -1){
println(bLabel[bID]);
switch(bID){
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
if(!decimalClicked)
num = num*10 + bID;
else {
div = 10 * div;
num = num + bID / div;
}
break;
case 10: // decimal
decimalClicked = true;
println("Decimal");
break;
case 11: // additon
case 12: // subtraction
case 13: // multiplication
case 14: // division
prevOp = bID;
acc = num;
num = 0;
break;
case 15: // equals
switch(prevOp){
case -1:
acc = num;
break;
case 11: // add
acc = acc + num;
break;
case 12: // subtract
acc = acc - num;
break;
case 13: // multiply
acc = acc * num;
break;
case 14: // divide
acc = acc / num;
break;
}
num = 0;
break;
case 16: // clear numbers
break;
default:
}
println(acc, num);
}
}