// Advanced if-else example from 2/20 class // Global constants: current shape and properties int RECTANGLE = 0; int ELLIPSE = 1; int WIDTH = 50; int HEIGHT = 50; // Global variables int currentShape = RECTANGLE; // Start with rectangles int currentStrokeWeight = 1; void setup () { size (400, 400); background (150); ellipseMode(CENTER); // Center ellipses on mouse position strokeWeight(currentStrokeWeight); smooth(); } void draw () { } void mousePressed () { // Use the value of currentShape to determine what // to draw this time around if (currentShape == 0) { rect (mouseX, mouseY, WIDTH, HEIGHT); } else // otherwise, draw an ellipse { ellipse(mouseX, mouseY, WIDTH, HEIGHT); } } void keyPressed () { // key is a system variable that holds the value of // the last key that was typed // If the user types 'c', clear the window // If the user types 'r', change shape to rectangle // If the user types 'e', change shape to ellipse // If the user types 'f', change the fill color // If the user types 's', change the stroke (line) color // If the user types 'w', change the stroke weight (width) // // Do nothing if the user hits any other key if (key == 'c') { // Clear the window background (150); } else if (key == 'r') { currentShape = 0; } else if (key == 'e') { currentShape = 1; } else if (key == 'f') { // Change fill to a random color fill(random(256), random(256), random(256)); } else if (key == 's') { // Change the stroke color to a random color stroke(random(256), random(256), random(256)); } else if (key == 'w') { // Increase the line weight by 2, but stay // within the range 0-16 int newStrokeWeight = currentStrokeWeight + 2; currentStrokeWeight = newStrokeWeight % 17; strokeWeight(currentStrokeWeight); } }