JFrame与模糊透明背景 [英] JFrame with blurred transparent background

查看:118
本文介绍了JFrame与模糊透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在JFrame的背景上做一个模糊,它是透明的,以显示它下面发生的事情,但我不知道如何模糊背景并避免闪烁。我想要实现的是透明的背景,模糊不清,但仍然显示其下方窗户的实时视图,而不是模糊的静态图片不会改变。
请记住,窗口可能需要全屏大小。

I would like to do a blur on the background of the JFrame which is transparent to show what is happening underneath it, but I have no idea how can I blur the background and avoid flickering. What I want to achieve is to have a transparent background which is slighlty blurred, but still shows "live view" of the windows underneath it, not to have a blurred static picture which doesn't change. Keep in mind that the window may take the size of full screen.

我希望我能正确描述它,因为我还是Java Graphics的初学者。

I hope I described it correctly, as I am still a beginner in Java Graphics.

代码中的透明框架:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;

public class BlurredBackgroundWindow {

    public static void main(String[] args) {
        new BlurredBackgroundWindow().drawGUI();
    }

    public void drawGUI() {
        myJFrame frm = new myJFrame();
        frm.setTitle("BlurredBackgroundWindow");
        frm.setSize(480, 360);
        frm.setUndecorated(true);
        frm.setBackground(new Color(0,0,0,1));
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frm.setVisible(true);
    }

    class myJFrame extends JFrame {

        public void paint(Graphics g) {
            super.paint(g);

            // Doing the rest of painting here
        }

    }

}

注意 - 框架是完全透明的,并且看到一些效果会将颜色更改为例如。

Note - The frame is completely transparent and to see some effect change the color to eg.

frm.setBackground(new Color(0,100,0,100));

感谢您的帮助

编辑1 :
以下是我想要避免的闪烁效果,但我真的不知道如何...这只适用于全屏窗口。

EDIT 1: Here is the effect in action WITH the flickering which I want to avoid, but I don't really know how... This one works for a fullscreen window only.

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;

public class BlurredBackgroundWindow {

    public static int FPS = 2;
    private BufferedImage temp = null;
    private BufferedImage out = null;
    private BufferedImage image = null;

    myJFrame frm = new myJFrame();

    public static void main(String[] args) {
        new BlurredBackgroundWindow().drawGUI();
    }

    public void drawGUI() {
        frm.setTitle("BlurredBackgroundWindow");
        frm.setExtendedState(JFrame.MAXIMIZED_BOTH);
        frm.setUndecorated(true);
        frm.setBackground(new Color(0,0,0,1));
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frm.setVisible(true);

        Timer bcg = new Timer();
        bcg.schedule(new TimerTask() {

            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            Rectangle screenRectangle = new Rectangle(screenSize);
            Robot robot;

            public void run() {

                System.out.println("Repaint");
                frm.repaint();
                try {
                    robot = new Robot();
                    image = robot.createScreenCapture(screenRectangle);
                } catch (AWTException e) {}
                frm.repaint();
                // Save the blurred image for debugging
                /*try {
                    ImageIO.write(image, "png", new File("C:\\test.png"));
                } catch (IOException e) {
                    e.printStackTrace();
                }*/

            }

        }, 0, (int) (1000f/FPS));
    }

    class myJFrame extends JFrame {

        public void paint(Graphics g) {
            super.paint(g);

            if(image != null) {
                   float[] matrix = {
                            0.111f, 0.111f, 0.111f, 
                            0.111f, 0.111f, 0.111f, 
                            0.111f, 0.111f, 0.111f, 
                        };
                        BufferedImageOp op = new ConvolveOp( new Kernel(3, 3, matrix) );
                        temp = op.filter(image, out);
                        out = temp;
                g.drawImage(out,0,0,null);
                temp=null;out=null;image=null;
            }

        }

    }

}


推荐答案

您可以使用以下代码中给出的略有不同的方式: -

You can use slightly different way as given in the following code :-

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsDevice;
import java.awt.GraphicsDevice.WindowTranslucency;
import java.awt.GraphicsEnvironment;
import java.awt.Paint;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class transparent extends JFrame {

public transparent() {
    super("Transparent Window");
    setBackground(new Color(0,0,0,0));

    //setting it causes the frame to be transparent .Hence both panel and frame are transparent.
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,200);
    getContentPane().setLayout(new FlowLayout());


    JPanel jp=new JPanel(){
        public void paintComponent(Graphics g)
        {
            //super.paintComponent(g);
            Graphics2D g2=(Graphics2D)g;
            Paint gp=new GradientPaint(0, 0, new Color(100,20,210,105), 0, 200, new Color(80,20,40,105));
            g2.setPaint(gp);
            g2.fillRect(0, 0, getWidth(),getHeight());
        }
    };
    //setOpacity(0.6f);
    setContentPane(jp);
    JButton jbtn=new JButton("Enter");
    jp.add(jbtn);
    setVisible(true);
}
public static void main(String[] args) {
    // TODO Auto-generated method stub
    GraphicsEnvironment ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice gd=ge.getDefaultScreenDevice();
    if(!gd.isWindowTranslucencySupported(WindowTranslucency.PERPIXEL_TRANSLUCENT))
    {
        System.out.println("Per-pixel Transency not supported");
        System.exit(0);
    }
    JFrame.setDefaultLookAndFeelDecorated(true);

    //setting it to true causes the look to be handled by look and feel. otherwise os look and feel is used
    //In other words ,it is,modify default look and feel-yes or no?
    SwingUtilities.invokeLater(new Runnable(){public void run(){new transparent();}});
}
/*public void paint(Graphics g)
{
    super.paint(g);
    Graphics2D g2=(Graphics2D)g;
    GradientPaint gp=new GradientPaint(0, 0, new Color(20,20,210,30), 300, 200, new Color(10,20,40,255),true);
    g2.setPaint(gp);
    g2.fillRect(0, 0, getWidth(),getHeight());
}*/
}

这是获得的输出要好得多和玻璃: -

Here is the output obtained which is much better and glassy :-

这篇关于JFrame与模糊透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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