Transcript
Clicking and Pressing
Click the Mouse
Do this
Answer the multiple choice questions below.
Which of the following in NOT a possible value for a boolean variable?
What does the program above do
Educator notes
mousePressed
is a boolean variable that can be used to track things that are either true or false. It’s possible to place a boolean variable into if-test directly without the double equals -sign (==), like:
if(mousePressed){
line(pmouseX, pmouseY, mouseX, mouseY);
}
Students are taught to use the double equals -sign because it’s easier to understand the relation to the tests with integer-variables in this way.
Press the Keys
Do this
Copy the program in the video from below.
Choose your own ranges for drawing color and stroke weight.
Give your creation a name and save it (File -> Save As)
void setup() {
size(500, 500);
fill(0);
rect(0, 0, 500, 500);
strokeWeight(5);
}
void draw() {
if (keyPressed == true) {
// The program selects a new random color and stroke weight when any key is pressed.
// Choose your own color-ranges (Tools > Color Selector can help)
stroke(random(0, 255), random(0, 255), random(0, 255));
// Choose your own range for stroke weight. Try big values too (e.g. 150).
strokeWeight(random(1, 30));
}
if (mousePressed == true) {
line(pmouseX, pmouseY, mouseX, mouseY);
}
println(mousePressed);
}
Educator notes
Some students might want to know how to check if some specific key has been pressed, like ENTER, UP or letter 'A' for example. It's possible to achieve this by using the key-variable or the keyCode-variable in Processing. The key-variable works best with the letters and numbers of the keyboard while the keyCode works best with keys like SHIFT, UP and DOWN. Try the following example, where Processing prints the key and the keyCode of the pressed key. Press different keys and see what is printed to the console!
void setup() {
}
void draw(){
if(keyPressed == true){
println("Key: " + key);
println("keyCode: "+ keyCode);
}
}
When you know the key or keyCode of the button, you can refer to it in the if-statement. The following code draws an ellipse to the mouse-location when the letter 'v' is pressed (at least on most keyboards). keyCodes work without quotation marks, but you have to use them when referring to keys (eg. 'v').
void setup() {
}
void draw(){
if(keyPressed == true){
if(key == 'v'){
ellipse(mouseX, mouseY, 100, 100);
}
}
}
The Color Palette
Do this
Copy the code in the video from below.
In the setup, draw a green button below the red- and blue buttons.
In the draw, add an else if -statement that changes the color to green if mouse is pressed on the green button.
void setup() {
size(500, 500);
strokeWeight(5);
fill(255, 0, 0);
rect(0, 0, 100, 100);
fill(0, 0, 255);
rect(0, 100, 100, 100);
// Add a green button below the red and blue buttons
}
void draw(){
if(mousePressed == true){
if(mouseX < 100 && mouseY < 100){
stroke(255, 0, 0);
}
else if(mouseX < 100 && mouseY < 200){
stroke(0, 0, 255);
}
// Add here an else if statement that checks if the mouse has been pressed on the green button.
// If the mouse has been pressed on the green button, change the color to green.
else{
line(pmouseX, pmouseY, mouseX, mouseY);
}
}
}
Educator notes
Conditional statements (if, else if and else) can sometimes make your brain hurt but don’t worry, it gets clearer with practice! To get the hang of it, see the code-example below where the green button is added to the program. The comments explain the code.
void setup() {
size(500, 500);
strokeWeight(5);
fill(255, 0, 0);
rect(0, 0, 100, 100);
fill(0, 0, 255);
rect(0, 100, 100, 100);
// The green button
fill(0, 255, 0);
rect(0, 200, 100, 100);
}
void draw(){
// First, the computer checks if the mouse has been pressed.
// The rest of the conditional statements are 'nested' inside this first if-statement, so the computer checks them only if mouse is pressed.
// If the mouse is not pressed, the program doesn't do anything!
if(mousePressed == true){
// If the mouse is on the red button, change the color of the stroke to red.
if(mouseX < 100 && mouseY < 100){
stroke(255, 0, 0);
}
// If the mouse is not on the red button, the computer checks if it is on the blue button.
// If it is, it changes the color of the stroke to blue.
else if(mouseX < 100 && mouseY < 200){
stroke(0, 0, 255);
}
// If the mouse is not on the red or blue buttons, the computer checks if it is on the green button.
// If it is, it changes the color of the stroke to green.
else if(mouseX < 100 && mouseY < 300){
stroke(0, 255, 0);
}
// If the mouse is not on any of the buttons, the program draws the line.
else{
line(pmouseX, pmouseY, mouseX, mouseY);
}
}
}