Highlighting Buttons Individually

edited October 2014 in How To...

Dear Processing forum,

My name is Anton Cujec. I'm a 12th grader at High Tech High Media Arts, a project based high school. In my digital arts class, we are using processing to create a project that will reflect research we've collected on an unfamiliar landscape. I have chosen the unfamiliar landscape of the drought in California. One of the main parts of my project is a survey that asks questions that will allow me to calculate about how much water they use every day. One small but key part of my project is creating the answer choices for each question. I am currently trying to make it so that when you click on an answer choice, that specific choice will change from a white box with black text, to a black box with white text. The issue is that when I click on an answer choice, the color inverts for all of my answer choices. I am currently using a class to set up my answer choices, as I will have over 70 and am looking for an efficient way to get this to work. My code can be found here!

If you have any other suggestions, please let me know!

Thanks in advance. :)

Tagged:

Answers

  • edited October 2014 Answer ✓

    I was unable to download the code. I clicked on the blue zip file link, then on the next page I clicked on Download and I got the error 415 message i.e.

    1. That’s an error.

    The requested URL was not found on this server.

  • edited October 2014

    Thank you for taking a look! I really appreciate it.

    I'm sorry the link isn't working. I tried it and got the same thing. I'm working on getting it onto dropbox now.

  • Sorry it took so long. After trying three different websites, I finally had success getting it on dropbox. The link to it is here. Let me know if there are any more problems. Thanks again for the help!

  • download works now

    I gather that when you press another button in the same line of buttons, the first is off again and the latter is highlighted

    how is that reflected in your code...?

  • At this point, it's not. We're having trouble getting them to turn on individually right now. The line of code that is in charge of changing the colors is on the page titled "Objects" in the class labeled "Answers". Also, I didn't know that accepting an answer would mark the discussion as "answered". Is there a way to undo that?

    Thanks.

  • please write a personal message to philho to undo it

  • Actually, the Vanilla plugin managing the Answered state of topics is badly made: not only it nags users for a choice, but it allows nobody, not even moderators / administrators, to remove an "answered" state.

  • edited November 2014

    briefly, your code is to complex too debug it quickly

    you have one object Answer.

    That is wrong, because one answer represents one line of check-boxes

    therefore I suggest have an array of Answer, one Answer for each line

    when you have different pages, have on page 1 e.g. answer 1 and 2, on page 2 answer 3, 4, and 5 and so on ....

  • also within class Answer:

    you have 10 checkboxes so you need to mouse check 10 times (or in a for-loop) but not only once

    and what's this...

     if (buttonSix>0) {
        secretbutton = -1;
        }
    
  • edited November 2014 Answer ✓

    experiment

            int [] x = new int [11];
            int hl=0;
            void setup() {
              size(816, 900);
            }
            void draw() {
              background(200);
              if (mouseX >180 && mouseX<480 && mouseY>0 && mouseY<40) {
                hl =-6+mouseX/30;
              } 
              for (int i =0;i<10;i++) {
                if (x[i]==1 ) {
                  fill(0);
                }
                else {
                  fill(255);
                } 
                rect (180+30*i, 2, 25, 20);
              }
    
              for (int i =0;i<10;i++) {
                if (x[i]==1 ) {
                  fill(255);
                }
                else {
                  fill(0);
                } 
                text (1+1*i, 188+30*i, 17);
              }
            }  
            void mousePressed() { 
              if (mouseX >180 && mouseX<480 && mouseY>0 && mouseY<40) {
                if (x[hl]==0 ) {
                  x[hl]=1;
                }
                else {
                  x[hl]=0;
                }
              }
            }
    
  • Answer ✓

    another test...........

    // one question
    ArrayList<LineOfButtons> questions = new ArrayList();
    
    void setup() {
      size (900, 400);
      questions.add ( new LineOfButtons(40, 40));
      questions.add ( new LineOfButtons(40, 110));
      questions.add ( new LineOfButtons(40, 180));
    }
    
    void draw() {
      background(0);
    
      for (LineOfButtons q1 : questions) {
        q1.display();
      }
    }
    
    void mousePressed() {
      for (LineOfButtons q1 : questions) {
        q1.checkMouse();
      }
    }
    
    // =============================================
    
    class LineOfButtons {
      //
      float x;
      float y;
    
      String text1 = "Question ?";
    
      ArrayList<Button> buttons = new ArrayList();
    
      LineOfButtons ( float x_, float y_ ) {
        x=x_;
        y=y_;
        for (int i = 0; i < 6; i++) {
          buttons.add ( new  Button( x*i+30, y ));
        }
      }
    
      void display() {
        fill(255);
        text (text1, x-10, y-10);
        for (Button b1 : buttons) {
          b1.display();
        }
      }
    
      void checkMouse() {
        Button b2=null;
        boolean foundOne = false; 
        for (Button b1 : buttons) {
          if (b1.checkMouse()) {
            b1.isOn=true;
            b2=b1; 
            foundOne=true;
          }//if
        }//for
        if (foundOne) {
          for (Button b1 : buttons) {
            b1.isOn=false;
          }
          b2.isOn=true;
        } // if
      }// func
    }
    
    class Button {
      //
      float x;
      float y;
      float sizeButton = 10; 
      boolean isOn = false;  
      Button ( float x_, float y_ ) {
        x=x_;
        y=y_;
      }
    
      void display() {
        if (isOn) {
          fill(255);
        }
        else {
          fill(0);
          stroke(255);
        }
        rect(x, y, sizeButton, sizeButton);
      }
      boolean checkMouse() {
        if (dist(mouseX, mouseY, 
        x+sizeButton/2, y+sizeButton/2) < 20)
          return true;
        else
          return false;
      }
    }
    
    //
    
  • edited November 2014

    Thank you all so much for your help! I'm working now to work these examples into my code. I'll post if I have any follow up questions. Again, thank you all. The help is really appreciated!

Sign In or Register to comment.