如何使用Swing和JLayer在Java中制作模糊的JFrame / JDialog<>? [英] How to make a blurry JFrame/JDialog in Java using Swing and JLayer<>?

查看:228
本文介绍了如何使用Swing和JLayer在Java中制作模糊的JFrame / JDialog<>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试模糊我的JFrame。我们的想法是使用JLayer / LayerUI模糊JFrame中的所有组件/控件。这是我到目前为止所做的:

I am trying to blur my JFrames. The idea is to blur all components/controls within a JFrame using JLayer/LayerUI. Here's what I have done so far:

这是产生模糊效果的LayerUI类:

This is the LayerUI class that makes the blur effect:

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import javax.swing.JComponent;
import javax.swing.plaf.LayerUI;

public class BlurLayerUI extends LayerUI<Component> {

    private BufferedImage mOffscreenImage;
    private final BufferedImageOp mOperation;

    public BlurLayerUI() {
        int blurValue = 6;
        int blurCount = blurValue*blurValue;
        float ninth = 1.0f / blurCount;
        float[] blurKernel = new float[blurCount];
        for(int i=0; i<blurCount; i++) {
            blurKernel[i] = ninth;
        }
        mOperation = new ConvolveOp(new Kernel(blurValue, blurValue, blurKernel), ConvolveOp.EDGE_NO_OP, null);
    }

    @Override
    public void paint (Graphics g, JComponent c) {
        int w = c.getWidth();
        int h = c.getHeight();
        if(w == 0 || h == 0) {
            return;
        }
        // only create the offscreen image if the one we have is the wrong size.
        if(mOffscreenImage == null ||  mOffscreenImage.getWidth() != w ||  mOffscreenImage.getHeight() != h) {
            mOffscreenImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        }
        Graphics2D ig2 = mOffscreenImage.createGraphics();
        ig2.setClip(g.getClip());
        super.paint(ig2, c);
        ig2.dispose();
        Graphics2D g2 = (Graphics2D)g;
        g2.drawImage(mOffscreenImage, mOperation, 0, 0);
    }

}

......这是一个测试创建JFrame的类:

... and here's a test class that creates a JFrame:

import java.awt.Component;
import javax.swing.JLayer;

public class BlurryTest extends javax.swing.JFrame {

    public BlurryTest() {
        initComponents();
        // what do I have to do here to blurry this JFrame ... ??
        JLayer<Component> blurLayer = new JLayer<>(this.getLayeredPane(), new BlurLayerUI());
        //this.add(blurLayer);
        //this.setGlassPane(blurLayer);
        //this.getGlassPane().setVisible(true);
    }

    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("jButton1");

        jButton2.setText("jButton2");

        jCheckBox1.setText("jCheckBox1");

        jTextField1.setText("jTextField1");

        jTextField2.setText("jTextField2");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 234, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jTextField1)
                    .addComponent(jCheckBox1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jTextField2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jCheckBox1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 69, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        pack();
    }                     

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new BlurryTest().setVisible(true);
            }
        });
    }

    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;

}

问题出在班级的构造函数中。如何构造提供LayerUI对象的JLayer对象以模糊JFrame的整个内容?

任何帮助高度赞赏 - 谢谢!

Any help highly appreciated - thanks!

推荐答案

组件直接添加到框架 contentPane ,尝试使用...

Components are added directly to the frames contentPane, try using...

JLayer<Component> blurLayer = new JLayer<>(this.getContentPane(), new BlurLayerUI());
setContentPane(blurLayer);

而不是

这篇关于如何使用Swing和JLayer在Java中制作模糊的JFrame / JDialog&lt;&gt;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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