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

2017/3/1

Color.getRGB()

Busch2207 Busch2207

2017/3/1

#
Hello, I just downloaded the latest Greenfoot-Version (My last version was 2.6). Now I had to replace all lowerCase Colors with the upper cases, which was no big problem. But now I have a very large problem: The constructor 'new Color(int)' doesn't exist, also the method Color.getRGB() does not exist. Is it possible, to add this two small functionalities to the next version? This would make it easier to put together different java.awt.Color and greenfoot.Color - applications/algorithms. And as sideeffect I would not have to rewrite hundreds of lines in many different projects to restore their functionality.
Nosson1459 Nosson1459

2017/3/2

#
I purposely am not using Greenfoot Version 3.1.0 yet because of the errors/bugs that come with it. I'm waiting for later versions of Greenfoot that these problems are fixed and I guess also in case these missing methods get added (I'm staying with Version 3.0.4). (I didn't know you're still around, it seems like the last time you've been on this site was more than a year ago.)
davmac davmac

2017/3/2

#
I will try to remember to raise this in our next developer meeting, but I suspect the decision will be not to add the getRGB() method and the `int` constructor, out of desire to keep the API small and simple, which overrides to some degree the desire for backwards compatibility. You can implement them yourself, something like:
1
2
3
4
5
6
7
8
9
10
11
12
public static int getRGB(Color c)
{
    return (c.getRed() << 16) +  (c.getGreen() << 8) + c.getBlue();
}
 
public static Color colorFromRGB(int rgb)
{
    int r = (rgb >> 16) & 0xFF;
    int g = (rgb >> 8) & 0xFF;
    int b = rgb & 0xFF;
    return new Color(r,g,b);
}
Also, if you really want, you can create your own Color class which extends greenfoot.Color, and add the constructor and getRGB method to it. Instances of this class could then be used anywhere that greenfoot.Color is accepted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Color extends greenfoot.Color
{
    public Color(int rgb)
    {
        super((rgb >> 16) & 0xFF, (rgb >> 8) && 0xFF, rgb & 0xFF);
    }
 
    // add other constructors which delegate to superclass as appropriate
 
    public int getRGB()
    {
        return (getRed() << 16) +  (getGreen() << 8) + getBlue();
    }
}
Busch2207 Busch2207

2017/3/4

#
Well, ok. But I don't think, this one constructor and one method will make it so unclear... Of course I know about the methods, for creating the int-values, but it would still be a big effort to change all method-calls... The second method is better, but I still don't like it much, to overwrite a class with a class with the same name. In my opionion the understanding of the int-value for the colors is a basis for programming, so I think, for some newbies it also might be interesting.
You need to login to post a reply.