Color.red is not a method, but a constant in the Color file. It looks like this:
public final static Color red = new Color(255, 0, 0);
Accordingly, it is sufficient to simply store the current color in a field of type Color .
Therefore, we add a public method to the class to set the color, store the set color in the field and use it when drawing.
As a result, the class looks like this:
public class Entity { private Color color = Color.green; public void setColor(Color color) { this.color = color; } public void render(Graphics g) { g.setColor(color); } }
And you can use it like this:
Entity entity = new Entity(); entity.setColor(Color.red); entity.render(); Color newColor = new Color(200, 200, 200); entity.setColor(newColor); entity.render();
Using both "standard" colors (for example, Color.red ), and manually created ones (for example, new Color(200, 200, 200) ).
Color.redis ajava.awt.Colorconstant located in the samejava.awt.Colorclass. Nobody forbids you to keep the fieldpublic Color colorand assign it any color. And, of course, writeg.setColor(color);. - Regentpublic Color color;Now I am writingg.setColor(color);And how can I change the color in another class? - Stab Ones