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

474<br />

LinearGradient subclass. The subclass can utilize the (accessible) properties <strong>and</strong> methods<br />

in the superclass, <strong>and</strong> they can be referred to directly, as if they were declared directly<br />

within the subclass; any subclass of Gradient would have this capability. You can see how<br />

building a class library using inheritance can create efficiencies <strong>and</strong> ultimately speed development—not<br />

to mention create more consistency for the user.<br />

The create() method is the abstract method inherited from Gradient that needs to be<br />

implemented. Again, implementing in this case means creating the method’s code block<br />

(the areas between <strong>and</strong> including the curly braces). The create() method is where the<br />

custom gradients are actually created.<br />

Finally, the LinearGradient class includes setAxis() <strong>and</strong> getAxis() methods for specifying<br />

which axis to draw the gradient across. I could have placed these two methods within<br />

the Gradient superclass, but it was more appropriate to put them in the LinearGradient<br />

subclass, as some gradients don’t utilize a dominant axis (e.g., radial gradients)—which<br />

illustrates that although subclasses share certain features (inherited from their common<br />

superclass), they may (<strong>and</strong> usually do) contain their own unique properties <strong>and</strong> methods<br />

as well.<br />

RadialGradient class<br />

class RadialGradient extends Gradient {<br />

//default constructor<br />

RadialGradient() {<br />

super();<br />

}<br />

//constructor<br />

RadialGradient(color c1, color c2) {<br />

this.c1 = c1;<br />

this.c2 = c2;<br />

}<br />

//constructor<br />

RadialGradient(color c1, color c2, Rectangle bounds) {<br />

this.c1 = c1;<br />

this.c2 = c2;<br />

this.bounds = bounds;<br />

}<br />

// required: implemented create method<br />

void create(){<br />

float px = 0, py = 0, angle = 0;<br />

float radius1 = bounds.width/2;<br />

float radius2 = bounds.height/2;<br />

float centerX = bounds.x+radius1;<br />

float centerY = bounds.y+radius2;<br />

float radiusMax = max(radius1, radius2);

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

Saved successfully!

Ooh no, something went wrong!