How to add RGB values into setColor() in Java?

我的未来我决定 提交于 2020-05-26 11:00:07

问题


How can I add (red, green, blue) values to my Java? For example:

 setColor(255, 0, 0);

The context looks like this:

public void render() {
    BufferStrategy bs = getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();

    g.setColor(); // <-- This line
    g.fillRect(0, 0, getWidth(), getHeight());

    g.dispose();
    bs.show();
}

I want to give my rectangle a color using RGB values like (200, 200, 200) for example; that'll be like a gray.


回答1:


You can get a Color instance with the simple code:

Color myWhite = new Color(255, 255, 255); // Color white

Then, you can set RGB color to your object with something like that:

g.setColor(myWhite);

Hope it helps you!




回答2:


Or you can do:

setColor(new Color(r, g, b));

For example:

setColor(new Color(0, 0, 0)); //sets the color to Black



回答3:


You can do it with Graphics.setColor

For example:

g.setColor(Color.getHSBColor(255, 79, 18));


来源:https://stackoverflow.com/questions/42855224/how-to-add-rgb-values-into-setcolor-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!