如何用鼠标单击删除对象?

时间:2010-03-24 18:52:11

标签: java

我制作了一个简单的FlowChat编辑器,它可以创建矩形和三角形,并将它们相互连接起来,并显示从上到下的方式。我也可以在屏幕上移动这些元素。

我现在正在尝试创建一个按钮来删除我单击的元素。有问题我可以删除MyTriangle个对象,但我无法删除MyRectangle个对象。它删除但不是我点击的对象。我从第一个对象删除到最后一个。

这是我的代码:

 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");

                }

            }
        }

编辑这里是MyRectangle和MyTriangle类

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);
}
}

修改2

这里我的funcs将对象添加到list.Is可能存在ant错误?因为我是她的ceratıng那个calss的新对象,我将该对象添加到rectlist或trianglelist中..

    public void initRect(Point c, Point in, Point out) {
    sCounter++;
    MyRect myrects = new MyRect(c, in, out, sCounter);

    rectsList.add(myrects);
    s_And_t_List.add(myrects);

    objectCounter.add("S " + sCounter);

    selectablePanel.repaint();
}

public void initTriangle(Point c, Point in, Point outYES, Point outNO) {
    tCounter++;
    MyTriangle mytriangles = new MyTriangle(c, in, outYES, outNO, tCounter);

    triangleList.add(mytriangles);
    s_And_t_List.add(mytriangles);
    objectCounter.add("T " + tCounter);
    selectablePanel.repaint();
}

1 个答案:

答案 0 :(得分:0)

看起来你的逻辑错了。在Rectangle类中,为什么不让一个方法返回一个布尔值来测试对象中是否包含一组给定的坐标。例如:

public boolean contains(int x, int y){  
    if(x_starting_point <= x && x <= x_starting_point+width
    && y_starting_point <= y && y <= y_starting_point+height)
        return true; 

    return false;
}