1 Reply Latest reply: Jan 14, 2014 3:11 AM by TPD-Opitz RSS

    Change cursor on mouseover a rectangle

    Bobbytown

      I want that the cursor change when the mouse is over one of the rectangles. Rectangles are displayed but nothing happens when the mouse is over.

       

      classResizing extendsJPanel { 
       
          java.awt.List myGuiList = newjava.awt.List(); 
          java.util.List<Rectangle> paths = newArrayList<Rectangle>(); 
       
          publicResizing() { 
              paths.add(newRectangle(100, 100, 75, 50)); 
              paths.add(newRectangle(200, 100, 75, 50)); 
         
       
          @Override
          protectedvoidpaintComponent(Graphics g) { 
              super.paintComponent(g); 
                  Graphics2D g2 = (Graphics2D)g; 
                  g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 
                  g2.setPaint(Color.blue); 
                  for(intj = 0; j < paths.size(); j++) { 
                      Rectangle path = paths.get(j); 
                      g2.draw(path); 
                 
         
       
          publicstaticvoidmain(String[] args) { 
                  JFrame f = newJFrame(); 
                  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
                  f.add(newResizing()); 
                  f.setSize(400,400); 
                  f.setLocation(100,100); 
                  f.setVisible(true); 
       
         
       
          classResizer extendsMouseAdapter { 
          Resizing component; 
          booleandragging = false
       
          publicResizer(Resizing rz) { 
              component = rz; 
              component.addMouseListener(this); 
              component.addMouseMotionListener(this); 
         
       
          publicvoidmouseMoved(MouseEvent e) { 
              Point p = e.getPoint(); 
              Cursor cursor = Cursor.getDefaultCursor(); 
              for(Rectangle r : paths) { 
              if(r.contains(p)) { 
                 cursor = Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR); 
                 break
             
           
            setCursor(cursor); 
         
      }}
        • 1. Re: Change cursor on mouseover a rectangle
          TPD-Opitz

          Why do you think your mouseMoved Method will be called?

           

          mouseMoved Method is part of the MouseMotionListener interface which tells us 2 things:

          1. The Object having the mouseMoved Method must implement MouseMotionListener interface or extend MouseMotionAdapter class
          2. The Object implementing MouseMotionListener interface or extend MouseMotionAdapter class must be registered with an object able to deliver MouseMotionEvents.

           

          In your code I don't see neither one.

           

          Also I doupt that the MouseMotionEvent is be the right choice. I'd rather look at MouseListener (and its default implementation MouseAdapter).

           

          bye

          TPD