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

2012/6/7

getColorAT not working properly?

1
2
kiarocks kiarocks

2012/6/7

#
I am trying to recolor an image and I used this method to achieve it:
public void colorDiff(Color c)
    {
        GreenfootImage img = getImage();
        for(int x = 0; x < getImage().getWidth(); x++)
        {
            for(int y = 0; y < getImage().getHeight(); y++)
            {
                System.out.println(img.getColorAt(x, y)+" "+img.getTransparency()+" "+img.getColorAt(x, y).getAlpha()+ " "+x+" "+y);
                if(img.getColorAt(x, y) != Color.black)img.setColorAt(x, y, c);
                Greenfoot.delay(50);
            }
        }
        setImage(img);
    }
However it always colors the image completely one color, the one you hand it. I don't know if this is a error on my side or a bug, because there is absolutely no black in the Image, but it always reports this (printed from System.out line above)
java.awt.Color[r=0,g=0,b=0] 255 0 49 3
java.awt.Color[r=0,g=0,b=0] 255 0 49 4
java.awt.Color[r=0,g=0,b=0] 255 0 49 5
java.awt.Color[r=0,g=0,b=0] 255 0 49 6
java.awt.Color[r=0,g=0,b=0] 255 0 49 7
java.awt.Color[r=0,g=0,b=0] 255 0 49 8
java.awt.Color[r=0,g=0,b=0] 255 0 49 9
java.awt.Color[r=0,g=0,b=0] 255 0 49 10
java.awt.Color[r=0,g=0,b=0] 255 0 49 11
java.awt.Color[r=0,g=0,b=0] 255 0 49 12
java.awt.Color[r=0,g=0,b=0] 255 0 49 13
java.awt.Color[r=0,g=0,b=0] 255 0 49 14
java.awt.Color[r=0,g=0,b=0] 255 0 49 15
java.awt.Color[r=0,g=0,b=0] 255 0 49 16
java.awt.Color[r=0,g=0,b=0] 255 0 49 17
java.awt.Color[r=0,g=0,b=0] 255 0 49 18
java.awt.Color[r=0,g=0,b=0] 255 0 49 19
java.awt.Color[r=0,g=0,b=0] 255 0 49 20
java.awt.Color[r=0,g=0,b=0] 255 0 49 21
java.awt.Color[r=0,g=0,b=0] 255 0 49 22
java.awt.Color[r=0,g=0,b=0] 255 0 49 23
java.awt.Color[r=0,g=0,b=0] 255 0 49 24
java.awt.Color[r=0,g=0,b=0] 255 0 49 25
java.awt.Color[r=0,g=0,b=0] 255 0 49 26
java.awt.Color[r=0,g=0,b=0] 255 0 49 27
java.awt.Color[r=0,g=0,b=0] 255 0 49 28
java.awt.Color[r=0,g=0,b=0] 255 0 49 29
java.awt.Color[r=0,g=0,b=0] 255 0 49 30
java.awt.Color[r=0,g=0,b=0] 255 0 49 31
java.awt.Color[r=0,g=0,b=0] 255 0 49 32
java.awt.Color[r=0,g=0,b=0] 255 0 49 33
java.awt.Color[r=0,g=0,b=0] 255 0 49 34
java.awt.Color[r=0,g=0,b=0] 255 0 49 35
java.awt.Color[r=0,g=0,b=0] 255 0 49 36
java.awt.Color[r=0,g=0,b=0] 255 0 49 37
java.awt.Color[r=0,g=0,b=0] 255 0 49 38
java.awt.Color[r=0,g=0,b=0] 255 0 49 39
java.awt.Color[r=0,g=0,b=0] 255 0 49 40
java.awt.Color[r=0,g=0,b=0] 255 0 49 41
java.awt.Color[r=0,g=0,b=0] 255 0 49 42
java.awt.Color[r=0,g=0,b=0] 255 0 49 43
java.awt.Color[r=0,g=0,b=0] 255 0 49 44
java.awt.Color[r=0,g=0,b=0] 255 0 49 45
java.awt.Color[r=0,g=0,b=0] 255 0 49 46
java.awt.Color[r=0,g=0,b=0] 255 0 49 47
java.awt.Color[r=0,g=0,b=0] 255 0 49 48
java.awt.Color[r=0,g=0,b=0] 255 0 49 49
So is this because it is transparent? I painted it using a new color (new Color(255, 255, 255, 0)) so does it always go black? And if so, how can i fix this?
davmac davmac

