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

2013/2/21

Problem about setColorAt & getColorAt method

1
2
Upupzealot Upupzealot

2013/2/21

#
1
2
3
4
5
int red;
int alpha = 15;
GreenfootImage test = new GreenfootImage(1, 1);
test.setColorAt(0 , 0, new java.awt.Color(123, 45, 67, alpha ));
red = test.getColorAt(0, 0).getRed();
find that red no longer equals 123, and actually it changes with alpha, how could that happen?
MatheMagician MatheMagician

2013/2/21

#
I tested the code and used System.out.println and it worked fine for me. It continually gave me 123, no matter how I changed alpha. Now, sometimes when I draw my images in an editor, the colors will not match up. I am not sure if this is your problem, if so, I just use a color threshold (i.e. if(color.getRed()>100&&color.getRed()<140)).
Upupzealot Upupzealot

2013/2/21

#
I tried again, using System.out.println(red); And again it's chaning... Could that be something wrong with my computer? BTW, what's the OS of your computer? Mine is Win7.
Upupzealot Upupzealot

2013/2/21

#
Alright, another friend of mine was having the same problem, both of us was running Greenfoot on Win7.
danpost danpost

2013/2/21

#
I am running win7 and NOT having that problem (Greenfoot is USB 2.2.1).
Upupzealot Upupzealot

2013/2/21

#
I tried standalone edition(USB) and pure_java, still...it changes.
Upupzealot Upupzealot

2013/2/21

#
I have uninstalled my JDK and Greenfoot and now using (Greenfoot USB 2.2.1). But it didn't work. I would try other methods later, if anybody could help, thanks.
MatheMagician MatheMagician

2013/2/21

#
I am on windows7 and it is working for me!!! This is very strange.
Upupzealot Upupzealot

2013/2/23

#
Problem solved!
Upupzealot Upupzealot

2013/2/23

#
test Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int rgbFromColor;
    int rgb1_GreenfootImage;
    int rgb1_BufferedImage_TYPE_4BYTE_ABGR_PRE;
    int rgb1_BufferedImage_TYPE_4BYTE_ABGR;
    public void test()
    {   
        int alpha = 15;  
        GreenfootImage greenfootImage = new GreenfootImage(1, 1);
        BufferedImage bufferedImage_TYPE_4BYTE_ABGR_PRE = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR_PRE);
        BufferedImage bufferedImage_TYPE_4BYTE_ABGR = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
        Color color = new Color(123, 45, 67, alpha );
        rgbFromColor = color.getRGB();
        greenfootImage.setColorAt(0, 0, color);
        bufferedImage_TYPE_4BYTE_ABGR_PRE.setRGB(0, 0, rgbFromColor);
        bufferedImage_TYPE_4BYTE_ABGR.setRGB(0, 0, rgbFromColor);
        rgb1_GreenfootImage = greenfootImage.getColorAt(0, 0).getRGB();
        rgb1_BufferedImage_TYPE_4BYTE_ABGR_PRE = bufferedImage_TYPE_4BYTE_ABGR_PRE.getRGB(0, 0);
        rgb1_BufferedImage_TYPE_4BYTE_ABGR = bufferedImage_TYPE_4BYTE_ABGR.getRGB(0, 0);
    }
Upupzealot Upupzealot

2013/2/23

#
test Result: Flickr 上 upupzealotQQ截图20130223205725
Upupzealot Upupzealot

2013/2/23

#
Looked into the Souce code of Greenfoot, setColor and getColor is actually getRGB and setRGB of an private BufferedImage. the Problem is GreenfootImage(on my computer) was using an bufferedImage of the type BufferedImage.TYPE_4BYTE_ABGR_PRE and if it is BufferedImage.TYPE_4BYTE_ABGR the problem no longer exists.
Upupzealot Upupzealot

2013/2/23

#
Flickr 上 upupzealotQQ截图20130223210417 check this int for any GreenfootImage in your Greenfoot. Flickr 上 upupzealotQQ截图20130223210646
davmac davmac

2013/2/25

#
Right; TYPE_INT_ARGB_PRE means the colour values (red, green, blue) are pre-multiplied by the alpha, which may be faster (depending on your graphics card). However, that loses some precision in the conversion. So, the color you get back is almost the same, but some values have changed a bit. The lower the alpha value, the less precision you have. If you set the alpha = 0, you'll find that the color that comes back is always (0,0,0,0). Basically, you shouldn't rely on the color coming back being exactly the same as the one you set.
Upupzealot Upupzealot

2013/2/26

#
Then why not GreenfootImage use an BufferedImage in TYPE_INT_ARGB?
There are more replies on the next page.
1
2