Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set custom size for cursor in swing?

Tags:

java

swing

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);
like image 265
swift Avatar asked Apr 12 '10 06:04

swift


1 Answers

You can determine the cursor size at runtime, it is controlled by the 'best-size'.

Dimension aBestSize = Toolkit.getDefaultToolkit().getBestCursorSize(0, 0);

(For windows this is 32x32)

Then blit the cursor image of the size you want onto a transparent buffered image of the best size, it will no longer be resized.

like image 195
JiroDan Avatar answered Oct 25 '22 21:10

JiroDan