We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › I need to understand.....
Pages: 1 2 
I need to understand..... (Read 2088 times)
I need to understand.....
Nov 23rd, 2005, 2:53pm
 
I just learned basics in processing and need some help...

I don't understand how to give a behavior to an object: I want my draw function to execute the "update function" of each object of the class I create....
I want to avoid this:
void draw(){
  object1.update();
  object2.update();
  object3.update();
}
and do something like that but inside the constructor:

class blabla{
    blabla(){
      this.draw(){
          // do something each new frame
         }
    }
 }

I hope you understand what I mean (need some help in english too!).

If you understand actionscript here is the code I want to "translate" in processing:

   movieclip.onEnterFrame = function(){
         do something each new frame;
         }
this function is like the draw function in processing but can be written inside the class constructor... and so be execute for each object created...

Thank you in advance....





Re: I need to understand.....
Reply #1 - Nov 23rd, 2005, 5:39pm
 
why do you have to put that method inside the constructor???

you can store your objects inside a vector, something like:

Quote:
Vector mcs;
void setup(){
 mcs=new Vector();
 mcs.add(new mc(...));
}
void draw(){
 for(var i=0; i<mcs.size(); i++){
   ((mc)mcs.get(i)).draw();
 }
}


I wrote it straight away and I hope there is no stupid mistake, btw the concept should be clear.

cya, chr
Re: I need to understand.....
Reply #2 - Nov 23rd, 2005, 10:23pm
 
Here's a way to do it without Vectors. It's longer, but more beginner friendly IMO.


Code:


class myObj
{
float x;
float y;
myObj()
{
//constructor sets the defaults
x=random(200);
y=random(200);
}
void drawObjects()
{
ellipse(x,y,20,20);
}
}

//make an array of these
myObject things[]=new myObject[10];

void setup()
{
//construct them
for(int i=0;i<things.length;i++)
{
things[i]=new myObject();
}
}

void draw()
{
//draw them
for(int i=0;i<things.length;i++)
{
things[i].draw();
}
}

Re: I need to understand.....
Reply #3 - Nov 23rd, 2005, 11:14pm
 
probably it is easier to understand, but he has anyway to learn that arrays in p5 are not "friendly" like they are in Flash Wink

cheers, chr
Re: I need to understand.....
Reply #4 - Nov 25th, 2005, 6:34pm
 
thank you guys,...

I don't really get the syntax for the vector but I like the second version .... thanks a lot.
Re: I need to understand.....
Reply #5 - Jan 13th, 2006, 11:36am
 
Quote:
---------------------------------------------
Vector mcs;
void setup(){
 mcs=new Vector();
 mcs.add(new mc(...));
}
void draw(){
 for(var i=0; i<mcs.size(); i++){
   ((mc)mcs.get(i)).draw();
 }
}
---------------------------------------------

I've read some codes with such kind of syntax.
Is there more explanation for this ?
Especially this line:

((mc)mcs.get(i)).draw();

Is the syntax like this:

float anyvar = (float) blabla.blablabla();

I mean the parentheses by 'mc' and 'float'.

thanks
Re: I need to understand.....
Reply #6 - Jan 13th, 2006, 6:54pm
 
chau, you're on the right track. the (mc) in ((mc)mcs.get(i)).draw(); is a way of casting an object to a correct type, just like (float)foobar is. however, a float or an int are simple data types and objects are more complicated.

the Vector class stores objects as instances of the superclass Object (which all classes are descended from). when you retrieve them from the Vector using the mcs.get(i) syntax you need to typecast them back to the type of object you want them to be. this is easy if you know that all objects in the Vector are of the same class.

so ((mc)mcs.get(i)).draw(); tells the Vector "mcs" to return the object at index "i", cast it to class "mc" and call its draw() command. this is a kind of shorthand which is perfectly valid but difficult to read if you don't understand how objects and Vectors work.
Re: I need to understand.....
Reply #7 - Jan 14th, 2006, 12:47am
 
Watz,thanks a lof for the explanation.
I think I almostly understand it.

And what about Vector ?
I don't find it in official reference.
Is it just a convention to use 'Vector',or
is there other reason ?

chau
Re: I need to understand.....
Reply #8 - Jan 14th, 2006, 1:40am
 
