06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

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.

432 <strong>Learning</strong> <strong>Processing</strong><br />

23.5<br />

Other Useful Java Classes: Rectangle<br />

Th e second helpful Java class we will examine is the Rectangle class:<br />

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Rectangle.html .<br />

A Java Rectangle specifi es an area in a coordinate space that is enclosed by the Rectangle object’s <strong>to</strong>p-left point<br />

( x , y ), its width, and its height. Sound familiar? Of course, the <strong>Processing</strong> function rect( ) draws a rectangle with<br />

precisely the same parameters. Th e Rectangle class encapsulates the idea of a rectangle in<strong>to</strong> an object.<br />

A Java Rectangle has useful methods, for instance, contains( ). contains( ) off ers a simple way of checking if<br />

a point or rectangle is located inside that rectangle, by receiving an x and y coordinate and returning true<br />

or false, based on whether the point is inside the rectangle or not.<br />

Here is a simple rollover implemented with a Rectangle object and contains( ) . See Example 23.3 .<br />

Example 23-3: Using a java.awt.Rectangle object<br />

Rectangle rect1, rect2;<br />

void setup() {<br />

size(200,200);<br />

rect1 = new Rectangle(25,25,50,50);<br />

rect2 = new Rectangle(125,75,50,75);<br />

}<br />

void draw() {<br />

background(255);<br />

stroke(0);<br />

if (rect1.contains(mouseX,mouseY)) {<br />

fill(200);<br />

} else {<br />

fill(100);<br />

}<br />

The contains() function is used <strong>to</strong> determine<br />

if the mouse is located inside the rectangle.<br />

rect(rect1.x, rect1.y, rect1.width,rect1.height);<br />

This sketch uses two<br />

Rectangle objects.<br />

The arguments for<br />

the construc<strong>to</strong>r<br />

(x,y,width,height) are<br />

documented in the Java<br />

reference: http://java.sun.<br />

com/j2se/1.4.2/docs/api/<br />

java/awt/Rectangle.html.<br />

// Repeat for the second Rectangle<br />

// (of course, we could use an array or ArrayList here!)<br />

if (rect2.contains(mouseX,mouseY)) {<br />

fill(200);<br />

} else {<br />

fill(100);<br />

}<br />

rect(rect2.x, rect2.y, rect2.width,rect2.height);<br />

}<br />

fi g. 23.5<br />

A Rectangle object only<br />

knows about the variables<br />

associated with a rectangle.<br />

It cannot display one. So<br />

we use <strong>Processing</strong>’s rect()<br />

function in combination with<br />

the Rectangle’s data.<br />

Let’s have some fun and combine the ArrayList “ particle ” example with the Rectangle “ rollover. ” In<br />

Example 23-4, particles are made every frame and pulled down by gravity <strong>to</strong>ward the bot<strong>to</strong>m of the<br />

window. If they run in<strong>to</strong> a rectangle, however, they are caught. Th e particles are s<strong>to</strong>red in an ArrayList<br />

and a Rectangle object determines if they have been caught or not. (Th is example contains answers <strong>to</strong><br />

Exercise 23-2.)

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

Saved successfully!

Ooh no, something went wrong!