Question

I'm fairly new to Java, and completely new to editing images inside Java. What I have is an image being loaded into my program that contains a 32x32 image of leaves with three shades of gray. What I have is leaf BufferedImage, which holds the gray leaf sprite. I also have a greenLeaf, redLeaf, orangeLeaf, and pinkLeaf BufferedImage. When I run the code below, for some reason the last color overwrites all of the other colors, and they all turn orange. Can anyone tell me why, and a way around it?

public void colorLeaves()
{
            leaf=ss.grab32Image(4, 1);
    greenLeaf=leaf;
    redLeaf=leaf;
    pinkLeaf=leaf;
    orangeLeaf=leaf;
    for(int xx=0;xx<leaf.getWidth();xx++)
    {
        for(int yy=0;yy<leaf.getHeight();yy++)
        {
            int clr = leaf.getRGB(xx, yy);
            int red = (clr & 0x00ff0000) >> 16;
            int green = (clr & 0x0000ff00) >> 8;
            int blue = clr & 0x000000ff;
            Color c1=new Color(0,0,0);
            Color c2=new Color(0,0,0);
            Color c3=new Color(0,0,0);

            /*
             * GREEN
             */
            c1 = new Color(0,158,15); //Middle Shade
            c2 = new Color(0,119,11); //Dark Shade
            c3 = new Color(0,198,16); //Light Shade
            if(red==128&&green==128&&blue==128)
            {
                greenLeaf.setRGB(xx, yy, c1.getRGB());
            }
            if(red==96&&green==96&&blue==96)
            {
                greenLeaf.setRGB(xx, yy, c2.getRGB());
            }
            if(red==165&&green==165&&blue==165)
            {
                greenLeaf.setRGB(xx, yy, c3.getRGB());
            }

            /*
             * RED
             */

            c1 = new Color(219,26,42); //Middle Shade
            c2 = new Color(183,22,35); //Dark Shade
            c3 = new Color(247,56,72); //Light Shade
            if(red==128&&green==128&&blue==128)
            {
                redLeaf.setRGB(xx, yy, c1.getRGB());
            }
            if(red==96&&green==96&&blue==96)
            {
                redLeaf.setRGB(xx, yy, c2.getRGB());
            }
            if(red==165&&green==165&&blue==165)
            {
                redLeaf.setRGB(xx, yy, c3.getRGB());
            }

            /*
             * PINK
             */

            c1 = new Color(255,56,152); //Middle Shade
            c2 = new Color(216,49,130); //Dark Shade
            c3 = new Color(255,102,175); //Light Shade

            if(red==128&&green==128&&blue==128)
            {
                pinkLeaf.setRGB(xx, yy, c1.getRGB());
            }
            if(red==96&&green==96&&blue==96)
            {
                pinkLeaf.setRGB(xx, yy, c2.getRGB());
            }
            if(red==165&&green==165&&blue==165)
            {
                pinkLeaf.setRGB(xx, yy, c3.getRGB());
            }

            /*
             * ORANGE
             */

            c1 = new Color(255,110,38); //Middle Shade
            c2 = new Color(224,97,33); //Dark Shade
            c3 = new Color(249,136,79); //Light Shade

            if(red==128&&green==128&&blue==128)
            {
                orangeLeaf.setRGB(xx, yy, c1.getRGB());
            }
            if(red==96&&green==96&&blue==96)
            {
                orangeLeaf.setRGB(xx, yy, c2.getRGB());
            }
            if(red==165&&green==165&&blue==165)
            {
                orangeLeaf.setRGB(xx, yy, c3.getRGB());
            }
        }
    }
}
Was it helpful?

Solution

redLeaf, greenLeaf, pinkLeaf and orangeLeaf all references to the same object. SO any modifications done to one, will change all the others. This is why only the last change takes place.

You can see this thread on how to clone a buffered image: How do you clone a BufferedImage

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top