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.

Figure 6-24. End cap variation example<br />

Running this code should give you a clear sense of the differences between the end cap<br />

styles. These caps become a more significant concern as you begin to join lines into more<br />

complex structures. However, before you do that, let’s review some of the code in the last<br />

example.<br />

The line int[]caps = {ROUND, PROJECT, SQUARE}; declares <strong>and</strong> initializes an array of type<br />

int, <strong>and</strong> fills the index positions [0], [1], <strong>and</strong> [2] with the three constants ROUND,<br />

PROJECT, <strong>and</strong> SQUARE. I discussed arrays in detail in Chapter 3. As a review, arrays are data<br />

structures that hold multiple variables of any type. All the elements in an array need to be<br />

of the same data type. To access an item stored in an array, you use an index number<br />

placed between brackets, directly to the right of the array name, as in caps[0]. Arrays are<br />

zero-indexed, meaning that the first position in the array is 0, not 1. The three end cap<br />

styles, ROUND, PROJECT, <strong>and</strong> SQUARE, which look like strings, are actually immutable<br />

(unchangeable) integer constants. Specifying an unchanging value as a constant <strong>and</strong> using<br />

all uppercase letters in naming it are common programming approaches. In Java, the keyword<br />

final is used to specify that a variable’s value is immutable. To find the corresponding<br />

int values for the constants, run the following example:<br />

println("ROUND = " + ROUND);<br />

println("SQUARE = " + SQUARE);<br />

println("PROJECT = " + PROJECT);<br />

// uncomment the next line to generate a semantic error.<br />

//ROUND = 27;<br />

Now, to prove that you can’t change a constant’s value, uncomment the last line of the<br />

code <strong>and</strong> run the sketch again, which should generate a compiler error telling you can’t<br />

assign another value to ROUND.<br />

<strong>Processing</strong> has a ton of constants defined in the language that work the same way. Each<br />

constant is usually assigned an integer value (but not always). Mostly, constants add meaning<br />

to the coding process. Isn’t it easier to remember the cap style ROUND than the number<br />

2? You can see <strong>Processing</strong>’s constants at http://dev.processing.org/source/index.<br />

cgi/trunk/processing/core/src/processing/core/PConstants.java?view=markup.<br />

LINES<br />

199<br />

6

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

Saved successfully!

Ooh no, something went wrong!