i have a sprite sheet(image) and i want to draw on screen.

image = sprite sheet image
x = player x postion
y = player y postion
width = player width
height = player heigh

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);
  }
}

this works fine and drraws a sprite sheet image. but the problem on sprite sheet there is a image of player. when it outs puts it draw the background blue, bc the sprite sheet background is blue.

is there way to del the background color using java code?? may be there is a funtion?

If you get the exact RGB value of the background then you can easily filter it out using java.awt.image.FilteredImageSource and a custom java.awt.image.RGBFilter like this:

final class BackgroundFilter extends java.awt.image.RGBImageFilter {
    @Override
    public int filterRGB(int x, int y, int rgb) {
        if((rgb & 0xffffff) == 0x0000ff) return 0;
        else return rgb;
    }
}

I tested this with two images and a javax.swing.JLayeredPane and it worked perfectly.

To fix the problem at its source convert the image file to a format that supports transparency, eg GIF and use an image editor to select end delete the background .

  • crossposted

  • again don't paint to the container directly, use JComponent, JPanel, JLabel

  • prepare an Image in SW that can do that, is designated for, save as png, put ImageIcon to the JLabel

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.