我有一个35x40像素的.png图片,我想在Swing应用程序中使用它作为自定义光标。该图像具有发光效果,因此包含alpha透明度值。问题是,当我尝试使用传统方法使用
这是我用作光标的图片:https://dl.dropbox.com/u/1186703/cursor.png 以下是我的代码:
Toolkit
生成自定义光标时,我会得到黑色像素,而不是alpha透明度值。这是我用作光标的图片:https://dl.dropbox.com/u/1186703/cursor.png 以下是我的代码:
public static void main(String[] args) throws IOException {
new Sandbox().gui();
}
private Cursor cursor;
private Toolkit kit;
private Image cursorImage;
public void gui() {
kit = Toolkit.getDefaultToolkit();
cursorImage = kit.createImage(getClass().getResource(
"/aurora/V1/resources/cursor.png"));
cursor = Toolkit.getDefaultToolkit().createCustomCursor(
cursorImage, new Point(0, 0), "CustomCursor");
setSize(800, 800);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setVisible(true);
setCursor(cursor);
}
这是当前的结果:
编辑 看起来这种方法在跨平台时效果不佳,例如 Windows LAF 不支持半透明。因此,我正在寻找任何解决方案,以在 Windows 上使其正常工作,假设该实现在 Mac OSX 上可行,我可以根据应用程序运行的操作系统指定要使用哪个实现。
HCURSOR
与窗口关联起来。 - Alexey Ivanov