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

470<br />

// abstract method, to be implmented by subclasses<br />

abstract void create();<br />

/* concrete methods*/<br />

//getter/setters<br />

void setColor1(color c1){<br />

this.c1 = c1;<br />

}<br />

color getColor1(){<br />

return c1;<br />

}<br />

void setColor2(color c2){<br />

this.c2 = c2;<br />

}<br />

color getColor2(){<br />

return c2;<br />

}<br />

void setBounds(Rectangle Bounds){<br />

this.bounds = bounds;<br />

}<br />

Rectangle getBounds(){<br />

return bounds;<br />

}<br />

}<br />

Abstract class declaration<br />

Notice that I began the class with the Java keyword abstract. By preceding a class declaration<br />

with abstract, you enforce the rule that the class cannot be instantiated. Therefore,<br />

the call new Gradient() will generate a compiler error. I designed the Gradient class to be<br />

used exclusively as a base class to extend when building custom gradients. To read what<br />

Sun has to say about abstract classes, check out http://java.sun.com/docs/books/<br />

tutorial/java/javaOO/abstract.html.<br />

Class constants<br />

Next, I declared two constants:<br />

final static int AXIS_VERTICAL = 0;<br />

final static int AXIS_HORIZONTAL = 1;<br />

Again, I borrowed some Java modifiers (final <strong>and</strong> static) to enforce good coding practice.<br />

Constants shouldn’t be changed, <strong>and</strong> the use of the final keyword ensures that the<br />

compiler enforces this rule. Once a constant has an initial value, you’ll generate a compile<br />

error if you try to assign another value to it. Typically, constants are assigned integer<br />

values, as I did in the Gradient class.<br />

Declaring properties with the static keyword makes their values directly accessible using<br />

the class name, without the use of an object. For example, to access the value of

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

Saved successfully!

Ooh no, something went wrong!