
Syntax introduced: mouseX mouseY pmouseX pmouseY mouseClicked() keyPressed boolean else
Another fun thing to do is write programs that involve interactivity via the keyboard and the mouse. Processing stores the position of the cursor as long as the cursor is inside the display window. It stores that position inside the variables mouseX and mouseY. It also stores the most recent previous position of the cursor in the variables pmouseX and pmouseY. You can take advantage of this by telling your program to do various things depending on where the cursor is.
Example 4.0: Interactivity via the mouse
This program draws a square and circle to the screen. The circle follows the cursor around the display window. Both the circle and square change color depending on the cursor's position.
color bg = color ( 220 , 220 , 200 ); void setup () { background (bg); size ( 200 , 200 ); } void draw () { strokeWeight ( 1 ); fill ( 48 , 107 , 198 , 100 ); if ( mouseX >= 100 && mouseY < 100 ) { fill ( 242 , 99 , 42 , 100 ); } if ( mouseX < 100 && mouseY >= 100 ) { fill ( 146 , 21 , 175 , 100 ); } rect ( 40 , 40 , 40 , 40 ); ellipse ( mouseX , mouseY , 50 , 50 ); } |


Click here for a transcript of the video of Example 4.0.
Example 4.1: More things you can do with the mouse
Below is another example program that uses mouseClicked(). mouseClicked() is a separate function written in its own block after draw(). In this program, clicking inside one of the squares will make the other square change color.
// Click within a square to change // the color of the other square. color value = color ( 255 , 0 , 0 ); color value2 = color ( 0 , 255 , 0 ); void setup () { size ( 200 , 200 ); background ( 255 ); } void draw () { fill (value); rect ( 25 , 25 , 50 , 50 ); fill (value2); rect ( 75 , 75 , 50 , 50 ); } // The mouseClicked function is called after a mouse button is pressed // and then released. void mouseClicked() { //if the cursor is inside the top square and the color of the bottom //square is green . . . if ( mouseX < 75 && mouseY < 75 && mouseX > 25 && mouseY > 25 && value2 == color ( 0 , 255 , 0 )) { value2 = color ( 0 , 255 , 255 ); } // If the cursor is inside the top square and the color of the bottom // square is aqua . . . else if ( mouseX < 75 && mouseY < 75 && mouseX > 25 && mouseY > 25 && value2 == color ( 0 , 255 , 255 )) { value2 = color ( 0 , 255 , 0 ); } //if the cursor is inside the bottom square and the color of the top //square is red . . . else if ( mouseX < 125 && mouseY < 125 && mouseX > 75 && mouseY > 75 && value == color ( 255 , 0 , 0 )) { value = color ( 255 , 0 , 255 ); } //if the cursor is inside the bottom square and the color of the top //square is pink . . . else if ( mouseX < 125 && mouseY < 125 && mouseX > 75 && mouseY > 75 && value == color ( 255 , 0 , 255 )){ value = color ( 255 , 0 , 0 ); } //tell me if I haven't satisfied any of the above possibilities else { println ( "You have to click inside a square for something to happen!" ); } } |
Review of if - else if - else
You already saw an example of the if-else structure back in Lesson 3, but I'm going to repeat the explanation here anyway. Bare bones it looks like:
if else if else if else if else |
inside the mouseClicked() function.
The way it works is like this:
if (test is true ) { do something; } else if (first test was false , then test if this test is true and if it is) { do some other thing; } else if (first two tests were false , test this one) { do some other thing; } else if (first three tests were falst, test this one) { do some other thing; } else { none of the above possibilities were true , so do this by default ; } |
You can write as many else ifs as you want to in between the first if and the final else. The final else does not include a parenthetical test because it is there to mop up. It will execute its commands by default when none of the tests above it have been satisfied.
Screenshot of the text of the program and a still from the program running in which clicking the mouse in certain regions changes the fill color of the squares.

Click here for a transcript of the Screencast.
Your turn to play around with mouse inputs:
Exercises
4.1 Draw a series of points in a trail following the mouse position.
4.2 Draw a shape that follows the mouse but does not leave a trail.
4.3 Draw a shape that follows the mouse but in reverse (reverse x and y or reverse the response to changes in directions of x and y)
4.4 Draw a shape that changes size depending on the mouse position.
4.5 Draw three different shapes of three different colors to the screen and have them move in an interesting relationship to the mouse when the mouse is moved, and have them change color when the mouse is clicked.