tags:

views:

99

answers:

2

I am using the below code to set a custom cursor for JPanel, but when i run the code its enlarging the image which i set for cursor. Is there a way to set a userdefined cursor size ?

Toolkit toolkit = Toolkit.getDefaultToolkit();
BufferedImage erasor=new BufferedImage(10,10, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=(Graphics2D) erasor.createGraphics();
g2d.setPaint(Color.red);
g2d.drawRect(e.getX(),e.getY() ,10, 10);
toolkit.getBestCursorSize(10, 10);
Cursor mcursor=toolkit.createCustomCursor(erasor, new Point(10,10), "Eraser");
setCursor(mcursor);
+1  A: 

An easy solution would be to use an image of "standard" size and transparent background.

fish
Toolkit toolkit = Toolkit.getDefaultToolkit();Image eraser = toolkit.getImage("images/eraserpointer.jpg");Graphics2D g2d=(Graphics2D) erasor.createGraphics();toolkit.getBestCursorSize(10, 10);Cursor mcursor=toolkit.createCustomCursor(eraser, new Point(10,10), "Eraser");setCursor(mcursor);Ya, i tried it with the above code, it is enlarging the image to some pre defined size.If there is some other approach plz reply me with a simple code snippet
swift
It's probably platform dependent if you can use a custom sized cursor. Therefore I suggest you make the image file (images/eraserpointer.jpg) to 32x32 (in Windows) and just paint the 10x10 corner of it and leave the rest transparent. Of course you need to use a file format that supports transparency (png or gif). On other platforms.. I guess you just have to have different sized pictures for different platforms.
fish
A: 

Windows seem to only allow cursors of size 32x32 pixels so if you want another size you have to work around it.

To get a smaller size use createCustomCursor with a 32x32 image where the unwanted pixels are transparent. If you use BufferedImage.TYPE_INT_ARGB you can make pixels transparent.

To make a larger cursor I beleive this will work:

Create a custom cursor that is completly transparent.

Use a mouseMotionListener to get the position of the cursor.

Draw your cursor image at the position of the real (transparent) cursor.

Anders