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.

AXIS_VERTICAL from outside the Gradient class (or its subclasses), you’d write<br />

Gradient.AXIS_VERTICAL; to access the value from within Gradient (or its subclass), you<br />

can just use AXIS_VERTICAL. You can use/access static properties from within any class<br />

without the need to instantiate an object of the class. Conversely, instance properties<br />

require an object to be used. Another way to think about the distinction between static<br />

<strong>and</strong> instance properties is that there is only ever a single value for any static property, but<br />

each object has its own copy of any instance properties.<br />

Instance properties<br />

Below the constants, I declared some instance properties. As I just discussed, these properties<br />

can only be accessed by an object of the class (or in this case, objects of the subclasses).<br />

For example, if you create a RadialGradient class <strong>and</strong> then instantiate an object<br />

using the syntax RadialGradient rg = new RadialGradient ();, you could access the<br />

object’s c1 property with the syntax rg.c1 or rg.getColor1().<br />

The instance property Rectangle r uses Java’s Rectangle class. This class is really h<strong>and</strong>y<br />

for holding the bounds of something. Instantiating an object of the Rectangle class takes<br />

four arguments, defining x, y, width, <strong>and</strong> height. These public properties can then be<br />

accessed directly, as in r.x, r.y, r.width, <strong>and</strong> r.height (assuming the Rectangle object is<br />

named r).<br />

Abstract method<br />

Below the instance properties is a strange-looking method declaration:<br />

abstract void create();<br />

This is an abstract method, which doesn’t include any actual implementation of the<br />

method; notice that curly braces aren’t even included. Abstract methods are used to<br />

enforce a rule between the abstract superclass <strong>and</strong> any subclasses: a class that extends an<br />

abstract class must implement any abstract methods within the superclass (or itself be<br />

declared an abstract class). This means that any class that extends the Gradient class must<br />

include a create() method—with the curly braces <strong>and</strong> preferably some gradient-creating<br />

code between them. You are not required to include an abstract method in an abstract<br />

class; you can just use st<strong>and</strong>ard implemented methods (also known as concrete methods)<br />

like the getter/setter methods included below the abstract create() method. Subclasses<br />

have no obligation to reimplement any concrete methods within the abstract superclass.<br />

In the Gradient class, it made sense to make the create() method abstract, since the<br />

code to create each gradient (linear or radial, in this case) will be radically different. In<br />

addition, by forcing subclasses of the Gradient class to implement their own create()<br />

method, you enforce a common interface—which is a good thing. Imagine if some company<br />

took your Gradient class <strong>and</strong> extended it to create a library of 1,000 specialized<br />

Gradient subclasses (<strong>and</strong> paid you gobs of money, of course). Since your superclass<br />

requires any subclasses to implement the create() method, a user of the subclasses would<br />

have immediate insight into how to draw any of the 1,000 custom gradients. The complicated<br />

plotting algorithms for each gradient would be encapsulated inside the custom<br />

implemented create() methods in each subclass—all happily hidden from the user.<br />

COLOR AND IMAGING<br />

471<br />

10

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

Saved successfully!

Ooh no, something went wrong!