开发者

JWindow with Black Opacity

I would like to create a JWindow that's not only has an opacity, but I want to change the default color of the opacity within Swing.

So for example, if I write:

AWTUtilities.setWindowOpacity(this, 0.5f);

This will make do exactly what I want wi开发者_开发百科th one exception, the color is white. How can I make the color become black?

I've tried everything from setBackground(Color.Black), etc. on "this"...


        window.getContentPane().setBackground(Color.BLACK);


Transparent JWindow.

public class TransparentWindow{

    JWindow window;

    public TransparentWindow(){
        initializeTransparentWindow();
    }

    private void initializeTransparentWindow() {
        // searching graphical configuration that provide transparent window
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = env.getScreenDevices();
        GraphicsConfiguration translucencyCapableGC = null;
        for (int i = 0; i < devices.length && translucencyCapableGC == null; i++) {
            GraphicsConfiguration[] configs = devices[i].getConfigurations();
            for (int j = 0; j < configs.length && translucencyCapableGC == null; j++) {
                if (AWTUtilities.isTranslucencyCapable(configs[j])) {
                    translucencyCapableGC = configs[j];
                }
            }
        }
        if (translucencyCapableGC != null) {
            window = new JWindow(translucencyCapableGC) {
                @Override
                public void paint(Graphics g) {
                    if (getWidth() > 4 && getHeight() > 4) {
                        g.clearRect(0, 0, getWidth(), getHeight());
                        g.setColor(new Color(0x0, 0x0, 0x0, 0xaa));
                        g.fillRect(0, 0, 1, getHeight());
                        g.fillRect(0, 0, getWidth(), 1);
                        g.fillRect(0, getHeight() - 1, getWidth(), 1);
                        g.fillRect(getWidth() - 1, 0, 1, getHeight());
                        g.setColor(new Color(0x0, 0x0, 0x0, 0x10));
                        g.fillRect(1, 1, getWidth() - 1, getHeight() - 1);
                    }
                };
            };
            AWTUtilities.setWindowOpaque(window, false);
        }
        else {
            window = new JWindow();
            AWTUtilities.setWindowOpacity(window, 0.5f);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