<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>beau&#039;s blog &#187; Programming</title>
	<atom:link href="http://beausievers.com/blog/index.php/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://beausievers.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 19 Jul 2011 02:23:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.1</generator>
		<item>
		<title>Processing tutorial: interfaces</title>
		<link>http://beausievers.com/blog/index.php/2009/07/10/processing-tutorial-interfaces/</link>
		<comments>http://beausievers.com/blog/index.php/2009/07/10/processing-tutorial-interfaces/#comments</comments>
		<pubDate>Sat, 11 Jul 2009 00:19:27 +0000</pubDate>
		<dc:creator>beau</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://beausievers.com/blog/?p=29</guid>
		<description><![CDATA[Earlier this week I wrote a tutorial about inheritance and abstract classes in Processing/Java. I mentioned there were several tools we could use to share functionality between classes; the first tool was the abstract class, the second tool is the interface. First, let&#8217;s do a quick review of abstract classes. An abstract class differs from [...]]]></description>
			<content:encoded><![CDATA[<p>Earlier this week I wrote a tutorial about inheritance and abstract classes in Processing/Java. I mentioned there were several tools we could use to share functionality between classes; the first tool was the abstract class, the second tool is the <em>interface</em>.</p>
<p>First, let&#8217;s do a quick review of abstract classes. An <em>abstract</em> class differs from a <em>concrete</em> class in that it can never be directly instantiated. A concrete class must <em>extend</em> the abstract class, and that concrete class may be instantiated. All concrete classes extending an abstract class <em>inherit</em> functionality from a single abstract class—multiple inheritance, or inheritance from more than one class at the same time, is not allowed. Abstract classes may contain both concrete methods and abstract method signatures. Concrete fields and methods are inherited by subclasses. Abstract method signatures define a contract which subclasses must fulfill. For example, if your abstract class contains the code <code>abstract void move();</code> then all subclasses must define their own <code>void move()</code> method.</p>
<p>Interfaces are a way of defining this kind of contract, but without specifying any functionality along the way. An object which <em>implements</em> an interface is committing to respond in a predictable way to a set of methods. For example, this is how you might specify an interface for objects that can move, and an object that implements this interface:</p>
<p><!--start_raw--><br />
<code class="codeblock">interface Moves {<br />
  void move(int dx, int dy);<br />
}</p>
<p>class Creature implements Moves {<br />
  int x, y;</p>
<p>  Creature(int initX, int initY) {<br />
    x = initX; y = initY;<br />
  }</p>
<p>  void move(int dx, int dy) {<br />
    x += dx; y += dy;<br />
  }<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>Note that when declaring a method signature in an interface (as opposed to an abstract class), it is not necessary to prepend <code>abstract</code>, as Processing/Java will figure out what we&#8217;re up to and make the method abstract implicitly.</p>
<p>But why use interfaces at all? Why not just use an abstract class, call it <code>MovingThing</code>, and have <code>Creature</code> extend <code>MovingThing</code>? Because while classes can only extend one superclass, they may implement any number of interfaces. For example:</p>
<p><!--start_raw--><br />
<code class="codeblock">interface Moves {<br />
  void move(int dx, int dy);<br />
}</p>
<p>interface ChangesColor {<br />
  void changeColor(int newColor);<br />
}</p>
<p>class Creature implements Moves, ChangesColor {<br />
  int x, y, c;</p>
<p>  Creature(int initX, int initY) {<br />
    x = initX; y = initY;<br />
  }</p>
<p>  void move(int dx, int dy) {<br />
    x += dx; y += dy;<br />
  }</p>
<p>  void changeColor(int newColor) {<br />
    c = newColor;<br />
  }<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>This is a way to get something a little like multiple inheritance in Processing/Java. Objects implementing these interfaces may not all do the same thing, as they each have the right to their own implementation, but at least we can be sure that they&#8217;ll do <em>something</em> when called forth to serve.</p>
<p>An object of any class which implements a given interface guarantees it will implement the methods described in that interface. Because we can be sure that an object of a class which implements an interface will have a specific set of features, Processing/Java allows us to typecast objects to the directly to the interface type. For example, say we&#8217;re writing a space shooter game, and we have an <code>ArrayList</code> called <code>listOfSpaceStuff</code> which keeps track of all of our moving objects on the screen. The list will hold objects of types <code>Asteroid</code>, <code>OurHero</code>, and <code>BadGuy</code>. As long as <code>Asteroid</code>, <code>OurHero</code>, and <code>BadGuy</code> all implement the interface <code>Moves</code>, we can do the following:</p>
<p><!--start_raw--><br />
<code class="codeblock">for(int i = 0; i < listOfSpaceStuff.size(); i++) {<br />
  Moves movingObject = (Moves)listOfSpaceStuff.get(i);<br />
  movingObject.move(dx, dy);<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>Note that because each of our classes has its own unique implementation of <code>move()</code>, they can all move around the screen in a different way. Also note that we could have created a superclass implementing moves, <code>SpaceThing</code>, made <code>Asteroid</code>, <code>OurHero</code>, and <code>BadGuy</code> subclasses of <code>SpaceThing</code>, and then cast the objects coming out of the list to <code>SpaceThing</code>.</p>
<p>Before the final example, a warning about interfaces. Because interfaces only contain abstract method signatures, they pass along the burden of definition to implementing classes. While changing an abstract class can painlessly enhance the functionality of all its subclasses, changing an interface will more likely break your program, because all of the classes which implement that interface will need to be modified so they fulfill the requirements of the new contract. It pays to do some thinking up-front about how your interfaces are going to work, and whether or not the functionality they describe is likely to change, before using them extensively. (The flip side of this is, of course, that using abstract classes can cause brittle code of an entirely different sort. Interested readers might look to <a href="http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html"><em>Why extends is evil</em> at JavaWorld</a> for some discussion.)</p>
<p>Here's an example which uses class inheritance as well as interface inheritance, and typecasts objects to interfaces. Notice that <code>Wall</code> and <code>Chameleon</code> both implement the interface <code>ChangesColor</code>, but in dramatically different ways.</p>
<p><!--start_raw--></p>
<div id="interfaces_container">
<p><!--[if !IE]> --><br />
	<object classid="java:interfaces.class"<br />
			type="application/x-java-applet"<br />
			archive="/processing/interfaces/interfaces.jar"<br />
			width="400" height="300"<br />
			standby="Loading Processing software..." ><param name="archive" value="/processing/interfaces/interfaces.jar" /><param name="mayscript" value="true" /><param name="scriptable" value="true" /><param name="image" value="/processing/interfaces/loading.gif" /><param name="boxmessage" value="Loading Processing software..." /><param name="boxbgcolor" value="#FFFFFF" /><param name="test_string" value="outer" /><!--<![endif]--></p>
<p>	<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"<br />
			codebase="http://java.sun.com/update/1.5.0/jinstall-1_5_0_15-windows-i586.cab"<br />
			width="400" height="300"<br />
			standby="Loading Processing software..."  ><param name="code" value="/processing/interfaces/interfaces" /><param name="archive" value="/processing/interfaces/interfaces.jar" /><param name="mayscript" value="true" /><param name="scriptable" value="true" /><param name="image" value="/processing/interfaces/loading.gif" /><param name="boxmessage" value="Loading Processing software..." /><param name="boxbgcolor" value="#FFFFFF" /><param name="test_string" value="inner" /><p>
			<strong><br />
				This browser does not have a Java Plug-in.<br />
				<br />
				<a href="http://java.sun.com/products/plugin/downloads/index.html" title="Download Java Plug-in"><br />
					Get the latest Java Plug-in here.<br />
				</a><br />
			</strong>
		</p>
<p>	</object></p>
<p><!--[if !IE]> --><br />
	</object><br />
<!--<![endif]--></p>
</div>
<p>
Source code: <a href="/processing/interfaces/interfaces.pde">interfaces</a>
</p>
<p><code class="codeblock"><br />
Wall wally = new Wall(50, 150, 0x88000000);<br />
Creature bob = new Creature(150, 150);<br />
Chameleon sally = new Chameleon(250, 150, #FFFFFF);</p>
<p>ArrayList drawList = new ArrayList();<br />
ArrayList colorList = new ArrayList();<br />
ArrayList moveList = new ArrayList();</p>
<p>void setup()<br />
{<br />
  size(400,300);<br />
  frameRate(30);</p>
<p>  drawList.add(wally);<br />
  drawList.add(bob);<br />
  drawList.add(sally);</p>
<p>  colorList.add(wally);<br />
  colorList.add(sally);</p>
<p>  moveList.add(bob);<br />
  moveList.add(sally);<br />
}</p>
<p>void draw()<br />
{<br />
  background(222);<br />
  for(int i = 0; i < drawList.size(); i++) {<br />
    Drawable drawableObject = (Drawable)drawList.get(i);<br />
    drawableObject.draw();<br />
  }</p>
<p>  for(int i = 0; i < colorList.size(); i++) {<br />
    ChangesColor colorableObject = (ChangesColor)colorList.get(i);<br />
    colorableObject.changeColor(color(random(255), random(255), random(255)));<br />
  }</p>
<p>  for(int i = 0; i < moveList.size(); i++) {<br />
    Moves moveableObject = (Moves)moveList.get(i);<br />
    moveableObject.move(int(random(-2,2)), int(random(-2,2)));<br />
  }<br />
}</p>
<p>interface Drawable {<br />
  void draw();<br />
}</p>
<p>interface Moves {<br />
  void move(int dx, int dy);<br />
}</p>
<p>interface ChangesColor {<br />
  void changeColor(int newColor);<br />
}</p>
<p>class Creature implements Drawable, Moves {<br />
  int x, y;</p>
<p>  Creature(int initX, int initY) {<br />
    x = initX; y = initY;<br />
  }</p>
<p>  void move(int dx, int dy) {<br />
    x += dx; y += dy;<br />
  }</p>
<p>  void draw() {<br />
    fill(200);<br />
    ellipse(x, y, 25, 40);<br />
  }<br />
}</p>
<p>class Wall implements Drawable, ChangesColor {<br />
  int x, y, c;</p>
<p>  Wall(int initX, int initY, int initColor) {<br />
    x = initX; y = initY;<br />
    c = initColor;<br />
  }</p>
<p>  void draw() {<br />
    fill(c);<br />
    rect(x, y, 20, 100);<br />
  }</p>
<p>  void changeColor(int newColor) {<br />
    c = newColor;<br />
  }<br />
}</p>
<p>class Chameleon extends Creature implements ChangesColor {<br />
  int c, target;<br />
  float progress;</p>
<p>  Chameleon(int initX, int initY, int initColor) {<br />
    super(initX, initY);<br />
    c = initColor;<br />
    target = initColor;<br />
    progress = 1;<br />
  }</p>
<p>  void changeColor(int newColor) {<br />
    if(progress >= 1) {<br />
      c = target;<br />
      target = newColor;<br />
      progress = 0;<br />
    }<br />
  }</p>
<p>  void draw() {<br />
    fill(lerpColor(c, target, progress));<br />
    ellipse(x, y, 30, 10);<br />
    ellipse(x, y, 10, 30);<br />
    if(progress < 1) {<br />
      progress += 0.05;<br />
    }<br />
  }<br />
}<br />
</code></p>
<p><!--end_raw--></p>
]]></content:encoded>
			<wfw:commentRss>http://beausievers.com/blog/index.php/2009/07/10/processing-tutorial-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing tutorial: inheritance and abstract classes</title>
		<link>http://beausievers.com/blog/index.php/2009/07/08/processing-tutorial-inheritance-and-abstract-classes/</link>
		<comments>http://beausievers.com/blog/index.php/2009/07/08/processing-tutorial-inheritance-and-abstract-classes/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 16:23:30 +0000</pubDate>
		<dc:creator>beau</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://beausievers.com/blog/?p=28</guid>
		<description><![CDATA[Recently I&#8217;ve been using Processing for a number of graphics programming projects. There are a lot of web tutorials covering concepts like inheritance, interfaces, inner classes, polymorphism, and so on from a Java perspective, but very few bridge the gap between Java and Processing, and fewer still offer working Processing demonstrations. Further, a great number [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been using <a href="http://www.processing.org/">Processing</a> for a number of graphics programming projects. There are a lot of web tutorials covering concepts like inheritance, interfaces, inner classes, polymorphism, and so on from a Java perspective, but very few bridge the gap between Java and Processing, and fewer still offer working Processing demonstrations. Further, a great number of Processing tutorials and demos seem to focus on specific animation techniques and not on writing solid, maintable code following best practices. I&#8217;m writing a series of tutorials for the advanced beginner or intermediate level Processing user interested in taking their programming skill to the next level.</p>
<p>Prerequisite to understanding this tutorial is having a decent sense of object-oriented programming, specifically how to define and use classes. <a href="http://processing.org/learning/objects/">This tutorial at the Processing website</a> is a good start.</p>
<p>Here&#8217;s a common problem for Processing programmers. We have a bunch of objects, lets call them creatures, which are similar in some fundamental ways, but have differences which are not simply superficial. All of the creatures have a shared vocabulary of properties and methods: they all have a width and a height, and they all can be drawn to the screen. However, each creature has one or two actions which are unique. For example, one type of creature might search the screen for food to eat, another might change colors when clicked, and a third might be capable of spontaneous asexual reproduction.</p>
<p>The breadth of difference in functionality might initially suggest coding each creature as a different class, like this:</p>
<p><!--start_raw--><br />
<code class="codeblock">class Monkey {<br />
  int w, h;</p>
<p>  Monkey(initWidth, initHeight) {<br />
    [...]<br />
  }</p>
<p>  void draw() {<br />
    [...]<br />
  }</p>
<p>  void lookForFood() {<br />
    [...]<br />
  }<br />
}</p>
<p>class Chameleon {<br />
  int w, h;</p>
<p>  Chameleon(initWidth, initHeight) {<br />
    [...]<br />
  }</p>
<p>  void draw() {<br />
    [...]<br />
  }</p>
<p>  void changeColors() {<br />
    [...]<br />
  }<br />
}</p>
<p>class Amoeba {<br />
  int w, h;</p>
<p>  Amoeba(initWidth, initHeight) {<br />
    [...]<br />
  }</p>
<p>  void draw() {<br />
    [...]<br />
  }</p>
<p>  void reproduceAsexually() {<br />
    [...]<br />
  }<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>This is bad for two reasons. First, as Good Programmers, it offends our sensibilities by violating a cardinal rule of our craft: <a href="http://en.wikipedia.org/wiki/Don't_repeat_yourself">Don&#8217;t Repeat Yourself</a>. We are specifying the properties of width and height as well as the constructor and <code>draw()</code> functions three times with no structural variation, so this is two times too many. Second, we&#8217;d like to keep track of all our creatures in one big <code>ArrayList</code>. While we&#8217;re free to add <code>Monkeys</code>, <code>Chameleons</code>, and <code>Amoebae</code> to an <code>ArrayList</code>, things go wrong when we try to take them out. Processing and Java will require us to cast the object we are getting from the list to a specific type. For example, we might want to do something like this:</p>
<p><!--start_raw--><br />
<code class="codeblock">for(int i = 0; i < creatureList.size(); i++) {<br />
  currentCreature = (Monkey) creatureList.get(i);<br />
  currentCreature.draw();<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>But what if the current creature is not, in fact, a <code>Monkey</code>, but instead an <code>Amoeba</code>? Then we'll get a type error. We could keep separate lists of <code>Monkeys</code>, <code>Chameleons</code>, and <code>Amoebae</code>—but then we'd be repeating ourselves. And what if we later wanted to add <code>GiantSloths</code> or <code>Tardigrades</code>? Our code would be difficult to modify and maintain. Fortunately, we have a few tools which can help us out. The tool we'll talk about today is the abstract class, which allows use of something called inheritance.</p>
<p>We can specify an abstract class, called Creature, which defines the properties and methods common to every object deigning to call itself creature. We can then implement subclasses of Creature (e.g. <code>Monkey</code>, <code>Chameleon</code>, and <code>Amoeba</code>) which have additional functionality of their own.</p>
<p><!--start_raw--><br />
<code class="codeblock">abstract class Creature {<br />
  int w, h;</p>
<p>  Creature(int initWidth, int initHeight) {<br />
    w = initWidth; h = initHeight;<br />
  }</p>
<p>  abstract void draw();<br />
}</p>
<p>class Monkey extends Creature {<br />
  Monkey(int initWidth, int initHeight) {<br />
    super(initWidth, initHeight);<br />
  }</p>
<p>  void draw() {<br />
    [Draw a monkey.]<br />
  }<br />
}</p>
<p>class Chameleon extends Creature {<br />
  Chameleon(int initWidth, int initHeight) {<br />
    super(initWidth, initHeight);<br />
  }</p>
<p>  void draw() {<br />
    [Draw a chameleon.]<br />
  }<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>An abstract class is like a partially filled-in template for subclasses. Members of an abstract class can never be directly instantiated (that is, created directly). Given the code above, for example, it would make no sense to write something like <code>Creature aCreature = new Creature(2,3);</code>. Instances of classes like <code>Monkey</code> and <code>Chameleon</code> can be <code>Creatures</code>, but there's no such thing as an object which is just a <code>Creature</code> and nothing else. Every subclass of <code>Creature</code> inherits its methods and variables. In this case, that means that <code>Monkeys</code> and <code>Chameleons</code> have integers for storing their width and height.</p>
<p>Abstract classes also make demands of their subclasses. The code <code>abstract void draw();</code> means that every subclass of <code>Creature</code> is required to implement a function of type void called <code>draw()</code>. If a subclass didn't implement this <code>draw()</code> function, we'd get an error. This set of requirements is like a contract that each subclass must honor. I'm going to expand on this notion of contractual obligation in the next tutorial, which will include a section on another tool we could use for this purpose: interfaces.</p>
<p>Additionally, something funny is happening in those subclass constructors. Subclasses are allowed to call the constructor function of their superclass, in this case <code>Creature()</code>, by using the method <code>super()</code>. The <code>Monkey()</code> and <code>Chameleon()</code> constructors just pass along the initial width and height values to the superclass constructor which handles the value assignment. If a subclass has additional variables not common to the superclass, those should also be initialized in the constructor. For example, <code>Chameleons</code> might want to store their current color in a variable, and begin life with a color passed to the constructor. We'd modify the <code>Chameleon</code> subclass as follows:</p>
<p><!--start_raw--><br />
<code class="codeblock">class Chameleon extends Creature {<br />
  int c;</p>
<p>  Chameleon(int initWidth, int initHeight, int initColor) {<br />
    super(initWidth, initHeight);<br />
    c = initColor;<br />
  }</p>
<p>  void draw() {<br />
    [Draw a chameleon.]<br />
  }<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>Now we can populate an <code>ArrayList</code> with <code>Monkeys</code> and <code>Chameleons</code>, then iterate through that list, casting each item to the type <code>Creature</code>. Because the <code>Creature</code> superclass requires all its subclasses implement the <code>draw()</code> method, we can safely call this method on any <code>Creature</code> we get from the <code>ArrayList</code>. Here's how that looks:</p>
<p><!--start_raw--><br />
<code class="codeblock">ArrayList creatureList = new ArrayList();</p>
<p>Monkey hanuman = new Monkey(4,8);<br />
Chameleon lizzy = new Chameleon(7,3);</p>
<p>creatureList.add(hanuman);<br />
creatureList.add(lizzy);</p>
<p>for(int i = 0; i < creatureList.size(); i++) {<br />
  currentCreature = (Creature) creatureList.get(i);<br />
  currentCreature.draw();<br />
}<br />
</code><br />
<!--end_raw--></p>
<p>Mighty convenient. Now if we add additional variables or methods to the <code>creature</code> class, all of its subclasses inherit that functionality while preserving their unique features. Below is a complete working demonstration of these principles in action, using Ellipsoids and Rectithings, creatures which are much easier to draw than apes and lizards.</p>
<p>Click on the sketch to give it focus, then press the spacebar to move all the creatures to the center of the screen.</p>
<p><!--start_raw--></p>
<div id="abstractClasses_container">
<p><!--[if !IE]> --><br />
	<object classid="java:abstractClasses.class"<br />
			type="application/x-java-applet"<br />
			archive="/processing/abstractClasses/abstractClasses.jar"<br />
			width="400" height="300"<br />
			standby="Loading Processing software..." ><param name="archive" value="/processing/abstractClasses/abstractClasses.jar" /><param name="mayscript" value="true" /><param name="scriptable" value="true" /><param name="image" value="/processing/abstractClasses/loading.gif" /><param name="boxmessage" value="Loading Processing software..." /><param name="boxbgcolor" value="#FFFFFF" /><param name="test_string" value="outer" /><!--<![endif]--></p>
<p>	<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"<br />
			codebase="http://java.sun.com/update/1.5.0/jinstall-1_5_0_15-windows-i586.cab"<br />
			width="400" height="300"<br />
			standby="Loading Processing software..."  ><param name="code" value="/processing/abstractClasses/abstractClasses" /><param name="archive" value="/processing/abstractClasses/abstractClasses.jar" /><param name="mayscript" value="true" /><param name="scriptable" value="true" /><param name="image" value="/processing/abstractClasses/loading.gif" /><param name="boxmessage" value="Loading Processing software..." /><param name="boxbgcolor" value="#FFFFFF" /><param name="test_string" value="inner" /><p>
			<strong><br />
				This browser does not have a Java Plug-in.<br />
				<br />
				<a href="http://java.sun.com/products/plugin/downloads/index.html" title="Download Java Plug-in"><br />
					Get the latest Java Plug-in here.<br />
				</a><br />
			</strong>
		</p>
<p>	</object></p>
<p><!--[if !IE]> --><br />
	</object><br />
<!--<![endif]--></p>
</div>
<p>
Source code: <a href="/processing/abstractClasses/abstractClasses.pde">abstractClasses</a>
</p>
<p><code class="codeblock"><br />
// Create three creatures.<br />
Ellipsoid jane = new Ellipsoid(100, 50, 300, 150);<br />
Rectithing alan = new Rectithing(40, 15, 100, 50);<br />
Ellipsoid bob = new Ellipsoid(10, 70, 150, 200);</p>
<p>// Create our ArrayList for creature storage.<br />
ArrayList creatureList = new ArrayList();</p>
<p>void setup()<br />
{<br />
  size(400,300);<br />
  frameRate(30);</p>
<p>  // Add our creatures to the list.<br />
  creatureList.add(jane);<br />
  creatureList.add(alan);<br />
  creatureList.add(bob);<br />
}</p>
<p>void draw()<br />
{<br />
  background(222);</p>
<p>  // Iterate through the list of creatures, telling each one to draw<br />
  // and move.<br />
  for(int i = 0; i < creatureList.size(); i++) {<br />
    Creature currentCreature = (Creature) creatureList.get(i);<br />
    currentCreature.draw();<br />
    currentCreature.move();<br />
  }<br />
}</p>
<p>// When the spacebar is pressed, iterate through the creatureList<br />
// and move every creature to the center.<br />
void keyReleased() {<br />
  if(key == ' ') {<br />
    for(int i = 0; i < creatureList.size(); i++) {<br />
      Creature currentCreature = (Creature) creatureList.get(i);<br />
      currentCreature.moveToCenter();<br />
    }<br />
  }<br />
}</p>
<p>abstract class Creature {<br />
  // Every subclass will inherit these variables.<br />
  int w, h, x, y;</p>
<p>  Creature(int initWidth, int initHeight, int initX, int initY) {<br />
    w = initWidth; h = initHeight;<br />
    x = initX; y = initY;<br />
  }</p>
<p>  // Every sublcass will inherit this method.<br />
  void moveToCenter() {<br />
    x = int(width * 0.5);<br />
    y = int(height * 0.5);<br />
  }</p>
<p>  // Every subclass is required to implement these methods on their own.<br />
  abstract void draw();<br />
  abstract void move();<br />
}</p>
<p>class Ellipsoid extends Creature {<br />
  // Ellipsoids have a changing color.<br />
  int c;</p>
<p>  Ellipsoid(int initWidth, int initHeight, int initX, int initY) {<br />
    // Call the superclass constructor on initialization.<br />
    super(initWidth, initHeight, initX, initY);<br />
    c = int(random(255));<br />
  }</p>
<p>  void draw() {<br />
    fill(c);<br />
    ellipse(x,y,w,h);<br />
  }</p>
<p>  void move() {<br />
    x += int(random(-2,2));<br />
    y += int(random(-2,2));<br />
    w += int(random(-3,3));<br />
    h += int(random(-3,3));<br />
    c += int(random(-10,10));<br />
  }<br />
}</p>
<p>class Rectithing extends Creature {<br />
  Rectithing(int initWidth, int initHeight, int initX, int initY) {<br />
    super(initWidth, initHeight, initX, initY);<br />
  }</p>
<p>  void draw() {<br />
    fill(127);<br />
    rect(x,y,w,h);<br />
  }</p>
<p>  void move() {<br />
    x += int(random(-3, 3));<br />
    y += int(random(-3, 3));<br />
  }<br />
}<br />
</code><br />
<!--end_raw--></p>
]]></content:encoded>
			<wfw:commentRss>http://beausievers.com/blog/index.php/2009/07/08/processing-tutorial-inheritance-and-abstract-classes/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

