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.

introduced to become larger, more complex, <strong>and</strong> more independent entities. OOP facilitates<br />

code reuse—which will become more important as you get more experienced—<strong>and</strong><br />

enforces certain programming rules. OOP is a much more complex approach to programming<br />

that delves into very interesting but quite challenging abstractions. I will cover OOP<br />

in Chapter 8, although I include a brief overview of it in the next chapter, which focuses on<br />

code grammar. OOP is such a big subject that it would be worthwhile to devote an entire<br />

book to creative OOP with processing—maybe if I’m still st<strong>and</strong>ing when I’m done writing<br />

this one . . .<br />

If you’re interested, here is a <strong>Processing</strong> OOP version of the little rectangle program, with<br />

some comments—the descriptions following the two forward slashes (//). I am not going<br />

to do a full analysis of the program here, as I’ll be covering these concepts in painstaking<br />

detail later in the book.<br />

// <strong>Processing</strong> Object-Oriented approach: Create a rectangle<br />

void setup(){<br />

// set display window to 400 x 400<br />

size(400, 400);<br />

// set background color to light gray<br />

background(225);<br />

//instantiate a MyRect object <strong>and</strong> assign it to the reference ➥<br />

variable rectangle1<br />

MyRect rectangle1 = new MyRect(width/8, height/8, width/4, ➥<br />

height/4, 200, -1);<br />

/* call drawRect() method using MyRect object reference, rectangle1<br />

calls the method using dot syntax (literally<br />

the period between the object <strong>and</strong> the method call).*/<br />

rectangle1.drawRect();<br />

}<br />

// MyRect class definition<br />

class MyRect {<br />

// public instance fields - each MyRect object will have<br />

// their own set of these properties<br />

float x, y, wdth, ht;<br />

int fillCol, strokeCol;<br />

/*constructor - called when the object is instantiated, using the new<br />

operator. the parameter list between the parentheses needs to match<br />

the argument list in the instantiation call */<br />

MyRect(float x, float y, float wdth, float ht, int fillCol, ➥<br />

int strokeCol){<br />

// initialize instance fields - assignment happens form<br />

// right to left, e.g. the value of x is assigned to the<br />

// instance property this.x<br />

this.x = x;<br />

CREATIVE CODING<br />

35<br />

2

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

Saved successfully!

Ooh no, something went wrong!