I've got a very basic rectangle drawing panel, but I would like to know if there is a simple way to add some sort of glow to the rectangles.
public class Blocks extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.BLACK);
for (int i = 1; i <= totalSteps; i++) {
g.setColor(Color.WHITE);
g.fillRect(100 + i*60, 260, 50, 50);
}
}
}
Generating a "glow" effect is a little bit involved, depending on what you want to achieve.
I use this approach to generate glow effects for transparent/non-rectangular shapes (great for generating drop shadows for example).
This example basically creates a
BufferedImage
which represents the "glow", it then generates a "mask" which cuts the originalBufferedImage
out of it. I do it this way, as it allows me to draw the "glow" beneath transparent/translucent images. In your case, you could skip the "masking" process, but that's up to you.You will also need a copy of the JHLabs, Image Filters, as I can't be bothered making my own blur filter
The basic work flow follows something like this:
BufferedImage
which represents the shape you want to apply a glow to (this is a opaque image)size
parameter, but which has the original image painted to it in the centerYou'll want to have a look at Compositing Graphics for more details about how the masking process works.
I use this kind of idea to generate drop shadows for transparent/no-rectangular shapes, for example, example and example