Don Havey

Sustainably harvested information

Archive for the generic classes tag

Building blocks  

I’ve written a very brief introduction to Processing. Hopefully people found it so tantalizingly uninformative that they resolved to visit the Processing website and learn the basics of the language via the tutorials there.

Today, I introduce my generic classes: the building blocks on which I’ll be creating some exciting applets for your viewing pleasure. This will be a little boring. You can skip it if you want, but in order to understand the code in future examples, you’ll eventually need to understand what’s happening in these core classes.

Wait!

I need to define the word class for those not accustomed to object-oriented programming and who haven’t read the tutorials yet. A class defines a type of object that can replicated within an applet. As an example, if you were creating a digital garden of sorts, you might define a “flower” class. Each class has a constructor function, which, when called, creates a new instance of that class. So for your pretty flower garden, you would write:
class Flower{
  //initialize all variables that belong to the flower class
  int npetals;
  //the flower class constructor function
  //is called whenever you write "new Flower()"
  Flower(int $npetals){
    //assign values to this instance's variables
    petals = $petals;
  }
  //some other function of the flower class
  void look_pretty(){
    //do something to look pretty
  }
}
//somewhere else in our code, we can now create flowers:
//initiate an array to contain some flowers
int nflowers = 50;
Flower[] flowers = new Flower[nflowers];
//loop through the array and initiate that many new flowers
for(int i=0;i<nflowers ;i++){
  flower[i] = new Flower(floor(random(5,12)));
}

Additionally, classes can be extended so that one class may inherit all of the methods and variables of another, and add some methods and variables of its own. More on that later.

Wait again!

Before I continue, I should admit that my coding style is a little nonconformist. I tend to write denser code than is recommended… less whitespace… and sometimes I’ll make two related statements share a line. This will make my code a little awkward to read for people who come from stricter programming backgrounds. Too bad. I like density. Less scrolling, less eye movement, less space. It’s an unbreakable habit! Sorry.

Proceed!

Getting back to the point….

This is an unintentional set of core classes. I did not sit down and think “I need core classes, maybe I’ll write some.” That would be a silly thing to think. You can already find many great pre-built classes on the Processing website. Rather, this set of classes evolved from many many projects, each of which required some addition to the classes I created for a simpler (previous) project… until I had a pretty comprehensive set of core classes, which I now reuse compulsively.

I am making these classes (and all the source code files I upload to this site) freely available. They can be modified and distributed at will, as long as the credits and statement at the top of each file remain intact. If you love them, you can use them for your own projects, but I’d recommend taking a look at the libraries available on the Processing website first. You can probably find a better set of classes available there.

If you really love these classes and are planning on using them in your own work, you could buy me a drink or something.

Download the classes

Click here to get the files.

You’ll need to open the files in Processing to take a look; I won’t be posting the entire source here. The Processing editor’s syntax highlighting makes the code much more legible than what I could offer you on this page. I’ll only be describing the classes and posting snippets of the supporting code here.

Read more…

The article has

no responses yet

Written by Don

April 5th, 2008 at 8:31 pm