Hi,

I have a png image wihtout background and want this png to be my JFrame where the transparent section must be transparent; I have tried different ways I used the setOpaque(), setOpacity(), ImageIO but no clue.

I want only my transparent png as JFrame. Any idea and solution will be appreciated.

Dani AI

Generated

Nice one, . For and anyone hitting this later: to show only the PNG (holes fully see-through) you want per-pixel translucency, not uniform window opacity. Avoid calling setOpacity(0.0f) because that makes the entire window invisible; use an undecorated window whose background has an alpha channel so Swing paints your PNG’s alpha as-is. This requires that the platform supports per-pixel translucency and that the window is not in full-screen mode. (docs.oracle.com)

Before wiring up your UI, check support once at startup:

import java.awt.*;

static boolean supportsPerPixelTranslucency() {
    GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
        .getDefaultScreenDevice();
    return gd.isWindowTranslucencySupported(
        GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSLUCENT);
}

If that returns false, fall back to a normal decorated frame or a solid background. If true, proceed with an undecorated window, lay out a single component that paints your PNG, pack() to the image’s size, and center it. (docs.oracle.com)

One subtlety: transparent pixels may still receive mouse events on some platforms. If you want clicks to “pass through” the holes, either shape the window to exclude transparent regions or do hit-testing against the PNG’s alpha before handling events. For example:

static boolean isOpaqueAt(BufferedImage img, int x, int y) {
    if (x < 0 || y < 0 || x >= img.getWidth() || y >= img.getHeight()) return false;
    int a = (img.getRGB(x, y) >>> 24) & 0xFF; // 0 == fully transparent
    return a > 0;
}

You can then ignore drags/clicks when isOpaqueAt(...) is false, or build a Shape and call setShape(...) so the OS never dispatches events to those pixels. (docs.oracle.com)

Recommended Answers

All 2 Replies

It works just fine in Java 7, but there are a number of calls that are needed. Something like this code from an old project's splash screen (maybe some redundancy - it start life under the temporary transparency support in Java 6.21)...

frame.setUndecorated(true); // gets rid of borders title bar etc (call before JFrame is visible)
frame.setBackground(new Color(0, 0, 0, 0)); // frame is transparent    
frame.getRootPane().setOpaque(false); // frame content (rootpane) is transparent...
frame.setOpacity(0.0f); // or maybe this achieves the same thing

JLabel iconLabel = new JLabel(); // use JLabel ImageIcon to paint image
URL imgURL = getClass().getResource("/images/xxx.png"); // transparent png
iconLabel.setIcon(new ImageIcon(imgURL))
frame.add(iconLabel;

Thank you, it worked :-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.