2010-03-24 53 views
0

我做了一個簡單的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有可能是螞蟻的錯誤?因爲我是她的角色的新對象,我是addıng該物體直接列表或三角形列表。

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(); 
} 
+0

是('rect.c.x','rect.c.y')矩形的中心還是左上角? – 2010-03-24 19:02:01

+0

它是中心。它是鼠標點擊的位置,它將拉力作爲中心點。 – Ercan 2010-03-24 19:02:53

+0

發佈MyRect和MyTriangle類 – Iraklis 2010-03-24 19:05:02

回答

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; 
} 
+1

這是爲了檢查我發現的對象?但我的代碼工作too.It可以找到確切的對象 – Ercan 2010-03-24 20:30:48

+0

我假設'rect.cx'意味着他的矩形的中心,所以它是有道理的,他是減去並添加一半寬度得到的範圍,我猜... – 2010-03-24 20:33:30

+0

是的..我thinf rect.cx-50 Ercan 2010-03-24 20:39:45