Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting java.awt.Image background color

 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of thing is usually discussed in our Swing forum.
Moving.
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its actually an image with a white background. Its not transparent.

Vijay.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you can replace the color of each pixel that's white by red?
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh really! Can that be done?

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

VIjay.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am actually trying to find how to get a BufferedImage from a java.awt.Image?

Vijay.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like so:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic