Processing: How to add multiple getchild() objects in an arraylist

144 Views Asked by At

I am trying to disablestyle() multiple children of an svg image with a mouse click. every time you click the next child gets disablestyle(). Only I cannot add multiple children in an array or arraylist. Any tips?

1

There are 1 best solutions below

0
On

You can create a generic array list like so:

ArrayList shapes = new ArrayList();

and a typed array list like so:

ArrayList<PVector> shapes = new ArrayList<PVector>();

To add an element simply call .add():

shapes.add(myPShape);

To check the length of the list use size()

println("shapes has: " + shapes.size() + " element(s)");

The first practical difference comes when retrieving data from the array list. If using the generic version, you will need to cast it:

PShape firstShape = (PShape)shapes.get(0));

Also notice you use .get() to retrieve an array list element by index (instead of [] you might be familiar with from arrays).

You don't need to this for the typed version (ArrayList<PVector> shapes):

PShape firstShape = shapes.get(0);

There are cases where one option is more practical or more flexible than the other and this mainly depends on the scope of your program.

Personally for small programs I use a typed one (though in larger programs using multiple classes and polymorphism the generic one is more useful).

For more info on what other things ArrayList do check the javadocs