CS 171 & 171L Introduction to Computer Science I & Lab Fall 2020 

Homework Assignment 2

Send me (dreimann@albion.edu) the following in a .java file as an attachment to an email message with Homework 2 as the subject line.

(30 points) Modify your program from Homework 1 so that the user can select several choices for facial features as follows. Adjust your original drawing if needed to demonstrate these additional features.

Define four different mouth styles: smile, flat, frown, round. Allow the user to cycle through these be pressing 'm' on the keyboard.

Define three different eye styles: narrow, normal, wide. Allow the user to cycle through these be pressing 'e' on the keyboard.

Define three different eyebrow styles: up, flat, down. Allow the user to cycle through these be pressing 'b' on the keyboard.

For each of these styles, use global variables to keep track of the current style.

Example

Pressing the s key in the graphics window above will cause the state variable to change. You may need to click on the window before the key press will be recognized. Pressing the s key invokes the keyPressed method. That method increments the state vaiable by 1. Then it it checks the value to see if the value is greater than or equal to 2 and then resets it back to 0 if needed. This will limit the values of state to be either 0 or 1.


int state = 0; // keep track of what we want to draw

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


void draw() {
  background(0);
  strokeWeight(5);
  if (state == 0) {
    stroke(255,0,0);
    line(0,0,width,height);
  }
  else if (state == 1) {
    stroke(255,0,0);
    line(width,0,0,height);
  }
}

// an event handler called by pressing a key
// never call this explicitly in your program
// don't draw anything in keyPressed!
void keyPressed() {
  if (key=='s') {
    state++; // increment state by 1
    if (state >= 2) state = 0;
  }
}

Other information

You must use comments in your program. At the top, put your name and assignment number in a comment. Label each major section with a comment. Use whitespace (a blank line or two) to also separate section of your code to improve its readability.

Have fun!


Copyright © 2020, David A. Reimann. All rights reserved.