Click and remove a specific item from an ArrayList of Objects

edited July 2016 in Questions about Code

Hello, i'm new to processing and i'm still figuring it out!

I want to click a specific item on screen and remove it from an ArrayList. So far I can remove only the 0th item from the list. This is obvious since i've something like this:

 if (mouseButton == RIGHT) {
        if(bubbles.size() > 1) {
          if(mouseX> ballX-radius*2 && mouseX < ballX+radius
          && mouseY> ballY-radius   && mouseY < ballY+radius){
            bubbles.remove(0);   ////HERE BE PROBLEMS
           }

I cannot understand how to select whatever object from the ArrayList

I've got other problems in the code, but if you guys help me out with the remove problem i should be able to get the other parts right.

here is the complete code.

[see below]

Answers

  • ctrl-t in the editor will indent your code nicely and make the code structure easier to see.

  • here is the code indented with ctrl+t! sorry for the bad formatting

    ArrayList<Bubble> bubbles;
    int ballRadius = 20;
    boolean mouseOver;
    boolean down = false;
    boolean dragging = false;
    PFont mono;
    int x=30;
    
    
    void setup() {
      size(500, 500);
      fill(0);
      mono = createFont("monos.ttf", 20);
      textFont(mono);
      fill(255);
      noStroke();
      ellipseMode(RADIUS);
      bubbles = new ArrayList<Bubble>();
      bubbles.add(new Bubble(250, 250, 20));
    }
    
    void draw() {
      background(255);
    
      for (int i = 0; i<bubbles.size(); i++) {
        Bubble bubble = bubbles.get(i);
    
        bubble.drag();
    
        bubble.click();
    
        bubble.display();
      }
    }
    
    void mousePressed() {
      down = true;
    }
    
    void mouseDragged() {
      dragging = true;
    }
    
    void keyTyped() {
      textSize(20);
      text(key, mouseX+x, mouseY);
      x=x+11;
    }
    
    class Bubble {
      float ballX;
      float ballY;
      int radius = 20;
      int colore = 255;
    
    
      Bubble(float ballXTemp, float ballYTemp, int radius) {
        ballX = ballXTemp;
        ballY = ballYTemp;
      }
    
    
      void display() {
        smooth();
        noStroke();
        fill(125);
        ellipse(ballX+5, ballY+5, radius*2, radius);
        stroke(0);
        fill(colore);
        ellipse(ballX, ballY, radius*2, radius);
    
        if (mouseX> ballX-radius*2 && mouseX < ballX+radius
          && mouseY> ballY-radius   && mouseY < ballY+radius) {
          colore=125;
        } else {
          colore=255;
        }
      }
    
      void click() {
    
        if (!dragging && down ) {
    
          if (mouseButton == LEFT) {
    
            bubbles.add(new Bubble(mouseX, mouseY, 10));
          }
    
    
          if (mouseButton == RIGHT) {
            if (bubbles.size() > 1) {
              if (mouseX> ballX-radius*2 && mouseX < ballX+radius
                && mouseY> ballY-radius   && mouseY < ballY+radius) {
                bubbles.remove(0);
              }
            }
          }
        }
        down = false;
        dragging = true;
      }
    
    
      void drag() {
        if (mouseX> ballX-radius*2 && mouseX < ballX+radius
          && mouseY> ballY-radius   && mouseY < ballY+radius) {
          if (keyPressed) {
            if (key == 'S' || key == 's') {
              if (dragging) {
    
                if (mouseButton == LEFT) {
                  {
                    ballX = mouseX;
                    ballY = mouseY;
                  }
                }
              }
            }
          }
        }
        dragging = false;
      }
    }
    
  • edited July 2016

    It shouldn't be to hard to fix. Loop, using a for statement and then to check for nearness by calling bubbles.get(i).x

    Actually, I see that you are using a method inside the class to delete itself from a structure outside of the class you have. I suggest moving that outside, as getting information about where a class is in an ArrayList might be messy. If you use something like

    void mouseClicked() {
       if (mouseButton == RIGHT) {...
         for(...
         ...
         ...
    }
    

    or

    void mousePressed() {
       if (mouseButton == RIGHT) {...
         for(...
         ...
         ...
    }
    

    Then, inside of that, you can loop through the elements with a for(int i = 0; i<bubbles.size();...

    And then use a new class specific function

    bubbles.get(i).clickedInside(mouseX,mouseY);

    to access the ith element of the ArrayList and call a function that returns true or false depending on whether you clicked inside the circle or not.

    After that, if the function returns something signifying that you did click in the ith circle, then

    bubbles.remove(i);
    

    should do the trick by removing the ith element of the ArrayList, the one you clicked. (If you used a .clickedInside(mouseX,mouseY); that returns a boolean, you can stick it inside the parenthesis following an if statement and put the bubbles.remove(i); in the curly braces.)

Sign In or Register to comment.