This site requires JavaScript, please enable it in your browser!
Greenfoot back
seanpham189
seanpham189 wrote ...

2017/4/23

Background

seanpham189 seanpham189

2017/4/23

#
I have a problem here. I need to use some image for my project but when I set image in greenfoot, the image always appears with the white background. So what should I do to make the white background disappear. thank you so much guys.
Super_Hippo Super_Hippo

2017/4/23

#
If you don't have a program to have transparent pixels, you can also do it in Greenfoot:
public GreenfootImage removeWhite(GreenfootImage image)
{
    if (image == null) return null;
    int width=image.getWidth(), height=image.getHeight();
    GreenfootImage newImage = new GreenfootImage(width, height);
    Color c=null, white=new Color(255, 255, 255);
    for (int x=0; x<width; x++) for (int y=0; y<height; y++)
    {
        c = image.getColorAt(x, y);
        if (!c.equals(white)) newImage.setColorAt(x, y, c);
    }
    return newImage;
}
Then you can do this to set the image:
setImage(removeWhite(new GreenfootImage("your image name.png")));
However, this will remove every white pixel.
You need to login to post a reply.