我如何将颜色设置为 java 中的整数?
How would I make a colour to an int in java?
我有一段文字想要制作随机颜色。我正在使用 Minecraft GUI 库。这是我的代码:
public void onEvent(event e) {
if (e instanceof EventRender) {
Gui.drawRect(5, 30, 70, 30 + Modules.Categories.values().length * 16 + 4, 0x90000000 );
// Here I want random color
Gui.drawRect(7, 33 + currentTab * 16, 9, 33 + currentTab * 16 + 12, -1);
int count = 0;
for(Categories c: Modules.Categories.values()) {
fr.drawStringWithShadow(c.name, 10, 36 + count * 16, -1);
count++;
}
List<Modules> modules = Client.getModulesByCategory(Modules.Categories.values()[currentTab]);
if(expanded) {
if(modules.size() <= 0)
return;
Gui.drawRect(70, 30, 70 + 68, 30 + modules.size() * 16 + 4, 0x90000000 );
Gui.drawRect(72, 33 + category.ModuleIndex * 16, 7 + 68, 33 + category.ModuleIndex * 16 + 12, -1));
for (Modules m : Client.getModulesByCategory(Modules.Categories.values()[currentTab]) ) {
fr.drawStringWithShadow(m.name , 10 + 70, -29 + count * 16, -1);
count++;
}
}
}
Gui.drawRect()
方法末尾的 -1
代表我想要的随机颜色。所以,我在 SO 中找到了一个随机颜色生成器。代码是new Color((int)(Math.random() * 0x1000000))
。但是当我尝试插入那段代码代替 -1
时,它说:
The Super Class only accepts ints as a colour.
那么有什么办法可以把那段代码变成整数吗?
class:
的完整代码
drawRect()
来自 Minecraft GUI 库的方法签名是:
public static void drawRect(int left, int top, int right, int bottom, int color)
因此,您不能只插入 Color
class 中的对象来代替 int color
参数。在这种情况下,您可以只使用 (int)(Math.random() * 0x1000000)
而无需创建 Color
对象。因此,您的代码部分应如下所示:
Gui.drawRect(7, 33 + currentTab * 16, 9, 33 + currentTab * 16 + 12, (int)(Math.random() * 0x1000000));
我有一段文字想要制作随机颜色。我正在使用 Minecraft GUI 库。这是我的代码:
public void onEvent(event e) {
if (e instanceof EventRender) {
Gui.drawRect(5, 30, 70, 30 + Modules.Categories.values().length * 16 + 4, 0x90000000 );
// Here I want random color
Gui.drawRect(7, 33 + currentTab * 16, 9, 33 + currentTab * 16 + 12, -1);
int count = 0;
for(Categories c: Modules.Categories.values()) {
fr.drawStringWithShadow(c.name, 10, 36 + count * 16, -1);
count++;
}
List<Modules> modules = Client.getModulesByCategory(Modules.Categories.values()[currentTab]);
if(expanded) {
if(modules.size() <= 0)
return;
Gui.drawRect(70, 30, 70 + 68, 30 + modules.size() * 16 + 4, 0x90000000 );
Gui.drawRect(72, 33 + category.ModuleIndex * 16, 7 + 68, 33 + category.ModuleIndex * 16 + 12, -1));
for (Modules m : Client.getModulesByCategory(Modules.Categories.values()[currentTab]) ) {
fr.drawStringWithShadow(m.name , 10 + 70, -29 + count * 16, -1);
count++;
}
}
}
Gui.drawRect()
方法末尾的 -1
代表我想要的随机颜色。所以,我在 SO 中找到了一个随机颜色生成器。代码是new Color((int)(Math.random() * 0x1000000))
。但是当我尝试插入那段代码代替 -1
时,它说:
The Super Class only accepts ints as a colour.
那么有什么办法可以把那段代码变成整数吗? class:
的完整代码drawRect()
来自 Minecraft GUI 库的方法签名是:
public static void drawRect(int left, int top, int right, int bottom, int color)
因此,您不能只插入 Color
class 中的对象来代替 int color
参数。在这种情况下,您可以只使用 (int)(Math.random() * 0x1000000)
而无需创建 Color
对象。因此,您的代码部分应如下所示:
Gui.drawRect(7, 33 + currentTab * 16, 9, 33 + currentTab * 16 + 12, (int)(Math.random() * 0x1000000));