I'm using eclipse and have started playing with multiple classes.
I have a Base class which extends PApplet (this is where void setup() and void draw() are). I then pass a reference to the Base object when initialising the new generic Object Class. This way I can generate loads of circle objects etc.
How can I get the object class to redraw once every frame?
I could make an update method manually.. and call obj.update() from void draw()
This would work but if I had say ten objects.. obj1, obj2 etc etc then I would have to call update() on all of them separately.
I have tried letting the object class call it's own update() (so that it can draw ellipses or rects itself) but I run into the problem of it drawing more than once per frame.
you can add a draw() method to your custom class and in it's constructor call registerDraw(this) on the Base class. that way the draw() method of your custom objects will be automatically called after draw() in the Base class.
"This would work but if I had say ten objects.. obj1, obj2 etc etc then I would have to call update() on all of them separately." Yes. That's not a problem. Particularly if you put the objects in an array.
registerDraw() is nice and handy. The registerXxx() methods are mostly designed for libraries that can't put their own calls in keyPressed() and such in the sketches and don't want to ask the user to do it for them.
But they have a cost: it is based on reflection, ie. Java has to do introspection to find the methods by name. It is generally slower than a straightforward method call. That's not a problem with a few objects, it can be more problematic with thousands of objects (eg. a particle system). Just something to keep in mind...
Could be interesting to measure if it has a real impact, though...
Note: a more classical way is to have objects to implement an interface, eg. defining a draw() or a display() method. Then, whatever the objects created, you put all the objects implementing the interface in an array list, and in draw() you iterate on the list to call the interface's method.
Thanks for having setting up this test. I rewrote it to make it a pure Processing sketch, and one sketch only. I see perhaps a very small advantage to the "classical way" (calling draw() manually on each object. The interface is just an implementation detail), but it isn't flagrant or even consistent... In general, the overhead of reflection is near zero with regard to the time needed to draw the scene. Good to know. Now, I think I will still use the classical way, because we still need to loop over the list of objects to update them, etc. And if we remove objects, we have to remember to unregister them first. At least, the classical way is consistent over the processing of objects, and we can control when, in the draw() function, we call it (sometime, order of drawing is important). But that's just my choice, and this registerXxx method is handy for libraries and smart.
My modified code:
ArrayList<TestObject> list = new ArrayList<TestObject>();