Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Translucent JFrame border JDK 7

I was asking question about Translucent JFrame border (see here) and I got very good answers, but unfortunatelly, given answers work perfectly only on JDK 6, but not 7. Any ideas how to make it work with JDK 7?

In JDK 6 it looks like this:

enter image description here

And JDK 7:

enter image description here

And my code looks like this:

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);

}
}

Thanks a lot!

like image 607
Jay Gridley Avatar asked May 04 '12 14:05

Jay Gridley


2 Answers

I've just solved my problem. The problem was, that JDK 7 implements AWTUtilities.setWindowOpaque() method from JDK6 in setBackground() method and I was (NetBeans did :-)) setting default background for JFrame in different place, so setting background to new Color(0, 0, 0, 0); makes JFrame transparent and all goes well now.

like image 166
Jay Gridley Avatar answered Nov 02 '22 22:11

Jay Gridley


For whoever stumbles upon this thread and wants his own transparent window, I devised this example. With how little information is available on the web, I almost had to break a leg to come up with something just works, and doesn't use image files or anything. (Combined from different examples on this site)

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;
    }
}
like image 2
Mark Jeronimus Avatar answered Nov 02 '22 23:11

Mark Jeronimus