How to delete object with a mouse click ?

Posted by Meko on Stack Overflow See other posts from Stack Overflow or by Meko
Published on 2010-03-24T18:52:10Z Indexed on 2010/03/24 20:33 UTC
Read the original article Hit count: 157

Filed under:

Hi all.

I made a simple FlowChat Editor that creates rectangles and triangles and connects them to each other and shows the way from up to down. I can move this elements on screen too.

I am now trying to create a button to delete the element which I clicked. There is problem that I can delete MyTriangle objects, but I can't delete MyRectangle objects. It deletes but not object which I clicked. I delete from first object to last.

Here is my code:

 if (deleteObj) {

        if (rectsList.size() != 0) {
            for (int i = 0; i < rectsList.size(); i++) {
                MyRect rect = (MyRect) rectsList.get(i);
                if (e.getX() <= rect.c.x + 50 && e.getX() >= rect.c.x - 50
 && e.getY() <= rect.c.y + 15 && e.getY() >= rect.c.y - 15) {
                    rectsList.remove(rect);
                    System.out.println("This is REctangle DELETED\n");

                }

            }
        }
        if (triangleList.size() != 0) {
            for (int j = 0; j < triangleList.size(); j++) {
                MyTriangle trian = (MyTriangle) triangleList.get(j);

                if (e.getX() <= trian.c.x + 20 && e.getX() >= trian.c.x - 20 
&& e.getY() <= trian.c.y + 20 && e.getY() >= trian.c.y - 20) {
                    triangleList.remove(trian);
                    System.out.println("This is Triangle  Deleted\n");

                }

            }
        }

Edit Here MyRectangle and MyTriangle classes

public class MyRect extends Ellipse2D.Double {

Point c;
Point in;
Point out;
int posX;
int posY;
int width = 100;
int height = 30;
  int count;


public MyRect(Point center, Point input, Point output,int counter) {

    c = center;
    in = input;
    out = output;
    count=counter;

}

void drawMe(Graphics g) {
    //    in.x=c.x+20;
    int posX = c.x;
    int posY = c.y;
    int posInX = in.x;
    int posInY = in.y;
    int posOutX = out.x;
    int posOutY = out.y;




    g.setColor(Color.MAGENTA);
    g.drawString(" S "+count ,posX-5, posY+5);


    g.setColor(Color.black);
    g.drawRect(posX-50, posY-15, width, height);
    g.setColor(Color.green);
    g.drawRect(posInX-3, posInY-9, 6, 6);
    g.setColor(Color.blue);
    g.drawRect(posOutX-3, posOutY+3, 6, 6);


}
}

public class MyTriangle {

Point c;
Point in ;
Point outYES ;
Point outNO ;
int posX;
int posY;
int count;

public MyTriangle(Point center,Point input,Point outputYES,Point outputNO,int counter)     {

    c = center;
    in = input;
    outYES = outputYES;
    outNO = outputNO;
    count=counter;
}

void drawMe(Graphics g) {

    int posX = c.x;
    int posY = c.y;
    int posInX=in.x;
    int posInY=in.y;
    int posOutYESX=outYES.x;
    int posOutYESY=outYES.y;
    int posOutNOX=outNO.x;
    int posOutNOY=outNO.y;

    int[] xPoints = {posX - 50, posX, posX + 50, posX};
    int[] yPoints = {posY, posY - 30, posY, posY + 30};

     g.setColor(Color.MAGENTA);
    g.drawString(" T "+count,posX-5, posY+5);
    g.setColor(Color.black);
    g.drawPolygon(xPoints, yPoints, 4);
   // draw input
    g.setColor(Color.green);
    g.drawRect(posInX-3,posInY-9, 6, 6);
    g.setColor(Color.blue);
    g.drawRect(posOutYESX-9,posOutYESY-3 , 6, 6);
    g.setColor(Color.red);
    g.drawRect(posOutNOX-3,posOutNOY+3 , 6, 6);
}
}

© Stack Overflow or respective owner

Related posts about java