简体   繁体   English

如何在摇摆中设置光标的自定义大小?

[英]How to set custom size for cursor in 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. 我使用下面的代码为JPanel设置自定义光标,但是当我运行代码时,它会放大我为光标设置的图像。 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 seem to only allow cursors of size 32x32 pixels so if you want another size you have to work around it. Windows似乎只允许大小为32x32像素的游标,所以如果你想要另一个大小,你必须解决它。

To get a smaller size use createCustomCursor() with a 32x32 image where the unwanted pixels are transparent. 要获得更小的尺寸,请使用带有32x32图像的createCustomCursor() ,其中不需要的像素是透明的。 If you use BufferedImage.TYPE_INT_ARGB you can make pixels transparent. 如果使用BufferedImage.TYPE_INT_ARGB ,则可以使像素透明。

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. 使用mouseMotionListener获取光标的位置。

  • Draw your cursor image at the position of the real (transparent) cursor. 在真实(透明)光标的位置绘制光标图像。

Windows has a defult cursor, which is always 32x32 pixels. Windows有一个defult游标,总是32x32像素。 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. 您可以将图像设置为具有其他尺寸的光标,但是窗口会将此图像的大小调整为32x32像素,这可能会触发其他副作用,尤其是当图像不是二次时。

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. 我没有足够的声誉。 ;/ ; /

You can determine the cursor size at runtime, it is controlled by the 'best-size'. 您可以在运行时确定游标大小,它由“最佳大小”控制。

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

(For windows this is 32x32) (对于Windows,这是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. 然后将所需大小的光标图像blit到最佳大小的透明缓冲图像上,将不再调整大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM