/* states.pde Written by Justin Hsia Demonstrate creation of program "states" and triggers */ int stage = 0; // state variable telling what the program should be doing void setup() { size(500,500); // drawing canvas size stroke(0); // black lines strokeWeight(5); // make lines thicker rectMode(CENTER); // change rect mode to CENTER (to match ellipseMode) } void draw() { background(255); // white background // draws different shape on canvas based on stage variable if(stage == 0) { plus(width/2,height/2,width/2); } else if(stage == 1) { fill(255,0,0); // red circle rect(width/2,height/2,width/2,height/2); } else if(stage == 2) { fill(0,0,255); // blue square ellipse(width/2,height/2,width/2,height/2); } } // rotate through stages in a set order when mouse is clicked void mousePressed() { //stage = 1 - stage; // this only works for 2 stages (0,1) //stage = stage+1; //if(stage > 2) { // stage = 0; //} stage = (stage + 1) % 3; // this works for 3 stages (0,1,2) } // change stages arbitrarily based on keyboard presses: // 'c' for circle, 's' for square, 'p' for plus void keyPressed() { if(key == 'c') { // 'c' for circle stage = 2; } else if(key == 's') { // 's' for square stage = 1; } else if(key == 'p') { // 'p' for plus stage = 0; } } // function that draws a plus centered at (x,y) // with height and width of len void plus(int x, int y, int len) { line(x-len/2,y,x+len/2,y); line(x,y-len/2,x,y+len/2); }