2009-09-27 251 views
0

我開發了一個繪製多邊形三角形的程序。三角形使用鼠標拖動繪製。三角形的座標存儲在數組列表中。每次鼠標光標,將鼠標懸停在現有繪製的三角形上(在三角形區域內),鼠標光標應該變成「CROSSHAIR_CURSOR」,但是這不會發生。幫助:-(鼠標移動-crosshair光標

... 
    public class DrawingBoardWithMatrix extends JFrame { 
     public static void main(String[] args) { 
     new DrawingBoardWithMatrix(); 
     } 

    public DrawingBoardWithMatrix(){ 
     this.add(new PaintSurface(), BorderLayout.CENTER); 
     ... 
    } 

    private class PaintSurface extends JComponent { 
     java.util.List<Polygon> triangles = new LinkedList<Polygon>(); 
     Point startDrag, endDrag, midPoint; 
     Polygon triangle; 

     public PaintSurface() { 
     ... 
     this.addMouseListener(new MouseAdapter() { 
     public void mousePressed(MouseEvent e) { 
      startDrag = new Point(e.getX(), e.getY()); 
      endDrag = startDrag; 
      repaint(); 
     }//end mousePressed 

     public void mouseReleased(MouseEvent e) { 
      if (startDrag.x > endDrag.x) 
      midPoint = new Point((endDrag.x +(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 
      else 
      midPoint = new Point((endDrag.x -(Math.abs(startDrag.x - endDrag.x)/2)),e.getY()); 

      int[] xs = { startDrag.x, endDrag.x, midPoint.x }; 
      int[] ys = { startDrag.y, startDrag.y, midPoint.y };  
      triangles.add(new Polygon(xs, ys, 3));  

      startDrag = null; 
      endDrag = null; 
      repaint(); 
     }//end mouseReleased    
     });//end addMouseListener 

     this.addMouseMotionListener(new MouseMotionAdapter() { 
     public void mouseDragged(MouseEvent e) { 
      endDrag = new Point(e.getX(), e.getY()); 
      repaint(); 
     }//end mouseDragged  
     });//end this.addMouseMotionListener 
    }//end paintSurface  

    //THIS CODE DOESNT WORK - AND I AM STUCK :-(  
    public void mouseMoved(MouseEvent e) { 
     startDrag = new Point(e.getX(), e.getY()); 
     if (triangles.contains(startDrag)) 
     setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 
     else 
     setCursor(Cursor.getDefaultCursor()); 
    }// end mouseMoved 

    private void paintBackground(Graphics2D g2){ 
    ... 
    } 

    public void paint(Graphics g) { 
    ... 
    } 

    }//end private class PaintSurface 

    }//end public class DrawingBoardMatrix 

回答

2

你看到mouseMoved方法都被調用呢?這是書面的方式,方法的mouseMoved是PaintSurface中的一員,但PaintSurface不是的MouseMotionListener。實施「的MouseMotionListener」將迫使它以實現mouseMovedmouseDragged你已經這樣做了之後,您可以將您的PaintSurface將自己作爲一個MouseMotionListener添加或者,你可以移動MouseMotionAdapter匿名類裏面的mouseMoved方法您已經定義:。

//paintSurface constructor 
.... 
this.addMouseMotionListener(new MouseMotionAdapter() { 
    public void mouseDragged(MouseEvent e) { 
     endDrag = new Point(e.getX(), e.getY()); 
     repaint(); 
    }//end mouseDragged  

    //TRY THIS CODE :-)  
    public void mouseMoved(MouseEvent e) { 
     startDrag = new Point(e.getX(), e.getY()); 
     if (triangles.contains(startDrag)) 
     setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); 
     else 
     setCursor(Cursor.getDefaultCursor()); 
    }// end mouseMoved 
});//end this.addMouseMotionListener 
}//end paintSurface  

EDIT(響應您的評論):

這樣看來,你的條件if (triangles.contains(startDrag))依賴於List<Polygon>找到Point是認爲自己等於Point傳遞。據我所知,通過查看Polygon中的代碼(它不覆蓋equals方法,因此需要從Object執行),您將無法「成功」執行此測試。您需要遍歷triangles集合中的Polygon,然後依次對每個集合執行contains操作。編輯2:

你可能在想這一點。爲了落實的建議「在您的收藏triangles遍歷您Polygon個... ...」,你可以這樣做以下:

public void mouseMoved(MouseEvent e) { 
     startDrag = new Point(e.getX(), e.getY()); 
     Cursor cursor = Cursor.getDefaultCursor(); 
     //you have a List<Polygon>, so you can use this enhanced for loop 
     for (Polygon p : triangles) { 
     if (p.contains(startDrag)) {//Polygon has a 'contains(Point)' method 
      cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); 
      break; //you've found a hit, break from the loop 
     } 
     } 
     setCursor(cursor); 
}// end mouseMoved 

你也可以考慮不與每一個鼠標移動設置光標。對於這一點,你可以把一個測試來檢查當前光標和你的鼠標移動有意設置的遊標類型的類型,只設置它,如果有變化:

if (cursor.getType() != getCursor().getType()) { 
     setCursor(cursor); 
    } 
+0

由於AKF 。我已經改變了addMouseMotionListener中mouseMoved方法的位置,但它仍然不起作用:-( – Jessy 2009-09-27 20:14:52

+0

Jessy,我已經更新了我的答案,請看一下 – akf 2009-09-27 20:33:25

+0

再次感謝akf。但是這聽起來很複雜,我會嘗試修改代碼 – Jessy 2009-09-27 21:18:13