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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

478<br />

The calling PDE code should be old hat by now. I simply created a bunch of color <strong>and</strong><br />

Rectangle objects <strong>and</strong> passed them to the respective gradient constructors. Both the<br />

LinearGradient <strong>and</strong> RadialGradient default constructors fill the entire screen with a<br />

black-<strong>and</strong>-white linear or radial gradient, respectively. I used the former to create a background<br />

gradient. Notice the Gradient.AXIS_HORIZONTAL argument in two of the<br />

LinearGradient instantiation calls. This is a st<strong>and</strong>ard use of a constant. I used the class<br />

name to preface the constant name, as you’ll remember that the constant was declared as<br />

a static property in the Gradient class.<br />

In running this example, you have two options: you can type all the code (the three classes<br />

<strong>and</strong> the main PDE code just shown) into <strong>Processing</strong>, or you can download the complete<br />

code from www.friendsofed.com/downloads.<br />

Organizing classes using multiple tabs<br />

Once you’ve got all the code, there are two ways you can organize it in <strong>Processing</strong>. The<br />

first <strong>and</strong> simplest way is to add all the code (including the classes) directly into the main<br />

(leftmost) tab. This is the approach you’ve been using thus far in the book. A better <strong>and</strong><br />

more organized way is to enter only the PDE code in the main tab <strong>and</strong> then create new<br />

tabs for each of the three classes. Assuming that you have all your code pasted in the main<br />

tab, here are the steps to reorganize it across multiple tabs.<br />

Select all the code of the Gradient class <strong>and</strong> cut it—you can either use the Copy comm<strong>and</strong><br />

under the Edit menu or the common shortcut Cmd+C (on the Mac)/Ctrl+C<br />

(on Windows). Then click the tabs arrow at the right side of the <strong>Processing</strong> window, <strong>and</strong><br />

select New tab. Give the file the same name of the class (Gradient). When the new tab<br />

opens, paste the Gradient class code into the window. Repeat this process for the<br />

LinearGradient <strong>and</strong> RadialGradient classes. When you have the classes safely pasted<br />

into their own tabs, delete the class code in the main tab (the leftmost one), but make<br />

sure you keep the remaining PDE code (including the setup() function). You should be<br />

able to run the sketch now.<br />

One final note on the tabs: when you name the individual tab documents, you don’t need<br />

to include the .pde suffix, as <strong>Processing</strong> does it automatically. If you do include it, that’s<br />

OK as well, as <strong>Processing</strong> is smart <strong>and</strong> will not append a second suffix. You can also create<br />

separate tabs for functions, following the same procedure I just outlined.<br />

Summary<br />

This has been a big chapter with some challenging—but also hopefully interesting—<br />

information <strong>and</strong> snippets. It began with a look at basic color theory, which you applied<br />

using <strong>Processing</strong>’s built-in color capabilities, including selecting between color models <strong>and</strong><br />

working with <strong>Processing</strong>’s color data type. I discussed the Java graphics context <strong>and</strong> its<br />

connection to how painting occurs in <strong>Processing</strong> <strong>and</strong> Java, <strong>and</strong> I briefly discussed<br />

<strong>Processing</strong>’s sophisticated matrices functions <strong>and</strong> how to specifically push <strong>and</strong> pop translations,<br />

as well as reset a matrix with the identity matrix.

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

Saved successfully!

Ooh no, something went wrong!