I want to create a button that is not completely transparent but it has just a blurry background, like if someone sanded a piece of transparent plastic. How do I create something like that? Because all I found so far is how to make it completely transparent.
Asked
Active
Viewed 100 times
-1
-
I'd look towards using `JLayer`, a few examples using the previous iteration of the library `JXLayer`, [example](https://stackoverflow.com/questions/35079444/brightness-implementation-for-jpanel-in-swing/35079995#35079995); [example](https://stackoverflow.com/questions/19799451/java-blocking-focus-from-jcomponent/19801009#19801009); [example](https://stackoverflow.com/questions/29143251/how-to-make-a-blurry-jframe-jdialog-in-java-using-swing-and-jlayer/29143478#29143478) – MadProgrammer Jun 03 '22 at 22:30
1 Answers
0
I would start by taking a look at How to Decorate Components with the JLayer Class
Blurred...
JLayer<JComponent> layeredButton = new JLayer<>(bluredButton, new BlurLayerUI());
public class BlurLayerUI extends LayerUI<JComponent> {
private BufferedImage mOffscreenImage;
private BufferedImageOp mOperation;
private int radius = 3;
public BlurLayerUI() {
int size = radius * 2 + 1;
float weight = 1.0f / (float)(size * size);
float[] blurKernel = new float[size * size];
for (int index = 0; index < blurKernel.length; index++) {
blurKernel[index] = weight;
};
mOperation = new ConvolveOp(
new Kernel(size, size, 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 off-screen 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_ARGB);
}
Graphics2D ig2 = mOffscreenImage.createGraphics();
ig2.setClip(g.getClip());
super.paint(ig2, c);
ig2.dispose();
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(mOffscreenImage, mOperation, 0, 0);
}
}
nb: I prefer to use JH Lab's filters for blurring, it generally produces better results. GitHub, apparently it's also available in Maven
Alpha...
JLayer<JComponent> layeredButton = new JLayer<>(bluredButton, new AlphaLayerUI(0.1f));
public class AlphaLayerUI extends LayerUI<JComponent> {
private BufferedImage mOffscreenImage;
private BufferedImageOp mOperation;
private float alpha;
public AlphaLayerUI(float alpha) {
this.alpha = alpha;
}
@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 off-screen 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_ARGB);
}
Graphics2D ig2 = mOffscreenImage.createGraphics();
ig2.setComposite(AlphaComposite.SrcOver.derive(alpha));
ig2.setClip(g.getClip());
super.paint(ig2, c);
ig2.dispose();
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(mOffscreenImage, mOperation, 0, 0);
}
}
Combined...
JLayer<JComponent> layeredButton = new JLayer<>(bluredButton, new SandPaperLayerUI(0.25f));
public class SandPaperLayerUI extends LayerUI<JComponent> {
private BufferedImage mOffscreenImage;
private BufferedImageOp mOperation;
private int radius = 3;
private float alpha = 1.0f;
public SandPaperLayerUI(float alpha) {
this.alpha = alpha;
int size = radius * 2 + 1;
float weight = 1.0f / (float)(size * size);
float[] blurKernel = new float[size * size];
for (int index = 0; index < blurKernel.length; index++) {
blurKernel[index] = weight;
};
mOperation = new ConvolveOp(
new Kernel(size, size, 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 off-screen 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_ARGB);
}
Graphics2D ig2 = mOffscreenImage.createGraphics();
ig2.setComposite(AlphaComposite.SrcOver.derive(alpha));
ig2.setClip(g.getClip());
super.paint(ig2, c);
ig2.dispose();
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(mOffscreenImage, mOperation, 0, 0);
}
}

MadProgrammer
- 343,457
- 22
- 230
- 366