Reputation: 1092
In Java I have declared this color object:
Color f_color = new Color(0,0,0);
Later in the code I would like to update that color variable setting, for instance, the red channel to 2. I try to do so in the following way:
f_color.r = 2;
Obviously I get an error. Is it possible to do what I am trying to ? or the only way is to create a new color object? Thank you.
Upvotes: 2
Views: 4584
Reputation: 3017
Color
is immutable. You'll have to create a new one:
Color newColor = new Color(oldColor.getRed()+2, oldColor.getGreen(), oldColor.getBlue());
Upvotes: 2
Reputation: 35491
Yes, the only way would be to create a new Color
object, at least to my knowledge.
There are no setter() methods in the Color
class.
However, you can store your colors in variables and then create a new color with updated values:
int r = 0;
int g = 0;
int b = 0;
Color f_color = new Color(r,g,b);
// ...
r = 2; // update red channel
f_color = new Color(r,g,b); // assign new color
Upvotes: 2