2009-09-27 252 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