如何为秋千中的光标设置自定义大小? [英] How to set custom size for cursor in swing?

查看:42
本文介绍了如何为秋千中的光标设置自定义大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码为JPanel设置自定义光标,但是当我运行该代码时,它将放大为光标设置的图像.有没有一种方法可以设置用户定义的光标大小?

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);

推荐答案

Windows似乎只允许使用32x32像素大小的游标,因此,如果要使用其他大小的游标,则必须解决该问题.

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

要缩小尺寸,请对32x32图像使用 createCustomCursor(),其中不需要的像素是透明的.如果使用 BufferedImage.TYPE_INT_ARGB ,则可以使像素透明.

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.

使用 mouseMotionListener 获取光标的位置.

在真实(透明)光标的位置绘制光标图像.

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

这篇关于如何为秋千中的光标设置自定义大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