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

How can i change the Color of Images on JComponent

 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do i change the color of Image on my JComponent.I placing an Image on JComponent through setIcon() method and passing ImageICon object.How can i change the color of the image which i passed to this Image Object.Or please sussest me any better way of doiing it.
I will be knowing the colors only at runTime.
here is my code which will explain easy to understand...
Say ...
JLabel label=new JLabel("My text");
ImageIcon icon=new IMageIcon("myImage.gif");
label.setIcon(icon);
now how can i change the color of this image in runtime.
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want to change the color? Normally an images has more than one color. Do you want to replace blue by red or something else? Anyway, you should write a bit more.
If you want to do something with images, then probably Java Advanced Imaging would be a good solution.
 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rene Liebmann:
Why do you want to change the color? Normally an images has more than one color. Do you want to replace blue by red or something else? Anyway, you should write a bit more.
If you want to do something with images, then probably Java Advanced Imaging would be a good solution.

i want to change the color of image based on user requirements.The original image color may be anything which i am least bothered about.
thanks in advance.
 
G Estes
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your image comes from a gif file, then you can't change it dynamically unless you load a different gif. What I think you may be getting at is changing the appearance of an image based on a given condition or state. If this is the case, then make the gif image have a transparent background. Place the gif into an image and change the image background to what is needed. For instance, have an image of a computer with a green background for available, yellow for in use and red for not operating. All you have to do is call image.setBackground(Color.xxx).
 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gil Estes:
If your image comes from a gif file, then you can't change it dynamically unless you load a different gif. What I think you may be getting at is changing the appearance of an image based on a given condition or state. If this is the case, then make the gif image have a transparent background. Place the gif into an image and change the image background to what is needed. For instance, have an image of a computer with a green background for available, yellow for in use and red for not operating. All you have to do is call image.setBackground(Color.xxx).

you guessed right!but i didn't get clear picture
what kind of image object i need to place on the component at the first place.because i am not seeing any methods like setbackground() on Images.
do u mean..

JLabel label =new JLabel();
label.setICon("myICon.gif");
label.setOpaque(true);
label.setBackground(myColorObj);
thanks for u r reply..
 
Bhagya Tangutur
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know that i can make the image transaprent.by changing image to transparent and setting background,it works fine.
Thanks
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"kumar" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

Thanks! and welcome to the JavaRanch!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic