Wednesday, October 21, 2009

Swing Low

Thanks to the book Swing Hacks for this one. An extended version of JButton that blurs when it's disabled.

/**
*
* @author bhegberg
*/
public class BlurJButton extends JButton {
public BlurJButton(String text) {
super(text);
}

public void paintComponent(Graphics g) {
if (isEnabled()) {
super.paintComponent(g);
return;
}
BufferedImage buf = new BufferedImage(getWidth(),getHeight(), BufferedImage.TYPE_INT_RGB);
super.paintComponent(buf.getGraphics());
// Blur the buffered image
float[] my_kernel = {
0.10f, 0.10f, 0.10f,
0.10f, 0.20f, 0.10f,
0.10f, 0.10f, 0.10f };
ConvolveOp op = new ConvolveOp(new Kernel(3,3, my_kernel), ConvolveOp.EDGE_NO_OP, null);
Image img = op.filter(buf,null);

g.drawImage(img,0,0,null);
}
}