Sign in to follow this  

[java] Changing Image Color?

This topic is 3723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, im new to game programming and was working on making my own centipede clone. if any of you are familar with centipede, the game colors (the worm color, the player color, and the obstacle color) changed with each level. I was wondering if there was an easy way to do this on the fly so instead of including a bunch of images for each level i could just have a "template" image and modify the image on the fly. When I took Java in school, the case study we used changed the color of the fish to match a user selected color which is where i got the idea. Any ideas/experience with implementing something like this? I was thinking some kid of recursive method working with each pixels color but im not very familiar with editing images. thanks in advance for any help with this.

Share this post


Link to post
Share on other sites
There is certainly a way you can access and modify the pixel data directly, somehow... However, this is probably much easier:

BufferedImage templateImage; //this is the original uncolored image

//get a graphics context for the image to draw to
Graphics2D g=(Graphics2D)templateImage.getGraphics();

AlphaComposite c=AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
g.setComposite (c);
g.setColor (Color.red);
g.fillRect (0, 0, width, height);

Where width/height are the dimensions of the image. Setting the alphacomposite object tells Graphics2D to combine the current image with the image being drawn, so instead of replacing the image with a red rectangle the current color becomes redder. There are several different options besides SRC_OVER. The value of alpha determines how the image is combined.

Share this post


Link to post
Share on other sites
thanks for the help, i got everything almost working, but im not sure what alpha is in your code. I looked up the getInstance() method and there is one with just a rule parameter, but another one with a rule and a float named alpha. My ommision of this is what i am guessing is causing my problem, but im not sure what the value should be. thanks again.


edit: right now, it is just drawing a rectangle over the image

[Edited by - mikesobczak on October 1, 2007 7:56:08 PM]

Share this post


Link to post
Share on other sites
Alpha is going to be a value between 0 and 1 which determines the intensity of the source image. If its .5, for example, the square will become half way red (or something like that). I guess the default value is 1, so its drawing a completely red square over it.

Share this post


Link to post
Share on other sites
okay, i understand that but im not sure what the alpha should be, when i looked at the documentation for AlphaComposite the stuff was WAY above my head. The doc assumes you have read some paper on alpha blending. is there some math i need to do to calculate the alpha? or is there a set value that will work?

Share this post


Link to post
Share on other sites
Oh, sorry I didn't elaborate on this in the first post. I figure 0.5 will work just fine, but you can just try it and see what happens. 0.1 will barely change the color of the original image at all, 0.9 will make it almost red (or whatever color you choose to draw over it. Doesn't have to be a solid color either, you can combine it with another image). I get confused by the detailed explanations of the blending functions myself, the fact that it doesn't require anything beyond multiplication and addition is cleverly hidden by complicated explanations I think...

Share this post


Link to post
Share on other sites

This topic is 3723 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this