Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 5-5: Button as switch
boolean button = false;

int x = 50;
int y = 50;
int w = 100;
int h = 75;

void setup() {
  size(200,200); 
}

void draw() {
  if (button) {
    background(255);
    stroke(0);
  } else {
    background(0);
    stroke(255);
  }
  
  fill(175);
  rect(x,y,w,h);
}

// When the mouse is pressed, the state of the button is toggled.   
// Try moving this code to draw() like in the rollover example.  What goes wrong?
void mousePressed() {
  if (mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) {
    button = !button;
  }  
}







  • http://www.facebook.com/stavros.sideris.7 Stavros Sideris

    How can i make it change into another color if i click again on the rectangle?

  • shiffman

    Make a variable for a color to put inside fill(). Then change the variable depending on state of button.

  • Juichiro

    Genius idea that one of using a set of conditions to turn on a boolean! It never occured to me. ;_;

  • Fatima

    “Try moving this code to draw() like in the rollover example. What goes wrong?” – I didn’t understand why it went wrong. Care to explain?

  • shiffman

    If you move it to draw() it will trigger over and over again (since draw() loops), changing the button state from true to false to true to false to true to false etc.

  • Guest

    int x;

    int y;

    int w;

    int h;

    boolean button;

    void setup(){

    size(200,200);

    x = 50;

    y = 50;

    w = 100;

    h = 75;

    }

    void draw(){

    background(0);

    if (mouseX > x && mouseX y && mouseY < y+h && button){

    background(255);

    }

    fill(155);

    rect(x,y,w,h);

    }

    void mousePressed(){

    button = !button;

    }

  • Ibrahim

    Thank you Sir.

  • tim

    How can I trigger a single value within a loop without it giving me a stream of values? For instance in the simple code below I only want a single random value generated each time x enters into this range.

    int x = 0;

    int value;

    void draw()

    {
    if((x > 200) && (x < 400))
    {
    value = random(100,1000);
    }
    x+=1;

    }

  • shiffman

    pick the random value in setup() and store it in a variable.

  • tim

    Thanks, I want it to give me a random value each time it crosses over this threshold actually. For instance, if I had an ellipse randomly moving across the screen and every time it enters a certain area it will generate a single random value and not a stream of values. Basically I am looking for a trigger.

  • shiffman

    if you use a boolean variable to keep track of whether it is just entering the range or whether it was previously already in the range that will do the trick.

  • tim

    Sorry if I am a bit slow with this but here is the code I am using to test my two types of triggers. objA_Trig works fine as it sends true when my object is within the range which simply changes the color depending on true or false. The problem lies with the other type of trigger represented by objA_Val. I want it to trigger a single float value as my object enters the range and a single value as it exits and not a stream of random values. I suppose similar to a collision detector. I have this value changing the size of the squares. The code I have a problem with is in void action

    boolean objA_Trig; //toggle trigger
    boolean objB_Trig;
    float objA_Val = 50; //button trigger
    float objB_Val = 50;
    float offset = 0;
    Mover[] obj = new Mover[2];

    void setup(){
    size(displayWidth /2, displayHeight );
    rectMode(CENTER);

    for( int i = 0; i < obj.length; i++){
    obj[i] = new Mover();
    }

    }

    void draw() {

    background(255);
    fill(100,100,100);
    rect(width/2 + offset,height/2 + offset,500,500);

    noFill();

    for( int i = 0; i width – size)||(position.x height – size * 2)||(position.y width/2 – 250) &&(position.x height/2 – 250) &&(position.y < height/2 + 250))
    {
    if (i == 0)
    {
    objA_Trig = true;
    objA_Val = random(10,100);
    }
    if (i == 1)
    {
    objB_Trig = true;
    objB_Val = random(10,100);
    }
    }
    else

    {
    if (i == 0)
    {
    objA_Trig = false;
    }

    if (i == 1)
    {
    objB_Trig = false;
    }

    }
    }
    }

  • ryan xD

    plsss help me ) How can i Change my color in facebook???