Sam*_*chi 5 java swing transparency cursor alpha-transparency
我有35x40像素.png图像我想在Swing应用程序中用作自定义光标.图像具有发光,因此包含Alpha透明度值.问题是当我尝试使用传统的方法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);
}
Run Code Online (Sandbox Code Playgroud)
这是当前的结果:
编辑它似乎这种方法不能很好地跨平台工作,例如Windows LAF不支持半透明.我正在寻找任何解决方案让这个在Windows上工作,假设这个实现在Mac OSX上工作,我可以在代码中指定基于运行应用程序的操作系统使用哪个实现.
您遇到的问题与Cursor
(在Windows下)未考虑图像透明度值的类有关
这绝不是一个"真正的"解决方案,而更多的是"捏造"结果......
public class TestMouseCursor {
public static void main(String[] args) {
new TestMouseCursor();
}
public TestMouseCursor() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MouseCursorPane());
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class MouseCursorPane extends JPanel {
private BufferedImage cursorImage;
private Toolkit kit;
public MouseCursorPane() {
try {
kit = Toolkit.getDefaultToolkit();
cursorImage = ImageIO.read(getClass().getResource("/cursor02.png"));
for (int i = 0; i < cursorImage.getHeight(); i++) {
int[] rgb = cursorImage.getRGB(0, i, cursorImage.getWidth(), 1, null, 0, cursorImage.getWidth() * 4);
for (int j = 0; j < rgb.length; j++) {
int alpha = (rgb[j] >> 24) & 255;
if (alpha < 128) {
alpha = 0;
} else {
alpha = 255;
}
rgb[j] &= 0x00ffffff;
rgb[j] = (alpha << 24) | rgb[j];
}
cursorImage.setRGB(0, i, cursorImage.getWidth(), 1, rgb, 0,
cursorImage.getWidth() * 4);
}
Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(
cursorImage, new Point(0, 0), "CustomCursor");
setCursor(cursor);
} catch (Exception exp) {
exp.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里得到了主意
归档时间: |
|
查看次数: |
2328 次 |
最近记录: |