2010-04-12 69 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"); 
    } 

對不起,但我無法上傳這個事實的圖像。我沒有足夠的聲譽。 ;/

相關問題