This is the solution:
1 2 3 4 5 6 7
| public static void makeTransparent(BufferedImage img) { Graphics2D g = img.createGraphics(); g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f)); g.fillRect(0, 0, img.getWidth(), img.getHeight()); g.dispose(); } |
Note how it keeps the composite local to the temporary Graphics2D object, as not to 'influence' the state of the Graphics instance you're using to render after you cleared the image.
Dang, I was just halfway through correcting @ra4king when you posted!

To be a pedant, I think you could just use -
1
| g.setComposite(AlphaComposite.Clear); |
The alpha value should be irrelevant from memory, and it saves allocating a composite - them nanoseconds can build up!
