2012-09-24 150 views
0

我創建使用光標:改變光標尺寸

BufferedImage im=null; 
    try { 
     im = ImageIO.read(new File("images/cursor1.jpg")); 
    } catch (IOException ex) { 
     Logger.getLogger(SRGView.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    Cursor cursor = getToolkit().createCustomCursor(im, new Point(1,1), "s"); 
    this.setCursor(cursor); 

的cursor1.jpg是5X5(以像素爲單位)。但是,當它顯示在屏幕上時,它會更大。我想做大小爲1X1,5X5,10X10的遊標。我寧願動態創建圖像比讀取圖像文件。即

for (int x = 0; x < w; x++) { 
     for (int y = 0; y < h; y++) { 
     im.setRGB(x, y, new Color(255, 0, 0).getRGB()); 
    } 
    } 

上面的代碼會創建一個紅色的圖像「im」的寬度,w和高度h,我想用它作爲我的光標。

怎麼辦?

回答

0

它似乎像Windows只接受32X32大小的遊標。所以這是我解決了這個問題:

的主要思想是使32X32尺寸圖像透明中的每個像素,並應用顏色只有ü要在光標看到那些像素。在這種情況下,我想要一個長方形的紅色矩形光標,在32X32圖像的中心,我將w x h個像素設置爲紅色,並將該圖像設置爲光標。

public void setCursorSize(int w, int h) { 
    this.cw = w; 
    this.ch = h; 
    if (w > 32) { 
     w = 32; 
    } 
    if (h > 32) { 
     h = 32; 
    } 
    Color tc = new Color(0, 0, 0, 0); 
    BufferedImage im = new BufferedImage(32, 32, BufferedImage.TYPE_4BYTE_ABGR);//cursor size is 32X32 
    for (int x = 0; x < 32; x++) { 
     for (int y = 0; y < 32; y++) { 
      im.setRGB(x, y, tc.getRGB()); 
     } 
    } 
    for (int x = 16 - w/2; x <= 16 + w/2; x++) { 
     for (int y = 16 - h/2; y <= 16 + h/2; y++) { 
      im.setRGB(x, y, new Color(255, 0, 0).getRGB()); 
     } 
    } 
    this.setCursor(getToolkit().createCustomCursor(im, new Point(16, 16), "c1")); 
}