Vectors are a class within Java. Processing makes Java applets for you using an open source library of handy gubbinz that makes learning C style object orientated languages very easy for you. There's a whole host of hidden functions available here

Personally I don't use Vectors, I use a method which involves something called a deep-copy. You make an empty array that's a bit longer (call it temp for example) and then deep-copy you old array into the new one. In that extra slot on the end you just put in the last bit of the old array again.

I read a post where it is said that the deep-copy method is faster than Vectors. Here's an example of some deep-copy methods you could adapt for your work. They're based on the array transforming methods in the reference.

Code:

//2D vector path class (not to be confused with Java's "Vector") and methods
class Vec2{
float x,y;
Vec2(){}
Vec2(float x, float y){
this.x = x;
this.y = y;
}
}

//array functions
Vec2 [] addVec2(Vec2 [] vectors, Vec2 newVector) {
Vec2 [] temp = new Vec2[vectors.length + 1];
System.arraycopy(vectors, 0, temp, 0, vectors.length);
temp[vectors.length] = newVector;
return temp;
}
Vec2 [] concatVec2(Vec2 [] vectors, Vec2 [] addVectors) {
Vec2 [] temp = new Vec2[vectors.length + addVectors.length];
System.arraycopy(vectors, 0, temp, 0, vectors.length);
System.arraycopy(addVectors, 0, temp, vectors.length, addVectors.length);
return temp;
}
Vec2 [] subsetVec2(Vec2 [] vectors, int offset, int len) {
Vec2 [] temp = new Vec2[len];
System.arraycopy(vectors, offset, temp, 0, len);
return temp;
}

Usage would be along the lines of:

myPath = addVec2(myPath, newCoords);

Yes you have to make it all yourself in Processing. But at least you can have multi-dimensional arrays of anything and everything and don't have to dicker with that _root["clipName" + cantPutMovieClipInArraySoHeresANumberTackedOnTheEndOfAString].stuff() that's driving me up the wall in Flash right now.
Re: I need to understand.....
Reply #9 - Jan 14th, 2006, 2:50am
 
No, no, no!  You sure as hell don't want to expand the array by *1* element each time you insert something new, that's O(n^2) runtime!  Any speed gain you may have by accessing is going to be crushed to a powder by the speed loss from inserts.

The better approach is double the array size each time you exceed its capacity.  That gives you amortized O(n) runtime (an order of n faster!).  It should be noted that Java's Vector uses this method because it's almost stupid to do it any other way.

The only thing that may be slower is iterating a java.util.Vector since you have extra overhead of calling object methods.

It's worth noting though, a bunch of the java.util classes are Java 1.2+ only (Vector is an exception, although some useful methods were added in 1.2, such as toArray() which returns an Object[]), so you lose compatibility with older browsers (if processing ever regains 1.1 support, that is).  

In any case, use Vector if you want a dynamic array and don't go for the raw array method unless you absolutely need to.
Re: I need to understand.....
Reply #10 - Jan 14th, 2006, 10:59pm
 
cello wrote on Jan 14th, 2006, 2:50am:
No, no, no!  ...  It should be noted that Java's Vector uses this method because it's almost stupid to do it any other way.


Ok children, let's play nice.

More seriously, I've only recently been reading up on Java and learning about the casting problems when playing with dynamic arrays, but I find this whole business of "loosing" the class of the object when storing it in the Vector array awfully messy. Of course if you put it there in the first place you should able to figure out its cast, but this is not always the case.

In Objective-c there's a nice little method for asking an object its ilk, that way you can make loosely typed interfaces, which is a pretty damn powerful thing. Someone will probably jump up at this point and shout, "you can do that too in Java", and of course I'm all ears.

But whatever the case, I'm pretty frustrated with the Java classes for lists, arrays, vectors, call them what you will. I find it all horridly clumsy and very computer-science-ish. Arrays are so essential to any interresting work -- and dynamic arrays even more important -- that I feel so cramped working inside of Processing/Java's arrays. It feels like the days before Garbage Collection. I see that Sun keeps adding new forms of arrays with each new Java version, but they look just as difficult, each one. Can anyone point me to a nice, clean, dynamic array class in Java

I've also been spoiled with Lingo's brilliant #property lists which are similar to "key-valued pairs" in Objective-C and "maps" in Java. They're so easy to write, and so *readable* which is often more important for me that getting a clock-cycle or two out of code.

