简体   繁体   English

鼠标移动-十字光标

[英]mouse moved -crosshair cursor

I developed a program to draw polygon triangles. 我开发了一个程序来绘制多边形三角形。 The triangles were drawn using mouse drag. 使用鼠标拖动绘制三角形。 The coordinate of the triangles were stored in array list. 三角形的坐标存储在数组列表中。 Every times the mouse cursor, mouse over on the existing drawn triangles(within the area of triangle), the mouse cursor should turns to "CROSSHAIR_CURSOR", however this were not happened. 每次将鼠标光标悬停在现有绘制的三角形(在三角形区域内)上时,鼠标光标应变为“ CROSSHAIR_CURSOR”,但这并未发生。 Help :-( 救命 :-(

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

Do you see the mouseMoved method being invoked at all? 您是否看到mouseMoved方法被调用了? The way this is written, the mouseMoved method is a member of PaintSurface, but PaintSurface is not a MouseMotionListener. 编写方式上,mouseMoved方法是PaintSurface的成员,但是PaintSurface不是MouseMotionListener。 Implementing 'MouseMotionListener' will force it to implement mouseMoved and mouseDragged . 实现'MouseMotionListener'将强制其实现mouseMovedmouseDragged After you have done that, you can add your PaintSurface to itself as a MouseMotionListener . 完成此操作后,可以将PaintSurface添加为MouseMotionListener Alternatively, you could move the mouseMoved method inside the MouseMotionAdapter anonymous class that you have already defined: 另外,您可以在已经定义的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 (in response to your comment): 编辑(针对您的评论):

It would appear that your conditional if (triangles.contains(startDrag)) depends on the List<Polygon> finding a Point that considers itself equal to the passed in Point . 这样看来,你的条件if (triangles.contains(startDrag))依赖于List<Polygon>找到一个Point是认为自己等于在传递Point As far as I can tell from looking at the code in Polygon (it doesnt override the equals method, so it takes the implementation from Object ), you will not be able to perform this test 'successfully.' 据我从查看Polygon中的代码可以看出(它不会覆盖equals方法,因此它采用Object的实现),您将无法“成功”执行此测试。 You will need to iterate over your Polygon s in your triangles collection and perform a contains operation on each in turn. 您将需要遍历您triangles集合中的Polygon ,并依次对每个Polygon执行一次contains操作。

EDIT 2: 编辑2:

You are probably over-thinking this a bit. 您可能对此有点想过。 In order to implement the suggestion 'to iterate over your Polygon s in your triangles collection...' you could do something like the following: 为了实现“迭代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 

You could also consider not setting the cursor with every mouse movement. 您也可以考虑不要在每次鼠标移动时都设置光标。 For that, you can put a test in to check the type of the current cursor and the type of the cursor that your mouse movement is intending to set, and only set it if there is a change: 为此,您可以进行测试以检查当前光标的类型以及要设置鼠标移动的光标的类型,并且仅在发生更改时进行设置:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM