bluechimp
bluechimp

Reputation: 59

Setting color using a string in Anylogic

I would like to set the fill colour of a closed polygon in an agent using a string parameter (for example "cyan"). The string is read in from a database so it can't be read in as a Color type (at least it doesn't seem like I can). I have tried various options but usually get the following:

Type mismatch: cannot convert from String to Color.

The best that I have managed is to use the getStandardColor(index) command, where I can use an integer, but then I haven't been able to find the list of colours corresponding to the 140 standard colours.

Is it possible to set the colour using a string type?

Thanks in advance.

Upvotes: 0

Views: 430

Answers (1)

Felipe
Felipe

Reputation: 9421

You can do this:

Color color;
String yourColor="red";
try {
     color = (Color)Color.class.getField(yourColor).get(null);
} catch (Exception e) {
    color = null;
}

Upvotes: 1

Related Questions