views:

62

answers:

2

Hi

I create custom cursor with bottom code:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("C:/Users/Administrator/Desktop/gaea/core/ui/gaeawindow/src/si/xlab/gaea/core/ui/gaeawindow/HandCursor.gif");

// Somewhere in mouse pressed action

public void mousePressed(MouseEvent e)
    {
        Cursor cursor = toolkit.createCustomCursor(imageClose, new Point(12,12), "Hand");
        e.getComponent().setCursor(cursor);
    }

Cursor is shown on Mac like it should be, but in emulated Windows 7 it isn't. It's shown increased and it's ugly.

What fix/trick should i apply to my code to fix this? Is this common problem?

A: 

Probably emulated Windows 7 cant find the image file. You should move image file into classpath, next to your java files, so that you can load this file with getClass().getResource().

It should work on both emulated Windows 7 and Mac.

    public class CursorTest extends JFrame {
    public CursorTest() {

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        URL url = getClass().getResource("/si/xlab/gaea/core/ui/gaeawindow/HandCursor.gif");

        Image image = null;
        try {
            image = ImageIO.read(url.openStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        Cursor cursor = toolkit.createCustomCursor(image, new Point(12, 12),
                "Hand");
        setCursor(cursor);

        setSize(new Dimension(200, 200));
        setVisible(true);
    }
}
feridcelik
+1  A: 

The problem is that Windows wants 32x32 cursors and will scale your image if it isn't. The Mac is more flexible.

The easiest solution is to pad your existing 16x16 cursors out to 32x32 with transparent pixels; this will then work on both platforms.

You can use

Toolkit.getDefaultToolkit().getBestCursorSize(w,h)

to see if a given size is supported.

For more info, see: http://forums.sun.com/thread.jspa?threadID=5424409 which also has a link to the MS site.

toddkaufmann