i want create brightness functionality in swing. jpanel , component's brightness level adjust in functionality.
to achieve used jlayeredpane , added jpanel brightnesspanel on top of jpanel called mainpanel. giving brightness effect changing opacity of brightnesspanel. simulate brightness effect mainpanel.
now problem that, not able click buttons present on mainpanel because of layer of brightnesspanel.
how pass through clicks brightnesspanel buttons present on mainpanel??
you might able make use jlayer
api allows 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 advantages of introduce "brightness" algorithm, rather faking it, using backing buffer, demonstrated link above (this use to blur ui)
Comments
Post a Comment