[java] Changing Image Color?

Started by
4 comments, last by PorkBag 16 years, 6 months ago
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.
Advertisement
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.
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]
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.
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?
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...

This topic is closed to new replies.

Advertisement