Reputation: 363
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);
Upvotes: 13
Views: 11659
Reputation: 705
Windows has a defult cursor, which is always 32x32 pixels. You can set a image aa cursor with a other size, but windows resize this image to 32x32 pixel, which can trigger other side effects especially when your image is not quadratic.
You can do a workaround with a transparent image like in this example.
/**
* Create a transparent cursor with a given frame. Note: The name of the
* cursor is <code>Trans</code>.
* <br>
* <b>Note</b>: The maximal size for the cursor is 32x32 pixel under windows.
* Technically it is possible to create a cursor bigger than 32x32 pixel, but this method must run under windows
* and so the size is limited to 32 pixel.
*
* @param size the size of the frame (horizontal/vertical)
* <br>
* <b>Note</b>: maximal size is 32 pixel.
* @param frameThickness the thickness of the frame
* @param frameColor the color of the frame
* @return a cursor which is a frame with the given size and color.
*/
public static synchronized Cursor createTransparentCursor( int size, int frameThickness, Color frameColor ) {
final int cursourSize = size + (2 * frameThickness);
System.out.println("cursourSize: "+cursourSize);
final BufferedImage bufferedImage = new BufferedImage( 32 + 2, 32 + 2, BufferedImage.TYPE_INT_ARGB );
final Graphics graphic = bufferedImage.getGraphics();
final Color colTrans = new Color( 0, 0, 0, 0 );
for( int i = 0 ; i < cursourSize ; i++ ){
for( int j = 0 ; j < cursourSize ; j++ ){
if( i <= frameThickness || i > cursourSize - frameThickness -1 || j <= frameThickness
| j > cursourSize - frameThickness - 1 ){
graphic.setColor( frameColor );
}
else{
graphic.setColor( colTrans );
}
graphic.fillRect( i, j, 1, 1 );
}
}
System.out.println("Buffered size:" +bufferedImage.getHeight() +"/"+ bufferedImage.getWidth());
final Point hotSpot = new Point( cursourSize / 2, cursourSize / 2 );
return Toolkit.getDefaultToolkit().createCustomCursor( bufferedImage, hotSpot, "Trans" );
}
Sorry, but I can't upload a image of this facts. I don't have enough reputations. ;/
Upvotes: 3
Reputation: 71
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 believe this will work:
Create a custom cursor that is completely transparent.
Use a mouseMotionListener
to get the position of the cursor.
Draw your cursor image at the position of the real (transparent) cursor.
Upvotes: 7
Reputation: 76
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.
Upvotes: 4
Reputation: 1050
An easy solution would be to use an image of "standard" size and transparent background.
Upvotes: 5