Jessy
Jessy

Reputation: 15661

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

Upvotes: 0

Views: 5261

Answers (1)

akf
akf

Reputation: 39485

Do you see the mouseMoved method being invoked at all? The way this is written, the mouseMoved method is a member of PaintSurface, but PaintSurface is not a MouseMotionListener. Implementing 'MouseMotionListener' will force it to implement mouseMoved and mouseDragged. After you have done that, you can add your PaintSurface to itself as a MouseMotionListener. Alternatively, you could move the mouseMoved method inside the MouseMotionAdapter anonymous class that you have already defined:

//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. 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.' You will need to iterate over your Polygons in your triangles collection and perform a contains operation on each in turn.

EDIT 2:

You are probably over-thinking this a bit. In order to implement the suggestion 'to iterate over your Polygons in your triangles collection...' you could do something like the following:

 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);
    }

Upvotes: 2

Related Questions