A Map would be better than two separate arrays, then you could have a getColour method which took a String as a parameter and use that as a key into the Map. The user wouldn't have to know the index of each of the colours then.
(blushing) Bringing this thread back on-topic... the other part of the answer to the original question is "No, you may find it surprising, but there is nothing in the standard Java API to convert strings to Color objects, at least, not in the way you describe."
Kee Kee moon
Joined: Dec 11, 2009
Thank you very much that is exactly what I was looking for.
Randall Twede wrote:is this what you were looking for?
If you want to map the named constants of the Color class (or any other) which are instances of the same class, here's a utility I wrote some time back that uses Reflection to do it.Usage to obtain a Map of static Color fields of the Color class:
edit: Thought I should mention that the class is abstract to force instances to be named or anonymous subclasses, which makes it possible to obtain the ParameterizedType from the genericSuperclass.
There are no new questions, but there may be new answers.
Rob Spoor wrote:Your class is missing the following three methods:
The first two are required as per the contract of java.util.Map.
Thanks Rob, for pointing that out. I'll keep that in mind if I ever publish the class.
Rob Spoor wrote:I actually don't like this trick. Instead of an entire class I'd use one static utility method instead:
I actually had this approach too, and the code is very similar to yours (but yours is more complete in that it checks the modifiers)I didn't think of using Class#cast(...) instead of suppressing the warning.
subject: converting a string color to an object color