I also have become addicted to growing arrays inside of arrays almost infinitely in Lingo, often I get down to about about a dozen levels. It allows me to do some pretty subtle things with the user's interactions. This is just too messy in Java.

Or maybe I just don't have enough experience with all this yet.
Re: I need to understand.....
Reply #11 - Jan 15th, 2006, 1:09am
 
Well I agree on the Object casting thing, and I tend to use dynamic arrays in some cases where I need backward compatibility but speed.  Things have changed quite a bit in Java 1.5, especially teamed up with the Eclipse 3.1 IDE, it's very nice and easy to do:
Code:
List<String> list = new List<String>();
list.add("hello");
list.add("goodbye");
for (String s : list) {
// do something with s
}


Again, you are then forced into using Java 1.5 to run your stuff, but in certain circumstances it's worth it.

However, if you want to go for your lingo/actionscript style support, one could integrate Rhino (JavaScript interpretter) as I mentioned in another thread and get all the benefits of dynamic arrays/variables/classes and JavaScript's versatile syntax without comprimising any feature loss (maybe a slight speed hit, but it runs surprisingly well and can be compiled to native Java classes).

Almost tempts me to make a Processing-Rhino library or interface system, but I'm unfortunately too caught up in other projects at the moment.
Re: I need to understand.....
Reply #12 - Jan 15th, 2006, 3:32pm
 
cello wrote on Jan 15th, 2006, 1:09am:
Eclipse 3.1 IDE


Ah, but I'm using the Processing IDE. Easy to use...

cello wrote on Jan 15th, 2006, 1:09am:
Code:
List<String> list = new List<String>(); 



I'm not used to this syntax. I'll check it out.
Re: I need to understand.....
Reply #13 - Jan 15th, 2006, 6:36pm
 
They're referred to as Generics.  A few links:
http://java.sun.com/docs/books/tutorial/java/javaOO/generics.html
http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

The Processing IDE is easy to use, but you'd be surprised, the Eclipse IDE is even easier to use if you're familiar with Java.  

It does things like realtime syntax error checking (puts a little red underline ala Word), warning checking (oftentimes can automatically fix warnings), autocomplete and method listing (if you type "List list = new List(", it will show you all the possible constructors, if you type "list." it'll show you all the available methods for that object, and additionally display the java doc for the one you highlight), and a snootful of other things.  

What you do lose is the ease of exporting, although it has a wizard for creating jar files (and the wizard settings can be saved to allow for fairly trivial recreation of the same jar).

If you are doing anything more than a simple sketch (where processing's IDE makes things really simple), I highly recommend it.  You can even pull the .java file that the processing IDE makes as a starting point for your eclipse project.

As far as I know, the Processing IDE does not support 1.5 features yet.  I haven't looked into it, because I'm personally not willing to sacrifice applet compatibility for Java 1.5's generics right now, though.

I think if you're developing a standalone application, it's fine to use generics, because it's easier to require the additional Java 1.5 download if they don't already have it.  However, for the applet environment, arguably some people are still using 1.1.

Marcello
Re: I need to understand.....
Reply #14 - Jan 15th, 2006, 6:42pm
 
Hrmm, where on earth did I read that deep-copy was faster. I've run a test - the results say Vector();
Code:

int iter=10000;
Vec2 [] test = new Vec2[1];
Vec2 temp;
Vector best;
void setup(){
test[0] = new Vec2(0,0);
temp = new Vec2(0,0);
best = new Vector();
int t0=millis();
for(int i=0; i<iter; i++) best.add(temp);
println("Vector method : "+(millis()-t0)+"ms");
t0=millis();
for(int i=0; i<iter; i++) test = addVec2(test, temp);
println("deep-copy method : "+(millis()-t0)+"ms");
}
class Vec2{
float x,y;
Vec2(){
}
Vec2(float x, float y){
this.x = x;
this.y = y;
}
}
Vec2 [] addVec2(Vec2 [] vectors, Vec2 newVector) {
Vec2 [] temp = new Vec2[vectors.length + 1];
System.arraycopy(vectors, 0, temp, 0, vectors.length);
temp[vectors.length] = newVector;
return temp;
}
Pages: 1 2