CSc 101: Section 7 – events and if-statements

In today’s section, we’ll continue talking about how to handle input device events, as well as if-statements.

Problem 1

Write a processing program that mimicks the behavior of this processing canvas. Notice that blue circles are drawn and "follow" the mouse position. However, the circles are only drawn in the top-half of the canvas. Use an if-statement to accomplish this.


Problem 2

Next, you'll modify your code from problem 1 to mimick the behavior shown in this next processing canvas recording. Notice that whenever the mouse is in the top third of the screen, blue circles are drawn with the mouse. In the middle third, green squares, and in the bottom third, orange circles. You can accomplish this using a few if-statements.


Problem 3

Write another processing program that mimicks the behavior of the processing canvas to the left. Notice a few differences from problem 2: First, connected lines are drawn instead of just circles or squares. Second, the lines only draw when the mouse is actually pressed down and moved around. Also, notice how the color of the brush changes. Imagine a line cut across the canvas from the top-left corner to the bottom-right. All of the drawing in the lower sector is drawn orange/red, and the drawing in the top sector is blue. How could you use an if-statement to achaive this effect?


Problem 4

Write another processing program that mimicks the behavior of the processing canvas to the left. When the 'e' button is pressed, an ellipse is drawn on the screen and then dissapears when the key is let-go. When the 'r' button pressed, draw a rectangle (or square) instead. When the 't' button pressed, draw a triangle instead.


Download Solutions