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

时间:2010-04-12 06:17:17

标签: java swing

我使用下面的代码为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 个答案:

答案 0 :(得分:5)

一个简单的解决方案是使用“标准”尺寸和透明背景的图像。

答案 1 :(得分:5)

Windows似乎只允许大小为32x32像素的游标,所以如果你想要另一个大小,你必须解决它。

要获得较小的尺寸,请使用带有32x32图像的createCustomCursor(),其中不需要的像素是透明的。如果您使用BufferedImage.TYPE_INT_ARGB,则可以使像素透明。

要制作更大的光标,我相信这会有效:

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

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

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

答案 2 :(得分:2)

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

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

(对于Windows,这是32x32)

然后将所需尺寸的光标图像blit到最佳尺寸的透明缓冲图像上,将不再调整其大小。

答案 3 :(得分:2)

Windows有一个defult游标,总是32x32像素。 您可以将图像设置为具有其他尺寸的光标,但是窗口会将此图像调整为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" );
    }

抱歉,我无法上传此事实的图片。我没有足够的声誉。 ; /