0

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??

teobais
  • 2,820
  • 1
  • 24
  • 36
Sandeep Kokate
  • 825
  • 4
  • 16
  • [please see below](http://stackoverflow.com/questions/6660908/how-to-make-jframe-transparent) you need to use `setWindowOpacity()` – Ashan Jan 29 '16 at 08:07
  • @ashan_SL That'll make the window see through, not really sure that's what the OP wants – MadProgrammer Jan 29 '16 at 08:18
  • @ashan_SL I am succeeded to increase and decrease the opacity to get Brightness effect, my problem is that, clicks doesn't pass through the panel to the Button components. – Sandeep Kokate Jan 29 '16 at 08:20
  • You might consider using a `JLayer` instead, which would allow you to affect the entire paint hierarchy of the container, have a look at [How to Decorate Components with the JLayer Class](http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html) for more details – MadProgrammer Jan 29 '16 at 08:20
  • @MadProgrammer : Thanks buddy.. !!! It's work with JLayer... – Sandeep Kokate Jan 29 '16 at 08:45

1 Answers1

3

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

Brighten

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)

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I have one off-topic question: Is it better to use `import javax.swing.whatever;` multiple times, than just `javax.swing.*` ? – dryairship Jan 29 '16 at 13:05
  • I think it's a matter of opinion, because at the end of the day, the JVM will be importing most of the classes anyway. This way I simple narrow the field and reduce the possibility that I get name clashes – MadProgrammer Jan 29 '16 at 19:46