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.

Figure 3-4. Continuous radial gradient<br />

If you don’t remember, the original stepped gradient example was about four times as<br />

long <strong>and</strong> not nearly as pretty.<br />

Finally, in the latest version of Java (1.5), as of this writing, there is a new variation on the<br />

for loop, called a for-each loop, or enhanced for loop. I am not going to cover it, as it is<br />

not currently supported in <strong>Processing</strong>. Information about it can be found on Sun’s site at<br />

http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html.<br />

<strong>Processing</strong> efficiency<br />

You already saw that arrays hold multiple values <strong>and</strong> loops allow you to repeatedly execute<br />

code. You can use these two structures together to form a powerful <strong>and</strong> highly efficient<br />

data processing system. As an example, here’s a simple mail merge program that<br />

outputs mailing addresses in the text area of the <strong>Processing</strong> application. The data is stored<br />

in six separate arrays, for first names, last names, street, city, state, <strong>and</strong> country.<br />

// mail merge program<br />

//create some arrays with address data<br />

String[]firstName = {"Ira ", "Sophie ", "Ian ", "Robin "};<br />

String[]lastName = {"Jed", "Rose", "Isaac", "McLennan"};<br />

String[]street = {"4 Happy Ln, ", "19 Hunan Pl, ", ➥<br />

"104 Hevi Hevi St, ", "1000 Donkey Dr, "};<br />

String[]city = {"Oxford", "Changsha", "Easton", "Edinboro"};<br />

String[]state = {"Ohio, ", "Hunan, ", "Pennsylvania, ", "Scotl<strong>and</strong>, "};<br />

String[]country = {"USA", "China", "USA", "UK"};<br />

// use a for loop to generate merge<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!