Delete Image with mousePressed

edited April 2014 in How To...

Hello everyone,

I need some advice about how I can delete an image with the command mousePress. In my code I first create an illustration by clicking with the mouse cursor on the screen. Later I would like to delete it again by clicking again on the object on the screen. Since I didn't find another solution, I programmed that by drawing a rectangle with the same color as the background, the image disappear. Probably there are many better ways how to delete the image.

Thank you!

 void mousePressed(){
            if (mousePressed && (mouseButton == LEFT)) { 
          //Draw the image to the screen at coordinates where clicked
          image(plant,mouseX,mouseY);
          }
          else if (mousePressed && (mouseButton == RIGHT)){
          //overdraws the image to the screen at coordinates where clicked
          fill(#BCFCA1);
          noStroke();
          rect(mouseX,mouseY,120,100);
          }
            // update the screen (run draw once)
          redraw();
        }

Answers

  • You need to keep track of where you placed images. With each mouse click, check if the mouse is nearby a previously clicked position, then remove the image at this position.

    The number of images you want is not fixed, so you want to use an ArrayList.

    ArrayList<PVector> positions;
    int radius = 10;
    
    void setup()
    {
      size(600, 600);
      smooth();
      fill(255);
    
      positions = new ArrayList<PVector>();
    }
    
    void draw()
    {
      background(0);
    
      //Draw an ellipse at every recorded position: 
      for (PVector position : positions)
      {
        ellipse(position.x, position.y, radius, radius);
        //image(plant, position.x, position.y);
      }
    }
    
    void mousePressed() {
      if (mouseButton == LEFT) {
        //Add the clicked position to the list:
        positions.add(new PVector(mouseX, mouseY));
      }
      else if (mouseButton == RIGHT) {
        //Check if you clicked on an image: 
        for (int i = 0; i < positions.size(); i++)
        {
          PVector position = positions.get(i);
          PVector mousePosition = new PVector(mouseX, mouseY);
          if (PVector.dist(position, mousePosition) < radius*0.5)
          {
            positions.remove(i);
            i = positions.size(); //Jump to the end of the for loop to only remove one image
          }
        }
      }
    }
    

    This works best for circles. If you really want to check if you clicked on an image, you could determine the colour of the pixel you clicked on and check if it is different from the background.

  • edited April 2014

    Thank you, that helps me a lot.

  • edited April 2014

    xxxx

  • edited April 2014

    xxxx

  • Sorry, that I ask again. I'm new at processing and do not have so much experience with programming. My illustration has three colors (red, dark green, brown) and the background light green.

    If I want to check compare the colors and remove it when different from the background, do I need to replace "position" by "colors" and then compare them by something like:

    if (distance < oldDist ) { 
    colorTo = colorTarget;
    }
    oldDist = distance;
    

    Thanks again.

  • edited April 2014

    xxxxxx

  • What are these xxx messages?

    I don't understand fully the second question, but perhaps the get() function can help you to get the color at a given position.

Sign In or Register to comment.