Mouse Cursor in Java

1.1k views Asked by At

Could you tell me, Why it doesn't work. When I run it, mouse hides. but I want to change mouse cursor's icon.

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;

import javax.swing.JList;

import acm.program.GraphicsProgram;

public class test1 extends GraphicsProgram {

    public Cursor stoneCursor;

    public void run() {
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Image image = toolkit.getImage("cursor.gif");
        Point hotspot = new Point(0, 0);
        stoneCursor = toolkit.createCustomCursor(image, hotspot, "Stone");
        getContentPane().setCursor(stoneCursor);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
    }
}
1

There are 1 answers

1
Rajendra arora On

Here is an example try this..

Standard Cursor Image:

setCursor (Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

User defined Image:

Toolkit toolkit = Toolkit.getDefaultToolkit();
  Image image = toolkit.getImage("icons/handwriting.gif");
  Cursor c = toolkit.createCustomCursor(image , new Point(mainPane.getX(),
     mainPane.getY()), "img");
  mainPane.setCursor (c);