Repainting a transparent bufferimage

Hello JGO

im triying to diaplay a grid on a transparent bufferimage, but i can’t find a viable way to repaint it to the screen without clearing the previous transparent bufferimage effectively. here is what i have:


//Create a blank transparent buffered image:
BufferedImage grid = new BufferedImage(800, 600, BufferedImage.TRANSLUCENT);
Graphics2D pintor = grid.createGraphics();

//transfer a single prototipe tile on to  my bufferedimage
prototipo.paint(pintor);

//painting it
public void printMapa(Graphics2D g){
	Rectangle2D.Double rect = new Rectangle2D.Double(0, 0, pantancho, pantalto);
	pintor.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
	pintor.fill(rect);
        g.drawImage(grid, 0, 0, padre);
        pintor = grid.createGraphics();
}


Any ideas…?

What exactly are you trying to do?

Firstly, thanks for your concern.

I have a blank bufferedimage (transparent) on the stage, where i am painting directly using a graphics2D object “pintor”.

I need to clear that image to it’s original state, to repaint the next frame of animation, not leaving a trace of what was there previuosly.

In Java creating a new BufferedImage for each frame of an animation is slow, but reusing images with transparency from frame to frame is tricky as BufferedImage doesn’t have an obvious way to set all of it’s pixels to completely transparent.

Unlike other colors we can’t just paint a transparent color over the image because, well, it’s transparent and won’t affect the pixels already in the image.

How can i clear or create a bufferedimage to hold new graphics assets in my repaint method?

Use AlphaComposite.CLEAR like you do now or just use AlphaComposite.SRC to paint your new image stuff - if I understand your issue correct.

Graphics.fillRect or clearRect?


Graphics g = myImage.getGraphics();
g.setColor(Color.white);
g.fillRect(0,0,myImage.getWidth(),myImage.getHeight());

Hello 65k

This way i can clear nicely my transparent bufferimage, but im having a problem. I cannot redraw anything on my image. It keeps blank. Any ideas?

Have you set the composite back to AlphaComposite.SrcOver?

no… ;D

Let me try it.

A simple fillRect is all that is needed, no need for AlphaComposite.

This is the solution:

   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! :wink:

To be a pedant, I think you could just use -

g.setComposite(AlphaComposite.Clear);

The alpha value should be irrelevant from memory, and it saves allocating a composite - them nanoseconds can build up! ;D