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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

void setup(){<br />

// create new rectangle object<br />

new Rectangle();<br />

}<br />

// class description<br />

class Rectangle {<br />

}<br />

This is all it takes to create a class in <strong>Processing</strong>. Of course, this class won’t do anything, but<br />

it’s a start. To create a class, you use the class keyword followed by the name of the class.<br />

You capitalize the name of your classes. To create an object from the Rectangle class, you<br />

use the new keyword. Typically, you also assign the new object to a variable so that you can<br />

refer to it later in your program. In the last example, even though I created a Rectangle<br />

object, I can’t communincate with it, as there is no variable assigned a reference to the<br />

object. Figure 9-29 shows an improved (<strong>and</strong> more useful) version of the last sketch:<br />

// object (reference) variable<br />

Rectangle r1;<br />

void setup(){<br />

// create new rectangle object<br />

r1 = new Rectangle(100, 200, 150, 150);<br />

}<br />

// class description<br />

class Rectangle {<br />

//class constructor<br />

Rectangle(int x, int y, int w, int h) {<br />

println("x pos = "+ x+"\ny pos = " + y);<br />

println("width = "+ w+"\nheight = " + h);<br />

}<br />

}<br />

Figure 9-29. OOP println() output example<br />

SHAPES<br />

379<br />

9

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

Saved successfully!

Ooh no, something went wrong!