aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Setting java.awt.Image background color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting java.awt.Image background color" Watch "Setting java.awt.Image background color" New topic
Author

Setting java.awt.Image background color

Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
I use a third party API to generate a barcode. It gives me a java.awt.Image. Is there a way to set the background color of the java.awt.Image to RED? The API has none.

I went through the Java API but could not find it.

Vijay.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Is the image you got transparent? If so, you can create a new BufferedImage, fill a red rectangle, then paint the original image on it.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
This sort of thing is usually discussed in our Swing forum.
Moving.
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Its actually an image with a white background. Its not transparent.

Vijay.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Maybe you can replace the color of each pixel that's white by red?


Ping & DNS - updated with new look and Ping home screen widget
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
Oh really! Can that be done?

Get the image in a byte stream, read the byte, then?

VIjay.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

BufferedImage again comes in to the rescue.

Create a BufferedImage with the same size. Paint the original image on the BufferedImage. Then, go through all pixels of the BufferedImage using getWidth(), getHeight() and getRGB(), and reset the pixel using setRGB if it's a white pixel.
Vijay Raj
Ranch Hand

Joined: Oct 10, 2005
Posts: 110
I am actually trying to find how to get a BufferedImage from a java.awt.Image?

Vijay.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Like so:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting java.awt.Image background color
 
Similar Threads
To get the color or pixels when the cursor move
JButton background color
How to make TextVIew behave like EditText
JTable -background color
Java API for image processing