I have an array of colours of size n. In my program, the number of teams is always <= n, and I need to assign each team a unique color. This is my color array:
private static Color[] TEAM_COLORS = {Color.BLUE, Color.RED, Color.CYAN, Color.GREEN, Color.ORANGE, Color.PINK};
When I print information about the players in the console, I want to print what color is associated with them. When I print the color, I get
java.awt.Color[r=...,g=...,b=...].
I understand that this is how Java prints colours. I was wondering if there was a way to instead print BLUE, RED, etc. (so the pre-defined color string).
Advertisement
Answer
Extending @Jon_Skeet reply by adding name also to the enum.
public enum NamedColor {
BLUE(Color.BLUE, "Blue"),
RED(Color.RED, "Red"),
...;
private final Color awtColor;
private final String colorName;
private NamedColor(Color awtColor, String name) {
this.awtColor = awtColor;
this.colorName = name;
}
public Color getAwtColor() {
return awtColor;
}
public String getColorName() {
return colorName;
}
}
NOTE: IF voting this pls vote @Jon_Skeet reply too as it is extension of that…