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 />

646<br />

contains this array. The SpinnyCube constructor accepts the same three arguments as the<br />

Cube class, for the width, height, <strong>and</strong> depth of the cube. The first line of the SpinnyCube<br />

constructor<br />

super(w, h, d);<br />

passes the arguments through to the superclass (Cube) constructor. This is a very efficient<br />

structure that allows you to build upon base <strong>and</strong> component classes. One little reminder—<br />

if there had been any other code in the SpinnyCube constructor, it would need to be put<br />

after the super() call, which (when included) must always be the first line in the subclass<br />

constructor. (However, you are not required to include a call to super() in a subclass constructor.)<br />

I recommend always adding a default no-argument constructor to any class that will<br />

be extended, since the superclass constructor is always invoked, even without an<br />

explicit call to super(). To learn more about this <strong>and</strong> related (albeit geeky <strong>and</strong> lowlevel)<br />

issues, check out www.beginner-java-tutorial.com/ <strong>and</strong> http://java.sun.<br />

com/docs/books/jls/second_edition/html/classes.doc.html.<br />

Following the constructors is the spinnyRotateXYZ() method. This method works practically<br />

identically to the rotateVertices() function in the previous example. To keep things<br />

organized, I declared <strong>and</strong> initialized all the temporary vertices arrays locally within this<br />

method. As in the last example, the transformed vertices need to be plugged into the next<br />

set of expressions. Finally, at the end of the method, I assigned the updated vertices to the<br />

transformedVertices[] array used to draw the cube. Following this method are three<br />

simple utility methods (spinnyRotateX(), spinnyRotateY(), <strong>and</strong> spinnyRotateZ()) that<br />

update rotation angles, used by the expressions in the spinnyRotateXYZ() method.<br />

The last methods in the SpinnyCube class are two overloaded create() methods, just as in<br />

the Cube class. Creating two more methods with the same name may seem confusing <strong>and</strong><br />

unnecessary, since the subclass has access to the superclass’s create() methods. I wanted<br />

to call the spinnyRotate() method internally from within the create() methods, <strong>and</strong> I<br />

didn’t want to make that change to the cube’s original create() methods. Since the create()<br />

methods in the SpinnyCube subclass have the same method signatures (parameter<br />

lists) as the create() methods in the Cube superclass, the methods in the subclass will<br />

override the same named ones in the superclass. In other words, when I call<br />

c1.create(quadBG);<br />

<strong>and</strong> c1 is of type SpinnyCube, the create() method in the SpinnyCube subclass is executed,<br />

not the one with the identical signature in the Cube class. The SpinnyCube create()<br />

methods are implemented nearly identically to the ones in the Cube class, except that they<br />

have additional lines for calling the spinnyRotateXYZ() method.

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

Saved successfully!

Ooh no, something went wrong!