1

As it is a first time I'm using ArrayList in Processing I'm experiencing some issues.

I have created an Arraylist that stores a PVector (x,y position) of an Ellipsee.

What I'm trying to do is very simple I think but I can't find much info on ArrayLists.

Code: ArrayList position;

void setup() 
{
   position= new ArrayList<Vectors>();
}

void draw() 
{
   position.get(i).display(); //display ellipse
}

void mousePressed() 
{
   position.add(new Vectors(new PVector(mouseX, mouseY)));
}

So every time mouse is Pressed a new ellipse is created at mouseX mouseY position. What I would like to do is when my I created an amount ellipses, I need to control each one separately to change it's size or color either by clicking on them or with KeyPressed().

1
  • Please add your code. Commented Nov 23, 2014 at 20:51

1 Answer 1

2

This won't compile automatically as I'm assuming your PVector object has already been created and that it has two public attributes of xPosition and yPosition:

// Initialise your arraylist
ArrayList<PVector> listOfPVectors = new ArrayList<PVector>;

// Objects can be added to your list as follows:
PVector pvectorObject = new PVector();  
listOfPVectors.add(pvectorObject);

// The size of your ArrayList can be output as follows:
println(listOfPVectors.size());

// You can iterate through every entry in the arraylist as follows:
for(int index = 0; index < listOfPVectors.size(); index ++) {
    println("X Position Value = " + listOfPVectors.get(index).xPosition);
    println("Y Position Value = " + listOfPVectors.get(index).yPosition);
}

Basically, you use the ArrayList.get(indexPosition) method to retrieve any element you want from your ArrayList. You can then work away with it as normal.

Hope this helps!

Sign up to request clarification or add additional context in comments.

6 Comments

Hi Anthony thank you for your answer. Does the getXPosition() work in Processing?
Hi, I edited my answer to suit Processing a bit more clearly. Hopefully it's a bit clearer now (I removed the getter methods for getXPosition() and getYPosition()). Let me know if this helps :)
Thank you Anthony it does help me. However I think to print the values in processing is: println("Y Position Value = " + listOfPVectors.get(index).x);
It depends on what you called those attributes in the PVector object. In my assumption above, I have called them xPosition and yPosition but if you have used simple x and y variables for these, then that's fine, too. Use those instead.
Thank you Anthony. I manage to do it. Just one question if you can help me. I manage resize the ellipse on mousePressed using position.get(i).display(r*2);. However when the mouse is released it goes back to normal. Do you have any idea how to fix this?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.