如何在swing中设置自定义光标大小?

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

我正在使用以下代码为JPanel设置自定义光标,但运行代码后发现光标所使用的图像被放大了。是否有一种方法可以设置用户定义的光标大小?

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);
4个回答

7

Windows似乎只允许使用32x32像素大小的光标,因此如果您需要其他大小的光标,则需要解决这个问题。

要获得较小的大小,请使用一个32x32图像调用createCustomCursor(),其中不需要的像素是透明的。如果使用BufferedImage.TYPE_INT_ARGB,则可以使像素透明。

要创建一个更大的光标,我相信以下方法可行:

  • 创建一个完全透明的自定义光标。

  • 使用mouseMotionListener获取光标的位置。

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


5
使用一个标准大小并带有透明背景的图片似乎是一种简单的解决方案。

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);我尝试了上面的代码,它将图像放大到一定的预定义大小。如果有其他方法,请回复我一个简单的代码片段。 - swift
如果您想使用自定义大小的光标,那么它可能与平台有关。因此,我建议您将图像文件(images/eraserpointer.jpg)设置为32x32(在Windows中),然后只绘制其10x10的角落并使其余部分透明。当然,您需要使用支持透明度的文件格式(如png或gif)。在其他平台上,我猜您只需为不同平台准备不同大小的图片即可。 - fish

4

您可以在运行时确定光标的大小,它由“最佳大小”控制。

维度aBestSize = Toolkit.getDefaultToolkit().getBestCursorSize(0, 0);

(对于Windows,这是32x32)

然后将所需大小的光标图像覆盖到最佳大小的透明缓冲图像上,它将不再被重新调整大小。


3

Windows有一个默认的光标,它的大小始终为32x32像素。

你可以设置一张其他尺寸的图片作为光标,但是Windows会将这张图片调整到32x32像素大小,这可能会引发其他副作用,特别是当你的图片不是正方形时。

你可以通过像这个例子中使用透明图片来解决这个问题。

    /**
     * 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" );
    }

抱歉,我无法上传这些图片。我的声望不够高。;/


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接