Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

rgb

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.

like image 724
Austin Grant Avatar asked Mar 17 '17 10:03

Austin Grant


Video Answer


2 Answers

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!

like image 88
Loic P. Avatar answered Oct 13 '22 06:10

Loic P.


Or you can do:

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

For example:

setColor(new Color(0, 0, 0)); //sets the color to Black
like image 38
Atomiz Avatar answered Oct 13 '22 07:10

Atomiz