Alex Mccane
Alex Mccane

Reputation: 53

If keyPressed in processing

I'm trying to make a simple program to calculate loan interest. The code I have here works but will only display the result when the 'c' key is pressed down. How can i change the logic so that once 'c' is pressed the result appears on the screen. Any help is appreciated in advanced.

   import controlP5.*;
ControlP5 cp5;
Textfield loan; 
Textfield interest;
Textfield months;
Button calculate;
Double loan1;
Double months1;
double interest1;
void setup(){
size(700,400);
PFont font = createFont("arial",20);
cp5 = new ControlP5(this);
loan =  cp5.addTextfield("Loan Amount")
 .setPosition(20,100)
 .setSize(200,40)
 .setFont(font)
 .setFocus(true)
 .setColor(color(255,0,0));
 loan.setInputFilter(ControlP5.INTEGER)
 ;         
  interest =  cp5.addTextfield("Interest Rate")
 .setPosition(20,190)
 .setSize(200,40)
 .setFont(font)
 .setFocus(false)
 .setColor(color(255,0,0));
 interest.setInputFilter(ControlP5.INTEGER)
 ;
  months =  cp5.addTextfield("Length in months")
 .setPosition(450,100)
 .setSize(200,40)
 .setFont(font)
 .setFocus(false)
 .setColor(color(255,0,0));
 months.setInputFilter(ControlP5.INTEGER)
 ;
  }
 void draw(){
background(120);
fill(255);
noStroke();
rect(0,0,750,70);
fill(20);
textSize(32);
text("Monthly Loan Repayment Calculator ", 70, 45); 
fill(41,194,214);
ellipse(340,170,130,130);
fill(24,74,140);
ellipse(340,170,125,125);
fill(255);
textSize(18);
text("Enter 'c'",305,155);
text("to calculate", 295,175);
fill(0);
rect(0,260,700,1);

String loan2 =     loan.getText();
String interest2 = interest.getText();
String months2 =   months.getText();

 if(keyPressed)
{
    if(key=='c'||key=='C'&&!loan2.equals("")&&!interest2.equals("")&&!months2.equals         ("")){
loan1 = Double.parseDouble(loan2);
interest1 = Double.parseDouble(interest2);
months1 = Double.parseDouble(months2);

interest1=interest1/100/12;
double payment = (loan1*interest1)/(1-Math.pow(interest1+1,-months1));
payment = (double)Math.round(payment*100)/100;
//println(payment);
//text(payment,20,20);

int total = (int)payment;

text(total,200,350);
  }


}
}

Upvotes: 0

Views: 2426

Answers (1)

John
John

Reputation: 2042

You are currently checking within your draw() method if a key is pressed or not and only displaying the text field showing the interest computation if the key is currently pressed. This is what

if (keyPressed) {
...

is doing. A better method is to override the actual keyPressed() method (outside of draw()) and set a boolean flag once the 'c' key is pressed. So at the top of your program you'd create something like:

boolean showInterestText = false;

You'd then change your two nested if statements

 if(keyPressed)
 {
    if(key=='c'||key=='C'&&!loan2.equals("")&&!interest2.equals("")&&!months2.equals         ("")){
 ...

to a single if statement like

if (showInterestText) {
...

Finally, override the keyPressed() method to set showInterestText to true if the 'c' key is pressed. Something like:

void keyPressed() {
    if (key == 'c' || key == 'C') {
        showInterestText = true;
    }
}

This way, the showInterestText boolean remains true after the key is no longer pressed.

PS. Welcome to stackoverflow, next time you post please format your code so that it is readable.

Upvotes: 1

Related Questions