Setting Color Using Int Variables

In summary, to set the color using RGB values, you will need to use the Color constructor with three float arguments: red, green, and blue. This constructor can be found in the API documentation for the Color class.
  • #1
Nick M
193
0
I'm trying to set the color using three integer values for R, G, and B stored in three int type variables (these values are randomly generated between 0 and 255).

Previously we set the color (e.g. yellow) using g.setColor(Color.YELLOW).

I'm unsure of how to do this. The course is online, and I've read the text section multiple times.
 
Physics news on Phys.org
  • #2
Look at the API documentation for whatever class is the object g belongs to. There will be a number of overloaded methods, call called setColor, but which accept different arguments. One of them may accept a Color object, and that should prompt you to look at the documentation of the Color class. You will find that you can construct a Color object with three arguments, red, green, and blue.

- Warren
 
  • #3
Hmmm... I've looked around the API and can't seem to find the code needed to set the color using RGB values.

Knew I shouldn't have taken an online class... :mad:
 
  • #4
http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Color.html

You are looking for this constructor:
Color(float r, float g, float b)
 
Last edited by a moderator:

FAQ: Setting Color Using Int Variables

How can I set color using int variables?

To set color using int variables, you can assign a value between 0 and 255 to each of the red, green, and blue channels. This is known as the RGB color model.

Why do we use int variables to set color?

Int variables are used to set color because they allow for a wide range of values, which translates to a larger range of colors. By using int variables, we can create a more precise and diverse range of colors.

Can I use other types of variables to set color?

Yes, you can use other types of variables such as floats or doubles to set color. However, int variables are commonly used because they offer a good balance between precision and efficiency.

How do I convert int values to color codes?

To convert int values to color codes, you can use a color picker tool or a color conversion formula. There are also many online resources and tools available that can assist with this process.

Is it possible to create custom colors using int variables?

Yes, it is possible to create custom colors using int variables. By adjusting the values of the red, green, and blue channels, you can create a unique color that is not part of the standard color palette. This allows for a more personalized and customizable approach to color selection.

Similar threads

Replies
17
Views
2K
Replies
3
Views
910
Replies
7
Views
2K
Replies
2
Views
3K
Replies
1
Views
5K
Replies
16
Views
13K
Replies
6
Views
3K
Replies
2
Views
4K
Back
Top