使用字符串在AnyLogic中设置颜色。

huangapple go评论67阅读模式
英文:

Setting color using a string in Anylogic

问题

我想要使用字符串参数(例如“cyan”)来设置代理中闭合多边形的填充颜色。该字符串是从数据库中读取的,因此无法将其读取为Color类型(至少我似乎无法这样做)。我尝试了各种选项,但通常会得到以下错误:

类型不匹配:无法从String转换为Color。

我最成功的尝试是使用getStandardColor(index)命令,其中我可以使用一个整数,但是我未能找到与140种标准颜色相对应的颜色列表。

是否可能使用字符串类型来设置颜色?

提前致谢。

英文:

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.

答案1

得分: 1

你可以这样做:

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

You can do this:

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

huangapple
  • 本文由 发表于 2020年10月15日 20:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64371219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定