鼠标移动-crosshair光标

时间:2009-09-27 18:49:21

标签: java swing mouse

我开发了一个绘制多边形三角形的程序。使用鼠标拖动绘制三角形。三角形的坐标存储在数组列表中。每次鼠标光标,鼠标悬停在现有的绘制三角形上(在三角形区域内),鼠标光标应该变为“CROSSHAIR_CURSOR”,但这并没有发生。帮助:-(

   ...
    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

1 个答案:

答案 0 :(得分:2)

您是否看到正在调用mouseMoved方法?编写它的方法是,mouseMoved方法是PaintSurface的成员,但PaintSurface不是MouseMotionListener。实现'MouseMotionListener'将强制它实现mouseMovedmouseDragged。完成后,您可以将PaintSurface添加为MouseMotionListener。或者,您可以在已定义的mouseMoved匿名类中移动MouseMotionAdapter方法:

//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       

编辑(回应你的评论):

您的条件if (triangles.contains(startDrag))似乎取决于List<Polygon>发现Point认为自己等于传入的Point。据我所知,Polygon中的代码(它没有覆盖equals方法,因此需要Object的实现),你将无法执行此操作测试'成功'。您需要对Polygon集合中的triangles进行迭代,然后依次对每个contains进行Polygon操作。

编辑2:

你可能过度思考了这一点。为了实现“在您的triangles集合中迭代 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);
    }

您还可以考虑不在每次鼠标移动时设置光标。为此,您可以进行测试以检查当前光标的类型以及鼠标移动要设置的光标类型,并且只有在有更改时才设置它:

{{1}}