我想通过我的字符串参数设置Polygon
的颜色。这是我的代码:
public void polygon(int xPoints[], int yPoints[], int nPoints, String col) {
this.graphics.setColor(col);
this.graphics.drawPolygon(xPoints, yPoints, nPoints);
}
这不起作用。因此,经过一些研究,我尝试在代码中添加 Color c = c.web(col);
这也行不通。如何将字符串 col
转换为 color
?
最佳答案
您可以使用以下内容:
// Fill the map with colors you required
static Map<String, Color> colorMap = Map.ofEntries(Map.entry("RED", Color.RED),
Map.entry("BLUE", Color.BLUE),
Map.entry( "BLACK", Color.BLACK),
Map.entry( "ORANGE", Color.ORANGE));
static Color getColor(String col)
{
return colorMap.get(col.toUpperCase());
}
public void polygon(int xPoints[], int yPoints[], int nPoints, String col)
{
this.graphics.drawPolygon(xPoints, yPoints, nPoints);
this.graphics.setColor( getColor(col) );
}
关于java - java awt中从字符串参数到颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59459656/