04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

public void mouseClicked(MouseEvent e){}<br />

public void mouseEntered(MouseEvent e){}<br />

public void mouseExited(MouseEvent e){}<br />

public void mouseReleased(MouseEvent e){}<br />

}<br />

// Java approach 2 extends the Java MouseAdapter class<br />

void setup(){<br />

new MouseEventDemo();<br />

}<br />

class MouseEventDemo extends MouseAdapter {<br />

//constructor<br />

MouseEventDemo(){<br />

addMouseListener(this);<br />

}<br />

public void mousePressed(MouseEvent e) {<br />

println("Java MouseAdapter class example: mouse press detected");<br />

}<br />

}<br />

// <strong>Processing</strong> "easy" approach<br />

void draw(){} // we're required to include draw() function<br />

void mousePressed() {<br />

println("<strong>Processing</strong> example: mouse press detected");<br />

}<br />

The two Java approaches are both more complex <strong>and</strong> require more code than the<br />

<strong>Processing</strong> approach. Using Java’s MouseAdapter class simplifies things somewhat, as the<br />

MouseAdapter class encapsulates the necessary implementation of all the required<br />

MouseListener methods. The <strong>Processing</strong> approach takes this concept of encapsulation<br />

one step further, making it unnecessary to explicitly implement or extend another mouse<br />

event–related interface or class. In reality, the <strong>Processing</strong> approach I wrote is a little too<br />

lean, as you’d almost always want to include <strong>Processing</strong>’s setup() function as well as<br />

draw(). However, all that is technically required for the mouse event detection to work in<br />

<strong>Processing</strong> is the inclusion of the draw() function.<br />

Mouse events<br />

Besides mouse press detection, <strong>Processing</strong> also includes event detection for mouse<br />

releases, clicks, moves, <strong>and</strong> drags. Mouse release detection occurs whenever the mouse<br />

button is released, regardless if the mouse position changes while the mouse is pressed. In<br />

comparison, mouse click detection occurs only if the mouse is released at the same point<br />

it was pressed. When the mouse is pressed <strong>and</strong> released at the same point, both release<br />

<strong>and</strong> then click detection occur—always in this order. Mouse move detection occurs when<br />

the mouse moves, without the mouse button being pressed, while mouse dragging detection<br />

occurs while the mouse moves with the button pressed. In addition, <strong>Processing</strong> can<br />

INTERACTIVITY<br />

565<br />

12

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!