如何在OSX删除Java AWT框架的阴影? [英] How to remove drop shadow of Java AWT Frame on OSX?

查看:409
本文介绍了如何在OSX删除Java AWT框架的阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

时可以禁用在OS X上一个Java AWT应用程序的阴影?
我想创建一个透明的窗口,它工作得很好,但我不能摆脱阴影的。

Is it possible to disable the drop shadow of a Java AWT application on OS X? I want to create a transparent window, which works fine, but I cannot get rid of the drop shadow.

如果我使用的是的JFrame 这可以使用来完成:

If I was using a JFrame this could be done using:

 JRootPane root = frame.getRootPane();
 root.putClientProperty( "Window.shadow", Boolean.FALSE );

对于AWT框架的任何可能性相似?

Any similar possibility for an AWT Frame?

我在使用.NET Framework 处理和我的code有看起来像这样:

I' using the Framework Processing and my code there looks like this:

void setup(){
  frame.removeNotify();
  frame.setUndecorated(true);
}

加工本身并没有主框架的创建,这里是<一个href=\"http://$c$c.google.com/p/processing/source/browse/trunk/processing/core/src/processing/core/PApplet.java\"相对=nofollow>来源。

推荐答案

这是一种使用上的最大OS X 10.7.5的Java 7下运行的透明窗口的简单应用程序有没有问题(已经Java 6的下运行)。 ..

This is a simple application that uses a transparent window running on Max OS X 10.7.5 under Java 7 (has run under Java 6) which has no problems...

分享一些code,所以我们可以复制的问题。

Share some code so we can replicate the issue

从反馈更新

我已经在Mac OS 10.7.5及放大器测试这一点; 10.8.2,使用JDK 1.7.0_07及放大器; 1.6.0_37

I have tested this on Mac OS 10.7.5 & 10.8.2, using JDK 1.7.0_07 & 1.6.0_37

如果没有与 Window.shadow 属性...

Without and with Window.shadow property...

没有进入大量的测试,并没有进一步的信息,我建议你要早,你可以拨打这个电话。如果不工作,使其成为最后一个电话,你使窗口可见之前。

Without going into a lot of tests and without further information, I would suggest you want to make this call as early as you can. If that doesn't work, make it the last call before you make the window visible.

这可能涉及到如何的Java / AWT连接到它的本地同行,presumably,一旦连接后,您将不再能够影响这些类型的属性...

This may be related to how Java/AWT connects to it's native peer, presumably, once the connection is made, you will no longer be able to effect these kinds of properties...

public class TransparentFrame {

    public static void main(String[] args) {
        new TransparentFrame();
    }

    public TransparentFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception ex) {
                }
                // Use this to test the transparentancy API
                //doTransparentFrame();
                doDropShadow();
            }

        });
    }

    protected void doDropShadow() {
        String version = System.getProperty("java.version");
        System.out.println(version);
        JFrame frame = new JFrame("Testing");
        JRootPane root = frame.getRootPane();
        root.putClientProperty("Window.shadow", root);
        frame.setUndecorated(true);
        frame.setContentPane(new ContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new ImagePane());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    protected void doTransparentFrame() {
        JFrame frame = new JFrame("Testing");
        frame.setUndecorated(true);
        frame.setContentPane(new ContentPane());

        String version = System.getProperty("java.version");
        System.out.println(version);
        if (version.startsWith("1.7")) {
            frame.setBackground(new Color(0, 0, 0, 0));
        } else if (version.startsWith("1.6")) {
            if (supportsPerAlphaPixel()) {
                setOpaque(frame, false);
            } else {
                System.out.println("Per Pixel Alphering is not support with Java " + version);
                System.exit(1);
            }
        } else {
            System.out.println("Per Pixel Alphering is not support with Java " + version);
            System.exit(1);
        }
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());
        frame.add(new ImagePane());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public static boolean supportsPerAlphaPixel() {

        boolean support = false;

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            support = true;

        } catch (Exception exp) {
        }

        return support;

    }

    public static void setOpaque(Window window, boolean opaque) {

        try {

            Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
            if (awtUtilsClass != null) {

                Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
                method.invoke(null, window, opaque);

            }

        } catch (Exception exp) {
        }

    }

    public class ContentPane extends JPanel {

        public ContentPane() {
            setOpaque(false);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
        }

    }

    public class ImagePane extends JPanel {

        private BufferedImage background;
        private BufferedImage offImage;
        private Ellipse2D offButton;
        private boolean mouseIn;

        public ImagePane() {
            setOpaque(false);
            try {
                background = ImageIO.read(new File("tamagotchi400.png"));
                offImage = ImageIO.read(new File("powerSmall.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            offButton = new Ellipse2D.Float(212, 330, 25, 25);
            MouseAdapter handler = new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1) {
                        if (offButton.contains(e.getPoint())) {
                            Window window = SwingUtilities.getWindowAncestor(ImagePane.this);
                            if (window != null) {
                                window.dispose();
                            }
                        }
                    }
                }

                @Override
                public void mouseMoved(MouseEvent e) {
                    Cursor cursor = Cursor.getDefaultCursor();
                    if (offButton.contains(e.getPoint())) {
                        cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
                    }
                    setCursor(cursor);
                }

                @Override
                public void mouseEntered(MouseEvent e) {
                    mouseIn = true;
                    repaint();
                }

                @Override
                public void mouseExited(MouseEvent e) {
                    mouseIn = false;
                    repaint();
                }

            };
            addMouseListener(handler);
            addMouseMotionListener(handler);
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(400, 400) : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - background.getWidth()) / 2;
                int y = (getHeight() - background.getHeight()) / 2;
                g2d.drawImage(background, x, y, this);
                if (mouseIn && offImage != null) {
                    g2d.drawImage(offImage, (int) offButton.getX(), (int) offButton.getY(), this);
                }
                g2d.dispose();
            }
        }

    }

}

