Results 1 to 2 of 2
  1. #1
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default change the mouse cursor - 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 triangles), the mouse cursor should turns to "CROSSHAIR_CURSOR", however this was not happened. Help :-(

    Java Code:
    ...
        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     
    
        //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
    
          });//end this.addMouseMotionListener
        }//end paintSurface       
    
         private void paintBackground(Graphics2D g2){
         ...
         }
    
         public void paint(Graphics g) {
         ...
         }
    
        }//end private class PaintSurface
    
        }//end public class DrawingBoardMatrix

  2. #2
    cassysumandak is offline Member
    Join Date
    Mar 2009
    Posts
    38
    Rep Power
    0

    Default

    Never mind, the problem has been solved.
    Java Code:
        		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);
        		}

Similar Threads

  1. Need to change cursor to hourglass at server side
    By bgupta in forum Java Servlet
    Replies: 0
    Last Post: 02-16-2009, 08:35 AM
  2. Cursor Size
    By ScottVal in forum AWT / Swing
    Replies: 1
    Last Post: 01-03-2009, 07:11 AM
  3. Cursor
    By serfster in forum New To Java
    Replies: 2
    Last Post: 06-15-2008, 06:49 AM
  4. GUI cursor change problem
    By ludragon in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2008, 11:03 PM
  5. Using a cursor
    By notnumber6 in forum New To Java
    Replies: 3
    Last Post: 11-26-2007, 11:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •