In this project you will use more mouse input variables, after which you will cover event functions. Event functions will launch you to the next level in Processing. Both of these abilities will allow you to create robust projects with less code. Finally, you'll learn to harness mouse clicks to create two simple drawing programs that work a lot more efficiently than what you've previously done.
You've already used a few of Processing's built in variables - but there are others including mousePressed and mouseButton. The mouseButton variable tells you which button (LEFT or RIGHT) was pressed.
These variables are handy when you only need to detect button presses, but as you're about to learn, event functions offer even more input-based functionality.
An event function executes only when a certain event happens. You implement your event function outside the basic draw() loop in your sketch, and when it is triggered by the event( i.e. the user input) it will run in parallel with draw() . This will reduce the number of if statements needed.
mouseClicked() the event triggers when the mouse button is clicked.
mouseDragged() the event triggers when the mouse moves while a button is held down.
mouseMoved() the event triggers when the mouse moves.
mousePressed() the event triggers when the mouse button is pressed.
mouseReleased() the event triggers when the mouse button is released.
When you want to use an event function, just create another function of the void type underneath your draw() function. A complete Processing program with an event function would follow this structure:
In this example I used only the mouseClicked() function, but you can add multiple event functions.
In this section, you will explore mouseDragged() and mousePressed() event functions to write a program that lets you create rainbow colored drawings.
First add the usual setup() and draw() sections, and then implement the mouseDragged() event function outside of your draw() loop as follows:
Inside mouseDragged(), sets a stroke weight, apply a random() stroke color and call the line() function to draw your line. Pass line() the previous and current mouse coordinates - (pmouseX, pmouseY) and (mouseX, mouseY) respectively - as its start and end points. You should have no code inside of your draw() because your line should be drawn only when you click and drag your mouse.
** You can write code saying if(mouseButton ==LEFT) ***
The rectangle and text will display the hardcoded color values, but how do you change the color on the fly? We'll put if() statements to good use to increment the color variables r , g, and b. Add the following code to your draw() loop.
if (key =='r') {
r++;
key = ' ';
if( r > 255) {
r = 0;
}
}
else if (key == 'g') {
g++;
key = ' ' ;
if( g >255) {
g = 0 ;
}
}
else if (key =='b') {
b++;
key = ' ' ;
if (b > 255) {
b =0;
}
}
To change the color of your pen, find the stroke() function in the mouseDragged() event function and modify it to use your new color variables.
void mouseDragged() {
if (mouseButton == LEFT) {
strokeWeight(50);
stroke(r,g,b);
line(pmouseX, pmouseY, mouseX, mouseY);
}
}