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

lundi 16 mai 2011

Using a PVector making a field

A vector is a point with a property of an added direction. In short: an arrow.
Nice drawings can be made using arrows over the whole plane. But doing this by hand is tedious. So we do it by code.
In Processing the thing to look at is a PVector.

There is a nice tutorial about PVectors:
http://processing.org/learning/pvector/

Vector on a plane (or in space) - when the vectors are ordered, that is vary in a "physical" way - form a so called vector field. The vectors are like hair being combed.

One way to imagine a vector field is to think of magnets, the field lines, so beautifully made visible by iron dust:
http://www.openprocessing.org/visuals/?visualID=21418

Another way is to look at fluids, waves:
http://www.openprocessing.org/visuals/?visualID=12569

This is also a nice example:
http://www.openprocessing.org/visuals/?visualID=6707

Or look at this one:
http://www.openprocessing.org/visuals/?visualID=27653

With color:
http://www.openprocessing.org/visuals/?visualID=1036

search on keyword "field" in OpenProcessing.org

Google search API in Processing

When you want to get a google search inside your Processing sketch:

http://lowfrequency.org/interactivity/wiki/index.php?title=Google_API_In_Processing

On this page there is an example:
http://lowfrequency.org/interactivity/wiki/index.php?title=GoogleJSONSearch_Example


which worked right away!

(be careful, look at the dates of the link you find, for instance, this link:
http://users.design.ucla.edu/~tatsuyas/tools/google/index.htm
is from 2005 and the libraries don't work anymore.....)

But that is because I already had a json.jar
If you don't have a json.jar download this zip, which has a json.jar in the folder code in the sketch folder. Put this folder inside the Processing folder and restart Processing before running it.
http://www.contrechoc.com/crosslab/googleSearch.zip

samedi 7 mai 2011

Timer considerations

This links shows how easy everything becomes in Object Oriented Programming:
http://www.learningprocessing.com/examples/chapter-10/example-10-5/

But this example when used with mousePressed (for example) has a flaw: the timer.isFinished() is always true when the timer is not started immediately.

So we added a stop() function for the timer, and now timer.isFinished() is also false so long as the timer is not started.

You can find the sketch here: http://www.contrechoc.com/crosslab/timerOO/applet/timerOO.html

When you click in the sketch square you see the background change after 3 seconds.


This is a good example to see the power of objects.