Code

void setup() {
  size (400,400);
  background(#333333);
  fill(0);
}
// set up my variable
boolean state = false;

void loop() {
  // if the box is off, turn the color black
  if (!state) fill(0);
  else fill(255);

  // draw the box
  rect(150,150,100,100);

}

void mousePressed(){
  // swap
  if (get(mouseX,mouseY)!=color(51,51,51)) {
    if (!state) state = true;
    else state = false;
  }
}

020 -- Flipping Square I: State Machines

Statement:Assignments numbered 02x will be due February 1.

This exercise tests your ability to maintain system state in some sort of variable, and also asks you to devise a way of testing the containment of a point (the cursor) within a special region (a reactive square).

In a 400x400 canvas, create a 100x100 pixel reactive square, such that each click in the square flips its color from white to black (if it is white), or from black to white (if it is black). Mouse clicks outside the square should have no effect! This example may be helpful to you.

hide statement