I am creating a cricle gradient using RadialGradientPaint, putting that on a BufferedImage and rendering the Image on top of my 2d game screen, creating a nice light-in-the-dark effect. I would, however, like to create more light sources, but creating and rendering a new BufferedImage for each light doesn't do the job (usually just the last light is seen, everything else is black). Is it possible to bake a few RadialGradientPaints into one BufferedImage or achieve the multiple lights effect in some other way?
Attached you can find the image of how one light looks like. It is a black BufferedImage with a RadialGradientPaint applied rendered on top of the screen. I would like to add more of these somehow.
Solution to this problem is using this (as pointed in the comment by @JanDvorak : Merging two images
The exact code I use is this:
public static BufferedImage mergeImages2(BufferedImage base, Collection<Light> images) {
BufferedImage output = new BufferedImage(base.getWidth(), base.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = output.createGraphics();
g.drawImage(base, 0, 0, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_IN, 1.0f));
for (Light l : images) {
g.drawImage(l.LIGHT_IMAGE, l.x, l.y, null);
l.LIGHT_IMAGE.flush();
}
g.dispose();
output.flush();
return output;
}
NOTE: This solves the problem but produces memory leaks, which I have described hoping for some help here: BufferedImage.createGraphics() memory leak