Sign in to follow this  

[java] Clear bufferedImage to transparent

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a sprite that has 3 layers (a tank one layer for the treads, the body, and the turret. using a graphics2d to draw all the stuff in the direction it's supposed be facing since the tank can move in any direction and the turret can face any direction ). This resides inside it's own class(which is used for your tank slaved to keyboard controls and enemy tanks slaved to an AI 'spooler'). The gameloop uses a getCurrentSprite method to get the image which gets all the layers as a single BufferedImage. ______________ However I can't find a way to 'clear' an image so it has a new transparent backround. I really think I'm missing something really simple or something that uses a few libraries. making a new image causes the images to flicker probably because it's a slow process and gets interrupted. You can't draw a transparent image on top because that wouldn't do anything. You can't make your image = to another one because then you're just going to take it's reference/pointer/whatever you want to call it. You can't use clear rect method from what I've seen. I'm trying to avoid drawing the images directly on the panel because that would break a bunch of readability and OOiness(even if I'm drawing a dozen small images an extra time I'd prefer to).

Share this post


Link to post
Share on other sites
There are 2 ways that I am aware of for clearing a bufferedImage.

No 1. - Instantiate a new BufferedImage in its place. You are most probably getting a flicker because you are not using any sort of buffering.

No 2. - Using either RescaleOp or your own loop to turn each pixel transparent.

The Method would look like this,

public BufferedImage makeTransparent(BufferedImage bufImg)
{
for(int x = 0; x < bufImg.getWidth(); x++)
{
for(int y = 0; y < bufImg.getHeight(); y++)
{
bufImg.setRGB(x, y, (0xFF));
}
}
return bufImg;
}

However, I wouldn't recommend this method if its being called frequently or If your BufferedImages are overly large, either situation will result in some FPS droppage.

Hope this helps


Share this post


Link to post
Share on other sites
I was buffering the image but I was using passive rendering instead of active. Which works perfectly fine if you're just drawing primitive shapes and a few other effects read: not drawing other images.

I'm using the create new image... I'd love if there was a more efficient way to clear an image out.

Share this post


Link to post
Share on other sites

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this