为什么当我将图像设置到光标时,它不起作用?

1 java swing image mouse-pointer

我是Java的新手,我在尝试将图像设置到光标时遇到问题.我正在使用a BufferedImage,Graphics.drawImage但它只是绘制图像的颜色而不是完整的png图像.

这是我的代码:

/*The images List*/
iconsBet.add(ImageIO.read(getClass().getResource("/resources/ChipType"+ String.valueOf(maxChipBet+1) +".png")));
/*The images List*/

BufferedImage output = new BufferedImage(iconsBet.get(0).getWidth(), iconsBet.get(0).getHeight(), BufferedImage.TYPE_INT_ARGB );
Graphics graphicsCursorIcon = output.getGraphics();

int count = 0;
for(BufferedImage icon : iconsBet)
{                
   graphicsCursorIcon.drawImage(icon, 0, count*10, null);
   count++;
}

graphicsCursorIcon.dispose();
Toolkit toolkit = Toolkit.getDefaultToolkit();
Cursor c = toolkit.createCustomCursor(output , new Point(mainPanel.getX(), mainPanel.getY()), "img");
mainPanel.setCursor(c);
Run Code Online (Sandbox Code Playgroud)

图像:这是我正在使用的图像组中的一个图像

该程序只绘制一个红色圆圈而不是png图像.

我已经尝试过使用所有BufferedImage类型,但是没有用.你能帮帮我吗?我需要做些什么来使它工作?

Tho*_*sch 5

我怀疑你误解了Toolkit.createCustomCursor的第二个参数(Image cursor,Point hotSpot,String name):

hotSpot - 大光标热点的X和Y; hotSpot值必须小于getBestCursorSize返回的Dimension

hotspot是指相对于光标图像的左上角,而不是面板的左上角.所以,而不是

new Point(mainPanel.getX(), mainPanel.getY())
Run Code Online (Sandbox Code Playgroud)

试一试

new Point(0, 0)
Run Code Online (Sandbox Code Playgroud)