ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

具有透明度的Java游标实现

2019-10-10 23:01:37  阅读:663  来源: 互联网

标签: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

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有