2010-04-12 68 views
9

我正在使用下面的代码来为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); 

回答

5

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

+0

Toolkit toolkit = Toolkit.getDefaultToolkit(); Image eraser = toolkit.getImage(“images/eraserpointer.jpg”);图形2D g2d =(Graphics2D)erasor.createGraphics(); toolkit.getBestCursorSize(10,10); 光标mcursor = toolkit.createCustomCursor(橡皮擦,新点(10,10),“橡皮擦”); setCursor(mcursor); 雅,我试着用上面的代码,它是放大图像到一些预定义的大小。 如果还有其他方法,请使用简单的代码片段回复我 – swift 2010-04-12 08:02:28

+0

如果您可以使用自定义大小的游标,则可能与平台有关。因此,我建议您将图像文件(images/eraserpointer.jpg)设置为32x32(在Windows中),并将其绘制为10x10的角落,并让其余部分保持透明。当然,您需要使用支持透明度的文件格式(png或gif)。在其他平台上..我想你只需要为不同的平台有不同大小的图片。 – fish 2010-04-12 09:26:34

5

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

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

为了做更大的光标,我相信这将工作:

  • 创建自定义游标是完全透明的。

  • 使用mouseMotionListener来获得游标的位置。

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

2

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

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

(适用于Windows,这是32×32)

随后的blit您想要到最佳尺寸的透明缓冲图像大小的光标图像,它将不再被调整。

2

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

对不起,但我无法上传这个事实的图像。我没有足够的声誉。 ;/

相关问题