File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Android Security Essentials Live Lessons this week in the Android 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: 19656
    
  18

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: 38100
    
  22
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: 41180
    
  45
Maybe you can replace the color of each pixel that's white by red?


Ping & DNS - my free Android networking tools app
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: 19656
    
  18

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: 19656
    
  18

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