Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have a sprite sheet (image) and I want to draw it on the screen.

image = sprite sheet image
x = player x position
y = player y position
width = player width
height = player height

public class main extends japplet

  public void paint(Graphics g)
      g.drawImage(image, x, y, x+width, y+height, 
                   0, 0, 40, 40 ,sprite_sheet.m1);

So on the sprite sheet I have the image of a player and its background color is blue. When I call drawImage it shows the blue background too. I could remove the color by using Photoshop but I really don't want to do this because it will remove part of my player.

Is there a way to remove the background color using Java code? Maybe there is a function?

share|improve this question
Are you really having that big a problem using Photoshop that you think it'd be easier to do this programmatically? Anyway, this is a question for StackOverflow –  user1306322 Mar 23 '13 at 3:26
This should help stackoverflow.com/questions/14097386/… –  Savlon Mar 23 '13 at 13:57
What exactly do you mean by "it will remove part of my player"? If you (as a person) cannot remove the color in Photoshop, how is an algorithm supposed to do it automatically? –  Thomas Mar 24 '13 at 5:38
It might help if you included a picture of your actual sprite, and some hint of how you want it to look like in the game (and maybe an example of what goes wrong when you try to remove the background in Photoshop). –  Ilmari Karonen Mar 24 '13 at 13:22
add comment

1 Answer

It seems you want to do some color keying, which Java cannot do on the fly with the Graphics2D library you are using. Due to this, you will need to create the alpha channel yourself, move to something that has color key support (SDL). Most engines nowadays do not support this operation as it is computationally expensive and you will need to do this "on the fly" to convert it anyway.

I have a nice answer here on why you should use alpha transparency instead.

If you have a large code base or insist on writing your rendering back end in Java2D with color key mapped images, then you can always decide to do something by hand. As linked in the comments, there is a a good post on StackOverflow on how to do this here.

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.