Results 1 to 3 of 3
  1. #1
    bobbytown is offline Member
    Join Date
    Jan 2014
    Posts
    5
    Rep Power
    0

    Default Change cursor on mouseover a rectangle

    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.

    Java Code:
        class Resizing extends JPanel {  
          
            java.awt.List myGuiList = new java.awt.List();  
            java.util.List<Rectangle> paths = new ArrayList<Rectangle>();  
          
            public Resizing() {  
                paths.add(new Rectangle(100, 100, 75, 50));  
                paths.add(new Rectangle(200, 100, 75, 50));  
            }  
          
            @Override  
            protected void paintComponent(Graphics g) {  
                super.paintComponent(g);  
                    Graphics2D g2 = (Graphics2D)g;  
                    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);  
                    g2.setPaint(Color.blue);  
                    for(int j = 0; j < paths.size(); j++) {  
                        Rectangle path = paths.get(j);  
                        g2.draw(path);  
                    }  
            }  
          
            public static void main(String[] args) {  
                    JFrame f = new JFrame();  
                    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
                    f.add(new Resizing());  
                    f.setSize(400,400);  
                    f.setLocation(100,100);  
                    f.setVisible(true);  
          
            }  
          
            class Resizer extends MouseAdapter {  
            Resizing component;  
            boolean dragging = false;  
          
            public Resizer(Resizing rz) {  
                component = rz;  
                component.addMouseListener(this);  
                component.addMouseMotionListener(this);  
            }  
          
            public void mouseMoved(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);  
            }  
        }}

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    936
    Rep Power
    2

    Default Re: Change cursor on mouseover a rectangle

    I don't see you using the Resizer class anywhere. I would expect a call like new Resizer(this) in your Resizing class.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Change cursor on mouseover a rectangle

    IIRC, the correct way to do this is by overriding Component (Java Platform SE 7 ) on the component you want to change the cursor.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Change Cursor When Not Over A JFrame
    By anthropamorphic in forum New To Java
    Replies: 0
    Last Post: 08-18-2011, 07:10 PM
  2. change the mouse cursor - crosshair cursor
    By cassysumandak in forum New To Java
    Replies: 1
    Last Post: 09-28-2009, 01:57 AM
  3. How to change color of area contained by rectangle
    By thayalan in forum AWT / Swing
    Replies: 2
    Last Post: 06-04-2009, 04:48 AM
  4. Need to change cursor to hourglass at server side
    By bgupta in forum Java Servlet
    Replies: 0
    Last Post: 02-16-2009, 07:35 AM
  5. GUI cursor change problem
    By ludragon in forum Advanced Java
    Replies: 2
    Last Post: 01-09-2008, 10:03 PM

Tags for this Thread

Posting Permissions

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