turning off other buttons while clicking on one of the buttons in an array.

Hi there,

I made an array of buttons using a class written. Buttons work fine but not turning off when I click on one of the other buttons in the array.

Any help would be much appreciated Many thanks in advance :)

Code

    //////////////////////

    Button [] b ;


    void setup() {


      size(800, 800);
      smooth();


      b= new Button[5];

      for (int i =0; i<b.length; i++) {
        b[i]= new Button(i*width/b.length, 0, width/b.length, height/18);
      }
    }


    void draw() {

      background(0);

      for (int i =0; i<b.length; i++) {
        b[i].display();
      }


    }
    void mousePressed() {    
        for (int i =0; i<b.length; i++) {
        b[i].interaction();
      }
    }



    //////////////////////////////


    class Button {

      float posX, posY, bWidth, bHeight;

      boolean status;

      Button(float _x, float _y, float _bWidth, float _bHeight) {


        posX = _x;
        posY = _y;
        bWidth = _bWidth;
        bHeight = _bHeight;
      }

      void interaction() {

        if (mouseX>posX&&mouseX<posX+bWidth&&mouseY>posY&&mouseY<posY+bWidth) {

          status = !status;
        }
      }

      void display() {

        noStroke();
        if (status) {

          fill(45, 45, 200);
        } else {
          fill(45);
        }

        rect(posX, posY, bWidth, bHeight);
      }
    }
Tagged:

Answers

  • Answer ✓
    //////////////////////
    
    Button [] b;
    
    
    void setup() {
      size(800, 800);
      smooth();
    
      b = new Button[5];
    
      for (int i =0; i<b.length; i++) {
        b[i]= new Button(i*width/b.length, 0, width/b.length, height/18);
      }
    }
    
    void draw() {
    
      background(0);
    
      for (int i =0; i<b.length; i++) {
        b[i].display();
      }
    }
    
    void mousePressed() {    
      for (int i =0; i<b.length; i++) {
        b[i].interaction();
      }
    }
    
    //////////////////////////////
    
    
    class Button {
    
      float posX, posY, 
        bWidth, bHeight;
      boolean status;
    
      // constr
      Button(float _x, float _y, 
        float _bWidth, float _bHeight) {
    
        posX = _x;
        posY = _y;
        bWidth = _bWidth;
        bHeight = _bHeight;
      }// constr
    
      void interaction() {
        boolean prevStatus=status; 
        status=false;
        if (mouseX>posX&&mouseX<posX+bWidth&&
          mouseY>posY&&mouseY<posY+bHeight) {
          if (prevStatus)
            status = false;
          else status = true;
        }//if
      }//method
    
      void display() {
    
        //noStroke();
        stroke(111);
    
        if (status) { 
          fill(45, 45, 200);
        } else {
          fill(45);
        }
    
        rect(posX, posY, bWidth, bHeight);
      }
    }//class
    //
    
  • Answer ✓

    The 2nd line in method interaction() status=false; switches each button off before checking it.

  • @Chrisir perfect! works well Thank you :)

  • @Chrisir perfect! works well Thank you :)

  • Answer ✓

    thank you!

    in the last version though, the active button gets switched off unfortunately when you click outside any button.

    In this new version the button stays ON.

    //////////////////////
    
    Button [] b;
    
    
    void setup() {
      size(800, 800);
      smooth();
    
      b = new Button[5];
    
      for (int i =0; i<b.length; i++) {
        b[i]= new Button(i*width/b.length, 0, width/b.length, height/18);
      }
    }
    
    void draw() {
    
      background(0);
    
      for (int i =0; i<b.length; i++) {
        b[i].display();
      }
    }
    
    void mousePressed() {
      int currI=-1;
      for (int i =0; i<b.length; i++) {
        if ( b[i].interaction() ) 
          currI=i;
      }
      // when we had a result 
      if ( currI != -1) {
        // we switch all others (!=currI) off
        for (int i =0; i<b.length; i++) {
          if (i!=currI)
            b[i].status=false;
        }
      }
    }
    
    //////////////////////////////
    
    class Button {
    
      float posX, posY, 
        bWidth, bHeight;
      boolean status;
    
      // constr
      Button(float _x, float _y, 
        float _bWidth, float _bHeight) {
    
        posX = _x;
        posY = _y;
        bWidth = _bWidth;
        bHeight = _bHeight;
      }// constr
    
      boolean interaction() {
        boolean result=false; 
        if (mouseX>posX&&mouseX<posX+bWidth&&
          mouseY>posY&&mouseY<posY+bHeight) {
          if (status) {
            status = false;
          } else { 
            status = true;
            result=true;
          }
        }//if
        return result;
      }//method
    
      void display() {
        //noStroke();
        stroke(111);
    
        if (status) { 
          fill(45, 45, 200);
        } else {
          fill(45);
        }
    
        rect(posX, posY, bWidth, bHeight);
      }//method
    }//class
    //
    
  • @Chrisir I was about to mention this :) but you did it already awesome

    Many thanks :)

  • You're welcome!

    ;-)

  • I'd be tempted to write a radio button set class which took a bunch of buttons and made sure that only one of them was chosen at any given time. (It's not much more than an ArrayList)

Sign In or Register to comment.