How to get position of an ArrayList of objects? + IndexOutOfBoundsException

edited March 2016 in Questions about Code

I want to make a program to show an object (Macabeu) 10 times in random position and, when the user clicks on one of the objects, it desappears. I have made this code:

ArrayList<Macabeu> macabeus ;
int[] X =new int[10];
int[] Y =new int[10];

void setup() {
  size(450, 450);
  macabeus=  new ArrayList<Macabeu>();
  for (int i=0; i<10; i++) {
    macabeus.add(new Macabeu());
  }
}

void draw() {
  background(0, 0, 0);
  for (int i=0; i<10; i++) {
    Macabeu tmp = macabeus.get(i);
    X[i]=tmp.x;
    Y[i]=tmp.y;

    println(X[i], ",", Y[i]);

    tmp.pintar();
  }
}

void mouseClicked() {
  for (int i =0; i<macabeus.size(); i++) {
    if (dist(mouseX, mouseY, X[i], Y[i])<30)
    {
      macabeus.get(i);
      macabeus.remove(i);
    }
  }
}

And I made this class in a new tab:

class Macabeu {
  PImage mac;
  int x;
  int y;

  Macabeu() {
    x=int(random(0, 400)); 
    y=int(random(0, 400));
  }

  void actualizar() {
  }

  void pintar() {
    mac = loadImage("macabeu.png"); 
    imageMode(CENTER);
    image(mac, x, y, 60, 60);
  }
}

It doesn't work and an error message appears: "IndexOutOfBoundsException: Index:9, Size:9" and marks the line 19 which is " Macabeu tmp= macabeus.get(i); "

I don't have idea what to do :( Can anybody help me with this?

Answers

Sign In or Register to comment.