标签:alpha-transparency java transparency swing cursor
我有35×40像素.我想在Swing应用程序中用作自定义光标的png图像.
图像有光晕,因此包含alpha透明度值.问题是,当我尝试使用使用工具包的常规方法来生成自定义光标时,我得到了应该设置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不支持半透明.因此,如果要在Mac OSX上运行该实现,我正在寻找使该运行在Windows上的任何解决方案,我只能根据应用运行的操作系统,在代码中指定要使用的实现.
解决方法:
您遇到的问题与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();
}
}
}
}
我想到了here
标签:alpha-transparency,java,transparency,swing,cursor 来源: https://codeday.me/bug/20191010/1888522.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。