Swing 中 JPanel 的亮度实现 [英] Brightness implementation for JPanel in Swing

查看:46
本文介绍了Swing 中 JPanel 的亮度实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Swing 中创建亮度功能.JPanel 及其组件的亮度级别将在此功能中进行调整.

为了实现这一点,我使用了 JLayeredPane 并在名为 MainPanelJPanel 顶部添加了 JPanel 作为 BrightNessPanel.我通过改变 BrightNessPanel 的不透明度来提供亮度效果.这将为我的 MainPanel 模拟亮度效果.

现在的问题是,由于 BrightNessPanel 层,我无法点击 MainPanel 上的按钮.

如何将来自 BrightNessPanel 的点击传递到 MainPanel 上的按钮??

解决方案

您可以使用

import java.awt.AlphaComposite;导入 java.awt.BorderLayout;导入 java.awt.Dimension;导入 java.awt.EventQueue;导入 java.awt.Graphics;导入 java.awt.Graphics2D;导入 java.awt.GridBagConstraints;导入 java.awt.GridBagLayout;导入 javax.swing.JButton;导入 javax.swing.JComponent;导入 javax.swing.JFrame;导入 javax.swing.JLayer;导入 javax.swing.JPanel;导入 javax.swing.JSlider;导入 javax.swing.JTextField;导入 javax.swing.UIManager;导入 javax.swing.UnsupportedLookAndFeelException;导入 javax.swing.event.ChangeEvent;导入 javax.swing.event.ChangeListener;导入 javax.swing.plaf.LayerUI;公共类测试{公共静态无效主(字符串 [] args){新测试();}公共测试(){EventQueue.invokeLater(new Runnable() {@覆盖公共无效运行(){尝试 {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {ex.printStackTrace();}TestPane testPane = new TestPane();BrightnessLayerUI layerUI = new BrightnessLayerUI();JLayer<JComponent>layer = new JLayer(testPane, layerUI);JSlider 滑块 = new JSlider(0, 100);滑块.addChangeListener(新的ChangeListener(){@覆盖public void stateChanged(ChangeEvent e) {int value = slider.getValue();浮动亮度 = (100 - 值)/100f;layerUI.setBrightness(brightness);testPane.repaint();}});滑块.setValue(100);JFrame frame = new JFrame("测试");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);框架.添加(层);frame.add(slider, BorderLayout.SOUTH);框架.pack();frame.setLocationRelativeTo(null);frame.setVisible(true);}});}公共类 TestPane 扩展 JPanel {公共测试窗格(){setLayout(new GridBagLayout());GridBagConstraints gbc = new GridBagConstraints();gbc.gridwidth = GridBagConstraints.REMAINDER;添加(新 JTextField(10),gbc);add(new JButton("Hello"), gbc);}@覆盖公共维度 getPreferredSize() {返回新维度(200, 200);}@覆盖受保护的无效paintComponent(图形g){super.paintComponent(g);Graphics2D g2d = (Graphics2D) g.create();g2d.dispose();}}公共类 BrightnessLayerUI 扩展了 LayerUI<JComponent>{私人浮动亮度 = 0f;公共无效设置亮度(浮动亮度){this.brightness = 亮度;}公共浮动 getBrightness() {返回亮度;}@覆盖公共无效油漆(图形g,JComponent c){super.paint(g, c);Graphics2D g2d = (Graphics2D) g.create();g2d.setColor(c.getBackground());g2d.setComposite(AlphaComposite.SrcOver.derive(getBrightness()));g2d.fillRect(0, 0, c.getWidth(), c.getHeight());g2d.dispose();}}}

这样做的一个优点是您可以通过使用后备缓冲区实际引入亮度"算法,而不是伪造它,这在上面的链接中进行了演示(用于模糊 UI)

I want to create brightness functionality in Swing. JPanel and its component's brightness level will be adjust in this functionality.

To achieve this I used JLayeredPane and added JPanel as BrightNessPanel on the top of my JPanel called MainPanel. I am giving brightness effect by changing the opacity of BrightNessPanel. This will simulate Brightness effect for my MainPanel.

Now problem is that, I am not able to click the buttons present on MainPanel because of layer of BrightNessPanel.

How do I pass through clicks from BrightNessPanel to the buttons present on MainPanel??

解决方案

You might be able to make use the JLayer API which allows you to perform painting operations ontop of other components.

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLayer;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.LayerUI;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                TestPane testPane = new TestPane();
                BrightnessLayerUI layerUI = new BrightnessLayerUI();
                JLayer<JComponent> layer = new JLayer<>(testPane, layerUI);

                JSlider slider = new JSlider(0, 100);
                slider.addChangeListener(new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        int value = slider.getValue();
                        float brightness = (100 - value) / 100f;
                        layerUI.setBrightness(brightness);
                        testPane.repaint();
                    }
                });
                slider.setValue(100);

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(layer);
                frame.add(slider, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            add(new JTextField(10), gbc);
            add(new JButton("Hello"), gbc);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.dispose();
        }

    }

    public class BrightnessLayerUI extends LayerUI<JComponent> {

        private float brightness = 0f;

        public void setBrightness(float brightness) {
            this.brightness = brightness;
        }

        public float getBrightness() {
            return brightness;
        }

        @Override
        public void paint(Graphics g, JComponent c) {
            super.paint(g, c);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setColor(c.getBackground());
            g2d.setComposite(AlphaComposite.SrcOver.derive(getBrightness()));
            g2d.fillRect(0, 0, c.getWidth(), c.getHeight());
            g2d.dispose();
        }

    }

}

One of the advantages of this is you could actually introduce a "brightness" algorithm, rather than faking it, by using a backing buffer, which is demonstrated the link above (this use to to blur the UI)

这篇关于Swing 中 JPanel 的亮度实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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