How to create input numeric box that convert input in a variable.

edited March 2018 in How To...

Hi, I'm a beginner, I'm trying to create a simple application with 2 numeric input box. I would like return the sum of these in another box. But I don't know how to create the input box and to take the value in the two respective variables. Would you know how I could do it?

Tagged:

Answers

  • edited April 2018

    Let’s say you have setup and draw

    You have 2 String variables defined before setup as ““ Make a 3rd function keyPressed

    Here say

    If(textBox1HasFocus)
    
    str1=str1+key; 
    
    else 
    
    str2=str2+key;
    

    When Return is presses say textBox1HasFocus=false;

    Before setup say

    boolean textBox1HasFocus=true;
    

    In draw () use rect() and text(str1 ,.....

    and text(str2, ....

    Then when return is pressed again display the sum....

    Hit ctrl-t

    Show your entire code here

  • edited April 2018

    Sorry I didn't understand very well Chrisir.

    At moment my code is

    int box1X, box1Y, box2X, box2Y;
    int LargBox, AltezzaBox;
    float str1, str2;
    color coloreBox;
    boolean textBox1HasFocus, textBox2HasFocus;
       
    void setup() {
      size(640, 360);
      noStroke();
      background(200);
      LargBox=width/5;
      AltezzaBox=height/10;
      box1X = 10;
      box1Y = 10; 
      box2X = box1X + LargBox + 10;
      box2Y = 10; 
      
    }
    
    void draw() { 
      fill(255);
      rect(box1X,box1Y,LargBox,AltezzaBox);
      rect(box2X,box2Y,LargBox,AltezzaBox);
    }
    
    
    // ----------  Funzioni ---------------------
    void keyPressed(){
      if(textBox1HasFocus){str1=str1+key;}
      else {str2=str2+key;}
    
    }
    
  • Thanks GotoLoop I would try to learn step by step how to make this thing if it's possible,

  • I worked a little on your sketh

    int box1X, box1Y, 
      box2X, box2Y;
    int LargBox, AltezzaBox;
    
    String str1="", str2="";
    
    color coloreBox;
    boolean textBox1HasFocus=true, textBox2HasFocus=false;
    
    boolean showResult=false; 
    
    void setup() {
      size(640, 360);
    
      noStroke();
      background(200);
      LargBox=width/5;
      AltezzaBox=height/10;
      box1X = 10;
      box1Y = 10; 
      box2X = box1X + LargBox + 10;
      box2Y = 10;
    }
    
    void draw() { 
      background(200);
    
      fill(255);
      rect(box1X, box1Y, LargBox, AltezzaBox);
      fill(0); 
      text(str1, box1X+2, box1Y+14);
    
      fill(255);
      rect(box2X, box2Y, LargBox, AltezzaBox);
      fill(0); 
      text(str2, box2X+2, box2Y+14);
    }
    
    
    // ----------  Funzioni ---------------------
    void keyPressed() {
    
      if (key==RETURN||key==ENTER) {
        if (textBox1HasFocus) {
          // we move on to number 2
          textBox1HasFocus=false;
          textBox2HasFocus=true;
        } else 
        {  
          // after number 2 we show the result - TO DO in draw() !!!!!!!!!!! 
          showResult=true;
          textBox1HasFocus=false;
          textBox2HasFocus=false;
        }
        return; // leave function
      } // if (key==RETURN||key==ENTER) { 
    
      // ---
    
      if (key>='0'&&key<='9') {
        if (textBox1HasFocus) {
          str1=str1+key;
        } else  if (textBox2HasFocus) {
          str2=str2+key;
        }
      }
    }
    //
    
Sign In or Register to comment.