半透明 JFrame 边框 JDK 7

我问了关于半透明JFrame边界的问题(见这里),我得到了非常好的答案,但不幸的是,给出的答案只在JDK 6上完美运行,而不是7。任何想法如何使它与JDK 7一起工作?

在 JDK 6 中,它看起来像这样:

enter image description here

和 JDK 7:

enter image description here

我的代码看起来像这样:

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.border.AbstractBorder;


public class ShadowBorder extends AbstractBorder {

private static final int RADIUS = 30;
private static BufferedImage shadowTop;
private static BufferedImage shadowRight;
private static BufferedImage shadowBottom;
private static BufferedImage shadowLeft;
private static BufferedImage shadowTopLeft;
private static BufferedImage shadowTopRight;
private static BufferedImage shadowBottomLeft;
private static BufferedImage shadowBottomRight;
private static boolean shadowsLoaded = false;

public ShadowBorder() {
    if (!shadowsLoaded) {
        try {
            shadowTop = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-top.png"));
            shadowRight = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-right.png"));
            shadowBottom = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-bottom.png"));
            shadowLeft = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-left.png"));

            shadowTopLeft = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-top-left.png"));
            shadowTopRight = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-top-right.png"));
            shadowBottomLeft = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-bottom-left.png"));
            shadowBottomRight = ImageIO.read(getClass().getResource("/cz/vutbr/fit/assets/shadow-bottom-right.png"));

            shadowsLoaded = true;
        } catch (IOException ex) {
            Logger.getLogger(ShadowBorder.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

@Override
public boolean isBorderOpaque() {
    return false;
}

@Override
public Insets getBorderInsets(Component c) {
    return new Insets(RADIUS, RADIUS, RADIUS, RADIUS);
}

@Override
public Insets getBorderInsets(Component c, Insets insets) {
    insets.top = RADIUS;
    insets.left = RADIUS;
    insets.bottom = RADIUS;
    insets.right = RADIUS;

    return insets;
}

@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_ATOP, 1f));

    int recWidth = width - (2 * RADIUS);
    int recHeight = height - (2 * RADIUS);
    int recX = width - RADIUS;
    int recY = height - RADIUS;

    //edges
    g2d.drawImage(shadowTop.getScaledInstance(recWidth, RADIUS, Image.SCALE_REPLICATE), RADIUS, 0, null);
    g2d.drawImage(shadowRight.getScaledInstance(RADIUS, recHeight, Image.SCALE_REPLICATE), recX, RADIUS, null);
    g2d.drawImage(shadowBottom.getScaledInstance(recWidth, RADIUS, Image.SCALE_REPLICATE), RADIUS, recY, null);
    g2d.drawImage(shadowLeft.getScaledInstance(RADIUS, recHeight, Image.SCALE_REPLICATE), 0, RADIUS, null);

    //corners
    g2d.drawImage(shadowTopLeft, 0, 0, null);
    g2d.drawImage(shadowTopRight, recX, 0, null);
    g2d.drawImage(shadowBottomLeft, 0, recY, null);
    g2d.drawImage(shadowBottomRight, recX, recY, null);

}
}

多谢!


答案 1

我刚刚解决了我的问题。问题是,JDK 7 在方法中实现了 JDK6 中的方法,而我(NetBeans 确实做到了:-))在不同的地方设置了 JFrame 的默认背景,所以设置背景使 JFrame 透明,现在一切都很顺利。AWTUtilities.setWindowOpaque()setBackground()new Color(0, 0, 0, 0);


答案 2

对于那些偶然发现这个线程并想要自己的透明窗口的人,我设计了这个例子。由于网络上可用的信息很少,我几乎不得不摔断一条腿才能想出一些有效的东西,并且不使用图像文件或任何东西。(结合本网站的不同示例)

public class GradientTranslucentWindowDemo
{
    public static void main(String[] args)
    {
        // Create the GUI on the event-dispatching thread
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                final JFrame f = new JFrame("Per-pixel translucent window");
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                f.setUndecorated(true);
                f.setBackground(new Color(0, 0, 0, 0));

                final BufferedImage backrgoundImage = makeBackrgoundImage(400, 400);

                JPanel panel = new JPanel()
                {
                    @Override
                    public void paintComponent(Graphics g)
                    {
                        super.paintComponent(g);
                        if (g instanceof Graphics2D)
                        {

                            g.drawImage(backrgoundImage, 0, 0, null);
                        }
                    }
                };
                panel.setOpaque(false);
                f.setContentPane(panel);

                f.setLayout(new GridBagLayout()); // Centers the button
                f.add(new JButton(new AbstractAction("Close")
                {
                    @Override
                    public void actionPerformed(ActionEvent e)
                    {
                        f.dispose();
                    }
                }));

                f.setBounds(100, 100, 400, 400);
                f.setVisible(true);
            }
        });
    }

    static BufferedImage makeBackrgoundImage(int w, int h)
    {
        BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

        // Draw something transparent
        Graphics2D g = img.createGraphics();
        g.setPaint(new RadialGradientPaint(new Point2D.Float(w / 2, h / 2), (w + h) / 4, new float[]{0, 1}, new Color[]{Color.RED, new Color(1f, 0, 0, 0)}));
        g.fillRect(0, 0, w, h);
        g.setPaint(Color.RED);
        g.drawRect(0, 0, w - 1, h - 1);
        g.dispose();

        return img;
    }
}

推荐