我无法使用类常量设置随机颜色。
当我运行程序时,(这只是代码的一小部分)它给了我
类Graphics中的方法setColor不能应用于给定类型
我对如何设置颜色非常不熟悉,有人可以解释一下吗?
public static final int COLOR = (int) (Math.random() * 256);
for(int i = 1; i <= count; i++)
{
g.setColor(new Color(COLOR), (COLOR), (COLOR));
g.drawLine(r.nextInt(MIDX), r.nextInt(MIDY), r.nextInt(MIDX), r.nextInt(MIDY));
}
答案 0 :(得分:3)
试试这个
g.setColor(new Color(COLOR, COLOR, COLOR));
基本上这是你试图调用的Color Color(int r, int g, int b)
的构造函数。
r,g,b的值可以在0到255之间。在你的情况下,似乎r,g,b将具有与使用相同常数相同的值。
答案 1 :(得分:1)
首先你的颜色不是随机的,因为R,G和B都是相等的所以它会是灰色的
g.setColor(new Color(RCOMPONENT, GCOMPONENT , BCOMPONENT));
生成三个随机整数并按上述方式执行
答案 2 :(得分:0)
您必须在0 to 255
之间获得三个随机值,然后构造Color
对象。
int red=20;
int green=33;
int blue=33;
Color color=new Color(red,green,blue);
g.setColor(color);