在code还包括透明度测试code测试的透明度API现在的Java 1.7和Java 1.6_10 +可用。我已经成功地使用这种code中的一些项目,其不太笨重则AWT机器人黑客,并提供现场回地面,但是这就是你需要做出选择。

The code also includes transparency test code to test the transparency API now available in Java 1.7 and Java 1.6_10+. I've used this code successfully in a number of projects, its less cumbersome then the AWT Robot "hack" and provides a live back ground, but that's a choice you need to make.

更新

示范使用的java.awt.Frame

public class TestTransparentFrame {

    public static void main(String[] args) {
        new TestTransparentFrame();
    }

    public TestTransparentFrame() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception exp) {
                }

                Frame frame = new Frame("Test");
                frame.setUndecorated(true);
                setOpaque(frame, false);
                frame.addWindowListener(new WindowAdapter() {
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.exit(0);
                    }

                });
                frame.setLayout(new BorderLayout());
                frame.add(new ContentPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class ContentPane extends JPanel {

        private BufferedImage background;

        public ContentPane() {
            try {
                background = ImageIO.read(new File("duke.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            setOpaque(false);
            JButton close = new JButton("Close");
            close.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    SwingUtilities.getWindowAncestor(ContentPane.this).dispose();
                }
            });
            setBorder(new LineBorder(Color.RED));
            setLayout(new GridBagLayout());
            add(close);
        }

        @Override
        public Dimension getPreferredSize() {
            return background == null ? new Dimension(200, 200) : new Dimension(background.getWidth(), background.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (background != null) {
                g.drawImage(background, 0, 0, this);
            }
        }

    }

    public static boolean supportsPerAlphaPixel() {

        boolean support = false;
        String version = System.getProperty("java.version");
        if (version.startsWith("1.6")) {
            try {
                Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
                support = true;
            } catch (Exception exp) {
            }
        } else if (version.startsWith("1.7")) {
            try {
                Class<?> winTransClass = Class.forName("java.awt.GraphicsDevice$WindowTranslucency");
                Field field = winTransClass.getField("TRANSLUCENT");

                GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                GraphicsDevice gd = ge.getDefaultScreenDevice();

                gd.isWindowTranslucencySupported(GraphicsDevice.WindowTranslucency.TRANSLUCENT);

                Method isWindowTranslucencySupported = GraphicsDevice.class.getMethod("isWindowTranslucencySupported", winTransClass);
                System.out.println(isWindowTranslucencySupported);

                Object value = isWindowTranslucencySupported.invoke(gd, field.get(null));
                if (value instanceof Boolean) {
                    support = ((Boolean) value).booleanValue();
                }

            } catch (Exception exp) {
            }
        }
        return support;
    }

    public static void setOpaque(Window window, boolean opaque) {

        String version = System.getProperty("java.version");
        if (version.startsWith("1.6")) {
            try {
                Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
                if (awtUtilsClass != null) {
                    Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
                    method.invoke(null, window, opaque);
                }
            } catch (Exception exp) {
            }
        } else if (version.startsWith("1.7")) {
            Color color = UIManager.getColor("Panel.background");
            if (opaque) {
                color = new Color(color.getRed(), color.getGreen(), color.getBlue());
            } else {
                color = new Color(color.getRed(), color.getGreen(), color.getBlue(), 0);
            }
            window.setBackground(color);
        }

    }

}

测试在Windows 7上的Java 1.6安培; 1.7的Mac OS 10.7.5和放大器; 10.8.2,使用JDK 1.7.0_07及放大器; 1.6.0_37

Tested on Windows 7 Java 1.6 & 1.7, Mac OS 10.7.5 & 10.8.2, using JDK 1.7.0_07 & 1.6.0_37

看样子,你不明白在Java中的窗口层次

It appears that you don't understand the window hierarchy in Java

在Java中的所有窗口派生自 java.awt.Window中的

All "windows" in Java derive from java.awt.Window.

的JFrame 扩展框架延伸窗口

这篇关于如何在OSX删除Java AWT框架的阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