lundi 23 mai 2011

Making and using a simple Object

Although you can do a lot of nice things just programming around (called "knitting") sometimes you meet things like objects and classes.
Some basic knowledge of Object Orientated ( "OO" ) programming may be useful.
In this post I present a simple example of a class, with which you can make objects.
A class is a black box. With this black box you can produce your own "things", for instance records of a name and an amount of payment.
So we have a name, "Beam" and we have a payment "1.75". In a so called constructor these are tied together. Later on we can get these data out again.

A class can save these data, but can also have functions. Like the data, these functions are also tailor made.
In your main sketch you can make the TestClass object like this:
TestClass record = new TestClass( "Beam", 1.75 );


In the class the format of the parameters must be specified, like String and float.
A whole bunch of records can be made like this:
TestClass[] namePayArray = new TestClass[20];


but these are empty objects at the moment, you can fill these objects using a for loop:

for (int i = 0; i<20 ; i++)


//making an object:
TestClass newtest = new TestClass("Beam" + str(i) , 2.5 + random(10) ); 
//putting this object in an array:
namePayArray[i] = newtest; 
}

First we make an object, then we fill in the object in the array.

The Processing example can be found here:
http://www.contrechoc.com/crosslab/objectExample.zip
The results are printed in the debug window.

An example of a useful object is the timer, a few post ago:
http://flashscriptingwonders.blogspot.com/2011/05/timer-considerations.html

Then a more elaborate class example is this interesting circle script from a few blog posts ago:
http://flashscriptingwonders.blogspot.com/2011/04/circle-packing-scripts.html


Here we already use the circle class.
The question is....what to do when you want to see more hives (circle packings) at different positions?
So the whole script of the circlepacking is placed in a different class, (and for convenience in a different tab).
What is left is a simple main sketch with the constructor of the Hive's and the draw and the mousePressed (etc) functions:

Hive testme1;
Hive testme2 ;
Hive[] hiveArray = new Hive[20]; //the array is filled with "empty" objects


void setup(){
size(1000,1000);
testme1 = new Hive(250,250);
testme2 = new Hive(550,550);


for (int i = 0; i<20 ; i++){ 

Hive newtest = new Hive( int( 50 + random(1000)) , int(50 + random(1000)) ); hiveArray[i] = newtest; }
}


void draw(){
background( 255 );


testme1.draw();
testme2.draw();


for (int i = 0; i<20 ; i++){ 

hiveArray[i].draw(); 
}
}


void mousePressed() {
for (int i = 0; i<20 ; i++){ hiveArray[i].myMousePressed(); } }


void mouseDragged() {
for (int i = 0; i<20 ; i++){ hiveArray[i].myMouseDragged(); } }


void mouseReleased() {
for (int i = 0; i<20 ; i++){ hiveArray[i].myMouseReleased(); } }



You can find the sketch here: http://www.contrechoc.com/crosslab/hiveCircleClassExample.zip

Aucun commentaire:

Enregistrer un commentaire