简体   繁体   中英

How would I make a colour to an int in java?

I have a piece of text that I want to make a random color. I am using the Minecraft GUI Library. Here is my code:

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++;
        }
    }
}

That -1 at the end of Gui.drawRect() method represents color which I want as random. So, I have found a random color generator here in SO. The code is new Color((int)(Math.random() * 0x1000000)) . But when I try to plug in that piece of code in place of -1 , it says that:

The Super Class only accepts ints as a colour.

So is there a way I can turn that piece of code to an integer? Full code for that class:

drawRect() method signature from Minecraft GUI library is:

public static void drawRect(int left, int top, int right, int bottom, int color)

So, you can't just plug in the object from Color class in place of int color argument. In this case, you could simply use only (int)(Math.random() * 0x1000000) without creating a Color object. So, the part of your code should look like this:

Gui.drawRect(7, 33 + currentTab * 16, 9, 33 + currentTab * 16 + 12, (int)(Math.random() * 0x1000000));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM