英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论