2012/6/7

#
You're using '==' (or rather '!=') to compare objects; use .equals(...) instead. Even if the pixel is black, the Color object returned is unlikely to be the same object as 'Color.black'. So, change line 9: if (! img.getColorAt(x, y).equals(Color.black)) img.setColorAt(x, y, c);
davmac davmac

2012/6/7

#
(Also, even then, because of the transparency, I'm not sure it will work. You could compare each of the red/green/blue values separately instead).
kiarocks kiarocks

2012/6/7

#
I also used another println to see if they were, in fact, exactly the same. They were. Both getColorAt and Color.black have the same RGB values. So why is it black when painted white?
davmac davmac

2012/6/7

#
You don't understand me, I think. They might have the same values, but they are not the same *object*. The '==' and '!=' operators check object identity. Much as the "same" String can be held in two different String objects. Try the following code for example:
   String a = "a string";
   String b = new String(a);
   System.out.println("a = " + a);
   System.out.println("b = " + b);
   System.out.println("a == b ?: " + (a == b));
kiarocks kiarocks

2012/6/7

#
But shouldn't the white color values be kept when it is transparent? EDIT: Using .equals didn't change anything. The problem here is that transparent white is turned to transparent black.
davmac davmac

2012/6/7

#
I don't know, your code doesn't show any color corresponding to white - are you sure you're passing in white?
kiarocks kiarocks

2012/6/7

#
the image it works on is made like this
GreenfootImage img = new GreenfootImage(50,50);
img.setColor(new Color(0,0,0,0));
img.fill();
setImage(img);
danpost danpost

2012/6/7

#
Try sending the method 'new Color(0, 0, 0, 255)' instead of 'Color.black'.
davmac davmac

2012/6/7

#
the image it works on is made like this
But what is your colorDiff method code now? And how is it called?
kiarocks kiarocks

2012/6/7

#
Ok, let me show you what the whole code is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Square here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Square extends Actor
{
    Number link;
    boolean valid = true;
    Blank b;

    public Square()
    {
        GreenfootImage img = new GreenfootImage(50,50);
        img.setTransparency(255);
        img.setColor(new Color(255, 255, 255, 0));
        img.fill();
        /*img.setColor(Color.black);*/
        img.drawRect(0, 0, 49, 49);
        setImage(img);
    }
    
    /**
     * Act - do whatever the Square wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.mouseClicked(this) && valid && b.curr != null)
        {
            getImage().setColor(Color.black);
            getImage().drawLine(5, 5, 44, 44);
            getImage().drawLine(5, 44, 44, 5);
            b.getCurrPlayer().add(link.getVal());
            b.toggle();
            valid = false;
        }
        /*else if(Greenfoot.mouseClicked(this)) colorDiff(Color.green);*/
    }   
    
    public void colorDiff(Color c)
    {
        GreenfootImage img = getImage();
        for(int x = 0; x < getImage().getWidth(); x++)
        {
            for(int y = 0; y < getImage().getHeight(); y++)
            {
                System.out.println(img.getColorAt(x, y)+" "+img.getTransparency()+" "+img.getColorAt(x, y).getAlpha()+ " "+x+" "+y);
                if(img.getColorAt(x, y).equals(Color.black));else img.setColorAt(x, y, c);
                Greenfoot.delay(50);
            }
        }
        setImage(img);
    }
    
    public void addedToWorld(World w)
    {
        link = (Number)getOneIntersectingObject(Number.class);
        b = (Blank)w;
    }
}
I call the method by right click then for input java.awt.Color.green
davmac davmac

2012/6/7

#
It works for me. All the pixels that aren't black, are turned Green. That's exactly what the code says to do...
kiarocks kiarocks

2012/6/7

#
What version are you running? I'm running 2.2.1
davmac davmac

2012/6/7

#
The same. I don't understand what exactly happens when you run the code?
kiarocks kiarocks

2012/6/8

#
It just paints it green NVM, it just was me being stupid. It works fine!
There are more replies on the next page.
1
2