Graphics2D: How to reset a BufferedImage with an Alpha chanel

C

charlym

Hello newsgroup!

I am trying to reset a specific area in a BufferedImage that has been
set up using an Alpha chanel (TYPE_INT_ARGB). The idea is that I have
to refresh a certain area an therefore I want to clear it before I
redraw it. Currently I am using the following code:

In the constructor:
this.image = new BufferedImage(this.getWidth(),
this.getHeight(), BufferedImage.TYPE_INT_ARGB);
this.dirtyHack = new BufferedImage(this.getWidth(),
this.getHeight(), BufferedImage.TYPE_INT_ARGB);

In the paintComponent method:
image.setData(dirtyHack.getData(bounds));

This works, but it uses a lot of memory. Is there a cheaper way of
doing this?

Thanks for your help!
 
T

Thomas Weidenfeller

charlym said:
I am trying to reset a specific area in a BufferedImage [...]

This works, but it uses a lot of memory. Is there a cheaper way of
doing this?

image.getGraphics().clearRect(...);

Check the API doc for details.

/Thomas
 
C

charlym

Thomas & the rest of the newsgroup,

I tried this but it does not do what I expected. I want the rectangle
to be totally transparent. clearRect fills the rectangle with the
backgroundcolor :-( I also tried to change the color using a
transparent color (new Color(0,0,0,0)) but it did not help either.

Any other ideas?
 
O

Oliver Wong

charlym said:
Thomas & the rest of the newsgroup,

I tried this but it does not do what I expected. I want the rectangle
to be totally transparent. clearRect fills the rectangle with the
backgroundcolor :-( I also tried to change the color using a
transparent color (new Color(0,0,0,0)) but it did not help either.

Any other ideas?

From the javadocs:

<quote>
Beginning with Java 1.1, the background color of offscreen images may be
system dependent. Applications should use setColor followed by fillRect to
ensure that an offscreen image is cleared to a specific color.
</quote>

Did you try that?

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top