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.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

386<br />

First, I need to create the Door object; in OOP this is called instantiation. Then I optionally<br />

set its knob position. Notice how I used the static variable LFT, preceded by the class name<br />

<strong>and</strong> a dot. Finally, the door is drawn. Look carefully at the syntax—the door object door1<br />

is attached directly to its method with a dot. If you eventually wanted to change the value<br />

of one of the properties, you’d target it the same way. For example, to change the x property<br />

to 194, you’d write: door1.x = 194. Of course, this comm<strong>and</strong> would have to come<br />

after the Door object has been instantiated: door1 = new Door(100, 250);. Before you<br />

move on to the Window class, I strongly recommend you review the Door class a few times,<br />

as well as my description. I also suggest trying to make some more doors on your own. This<br />

little sketch covers a lot of important issues that will be built upon.<br />

Window class<br />

A window shares certain characteristics with a door. Both have an x <strong>and</strong> y position, as well<br />

as width <strong>and</strong> height. If this example were more detailed, both objects might also have<br />

color, material, price, <strong>and</strong> inventory number characteristics. There is a way of minimizing<br />

this redundancy between classes in OOP, called inheritance (covered extensively in<br />

Chapter 8), in which classes can inherit properties <strong>and</strong> methods from other classes.<br />

However, inheritance adds another level of abstraction <strong>and</strong> complexity, so for this example<br />

I’ll redundantly create the same properties in the different classes. Later on in the book,<br />

when you’re an advanced object-oriented programmer, you’ll apply inheritance. Here’s a<br />

sketch that draws three windows using the new Window class (see Figure 9-32):<br />

// Drawing Some Windows<br />

void setup(){<br />

size(450, 250);<br />

background(200);<br />

smooth();<br />

Window window1 = new Window(100, 150);<br />

window1.drawWindow(50, 50);<br />

Window window2 = new Window(100, 150, true, Window.DOUBLE);<br />

window2.drawWindow(175, 50);<br />

Window window3 = new Window(100, 150, true, Window.QUAD);<br />

window3.drawWindow(300, 50);<br />

}<br />

class Window{<br />

//window properties<br />

int x;<br />

int y;<br />

int w;<br />

int h;<br />

// customized features<br />

boolean hasSash = false;

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

Saved successfully!

Ooh no, something went wrong!