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.

a Window object argument */<br />

House(Window win){<br />

this.win = win;<br />

}<br />

// other House methods would go here<br />

}<br />

From within <strong>Processing</strong>, I could use these classes like this (assuming, of course, that the<br />

two classes were pasted below the setup() function, or each in their own separate tab):<br />

void setup(){<br />

Window myWin = new Window(4, true, Window.ELLIPTICAL);<br />

House myHouse = new House(myWin);<br />

}<br />

OBJECT-ORIENTED PROGRAMMING<br />

Notice that I included three constants in the Window class. It is a coding convention to use<br />

all caps for constant names. One of the most common uses for constants is for preset<br />

values. In my example, I am giving a user of the Window class a choice of three different<br />

window configurations. I could have just forced the user to type in a number, but it is easier<br />

for most people to remember a name. In my Window class, I would eventually want to<br />

add a method that figures out what constant the user selected (which is just an int value),<br />

<strong>and</strong> then have that type of window created. You’ll notice in the <strong>Processing</strong> setup() function<br />

that I use the static ELLIPTICAL property by attaching it to the class name with a dot<br />

(Window.ELLIPTICAL). Static properties are class properties that are accessible from any<br />

class. There is no need to instantiate an object of the class to use them. Remember also<br />

that <strong>Processing</strong> <strong>and</strong> Java are case sensitive, so when using the class name, you need the initial<br />

capital.<br />

After the static properties, I declare three instance properties, which should hopefully look<br />

familiar by now. Next, in the Window constructor, I do my normal parameter-to-property<br />

assignments. I didn’t include any other methods in the class for the sake of brevity (as if<br />

that were still possible).<br />

The House class is very short, but gives a clear example of class composition. I only<br />

declared one instance variable, <strong>and</strong> you’ll notice it is of type Window. Now this may look<br />

pretty odd, as you normally declare a property or variable as a valid data type, built into<br />

the <strong>Processing</strong> <strong>and</strong> Java core languages. Well, it so happens that any class you create is a<br />

valid data type. The subtlety of this concept took me some time to fully grasp. Just remember<br />

that a class is a data type. So it is perfectly valid to declare a property of a class type.<br />

In the House constructor, there is a parameter of type Window that requires that an argument<br />

of type Window be passed in when a House object is instantiated. Within the House<br />

constructor, I do a st<strong>and</strong>ard assignment, using the parameter <strong>and</strong> property—both of type<br />

Window. The really cool thing is that now I can communicate directly with the Window<br />

object from within the House class, through its internal reference passed into the House<br />

constructor. Of course, if I were to finish the class, I would want to include additional<br />

properties <strong>and</strong> methods, allowing the Window object to be placed <strong>and</strong> sized within the<br />

house.<br />

325<br />

8

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

Saved successfully!

Ooh no, something went wrong!