Java background customization from JAR and its transparency

Be patient, I'm still a beginner, without rude comments.

So the purpose of this question is that I will learn how to set the background of my already transparent JFrame for blurring. This is what I have now.

Current jframe

So you can see it transparent but not blurry. I thought that Java has some kind of process that can blur the image from the JAR and use it as a background without changing the original image. So I looked and looked, but found nothing, helped me.

The JFrame background, as seen above, is a label with no text, but rather a “background image” as a label icon.

JAR Organization:

META-INF(folder) > MANIFEST.MF

SystemInfo(folder) > classes and background image

This is the code that I still have, but it does not work.

        setOpacity(); // sets opacity of the background
        Color c = Color.WHITE; // default color
            setColor(c);

        //background = (new JLabel((Icon) new ImageIcon(a)));
        BufferedImage mshi = null;    
        try{mshi = ImageIO.read(new File("SystemInfo/black.png"));}catch(Exception e){e.printStackTrace();}

        BufferedImage databuf = new BufferedImage(mshi.getWidth(null),
                mshi.getHeight(null),
                BufferedImage.TYPE_INT_BGR);

        Graphics g = databuf.getGraphics();
        g.drawImage(mshi, 455, 255, null);

        float[] blurKernel = {
            1 / 9f, 1 / 9f, 1 / 9f,
            1 / 9f, 1 / 9f, 1 / 9f,
            1 / 9f, 1 / 9f, 1 / 9f
        };

        BufferedImageOp blur = new ConvolveOp(new Kernel(3, 3, blurKernel));
        mshi = blur.filter(mshi, new BufferedImage(mshi.getWidth(),
                mshi.getHeight(), mshi.getType()));
        g.dispose();
        //background = (new JLabel((Icon) new ImageIcon(mshi)));
        background.setIcon((Icon)new JLabel(new ImageIcon(mshi)));

Errors mentioned above:

javax.imageio.IIOException: Can't read input file!
    at javax.imageio.ImageIO.read(ImageIO.java:1301)
    at SystemInfo.GuiMain.cinitComponents(GuiMain.java:150)
    at SystemInfo.GuiMain.<init>(GuiMain.java:30)
    at SystemInfo.GuiMain$8.run(GuiMain.java:256)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at SystemInfo.GuiMain.cinitComponents(GuiMain.java:152)
    at SystemInfo.GuiMain.<init>(GuiMain.java:30)
    at SystemInfo.GuiMain$8.run(GuiMain.java:256)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

If necessary, setOpacity (); funciton:

        float fOpacity = (float)opacity/10;
        String sOpacity = Float.toString(fOpacity)+"f";
        this.setOpacity(Float.parseFloat(sOpacity));

- JFrame :

What I would like to see

, .

.

+3
2

"SystemInfo/black.png" File, / , Jar.

( ImageIO), - ...

ImageIO.read(getClass().getResource("SystemInfo/black.png"))

.

-, , , , , , .

.

, Robot, .

"" .

+3

Java , - , Java , .

. , . , . , :

, , , , ( , Image I/O OK), .


(: Windows)

Windows JNA DwmEnableBlurBehindWindow, . Java 7 .

:

package dwmtest;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.win32.W32APIOptions;

public class DwmWindow extends JFrame
{
    public static void main(String... args)
    throws Exception
    {
        JFrame.setDefaultLookAndFeelDecorated(true);

        DwmWindow f = new DwmWindow();
        f.setSize(600, 500);
        f.setBackground(new Color(0,0,0,0));
        f.setTitle("Hello");
        TranslucentPanel panel = new TranslucentPanel();
        panel.setLayout(new BorderLayout());
        JLabel label = new JLabel("My background is blurry!");
        label.setFont(new Font("Dialog", Font.BOLD, 48));
        panel.add(label, BorderLayout.CENTER);
        f.add(panel);
        f.setDefaultCloseOperation(EXIT_ON_CLOSE);
        f.setVisible(true);

        HWND hwnd = new HWND(Native.getWindowPointer(f));
        Dwmapi.DWM_BLURBEHIND pBlurBehind = new Dwmapi.DWM_BLURBEHIND();
        pBlurBehind.dwFlags = Dwmapi.DWM_BB_ENABLE;
        pBlurBehind.fEnable = true;
        pBlurBehind.fTransitionOnMaximized = false;
        Dwmapi.INSTANCE.DwmEnableBlurBehindWindow(hwnd, pBlurBehind);
    }

    private static class TranslucentPanel extends JPanel
    {
        @Override
        protected void paintComponent(Graphics g) 
        {
            if (g instanceof Graphics2D) {
                final int R = 240;
                final int G = 240;
                final int B = 240;

                Paint p = new GradientPaint(0.0f, 0.0f, new Color(R, G, B, 0), 0.0f, getHeight(), new Color(R, G, B, 255), true);
                Graphics2D g2d = (Graphics2D)g;
                g2d.setPaint(p);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        }
    }

    public static interface Dwmapi extends StdCallLibrary
    {
        Dwmapi INSTANCE = (Dwmapi)Native.loadLibrary("Dwmapi", Dwmapi.class, W32APIOptions.UNICODE_OPTIONS);

        int DWM_BB_ENABLE = 0x00000001;

        boolean DwmEnableBlurBehindWindow(HWND hWnd, DWM_BLURBEHIND pBlurBehind);

        public static class DWM_BLURBEHIND extends Structure 
        {
            public int dwFlags;
            public boolean fEnable;
            public IntByReference hRgnBlur;
            public boolean fTransitionOnMaximized;

            @Override
            protected List getFieldOrder() 
            {
                return Arrays.asList("dwFlags", "fEnable", "hRgnBlur", "fTransitionOnMaximized");
            }
        }
    }
}

​​Aero- Windows, :

Blurry Java window

Aero, , .

- , , . , , , . , -.

+3

All Articles