0

I wish to create an array of all the Colors predefined in java.awt.Color in order to randomly select one of them.

My current best attempt is:

`           Color[] colors = Color.getClass().getEnumConstants();

which was suggested in the top answer to the question: Color Class in Java

but that generates the error:

Cannot make a static reference to the non-static method getClass() from the type Object

The constructor in which the erroneous call is made is below:

private Ball() {
    Random initialSetter = new Random();
    ballX = marginSize + initialSetter.nextInt(xSize - 2 * marginSize);
    ballY = marginSize + initialSetter.nextInt(ySize - 2 * marginSize);
    ballXV = initialSetter.nextInt(doubleMaxV) - doubleMaxV/2;
    ballYV = initialSetter.nextInt(doubleMaxV) - doubleMaxV/2;
    Color[] colors = Color.getClass().getEnumConstants();
    color = colors[initialSetter.nextInt(colors.length)];
}

Replacing ".getClass().getEnumConstants()" with ".values()" generates much the same error (static reference to non-static method).

Surprised
  • 21
  • 5
  • `Color[] colors = Color.values()`. We use `Class.getEnumConstants()` when the `Class` instance is a dynamic value obtained at runtime. – ernest_k Feb 29 '20 at 11:05
  • That suggestion generates the following error for me: `The method values() is undefined for the type Color` – Surprised Feb 29 '20 at 11:13
  • That means `Color` is not an enum. And even `colorObject.getClass().getEnumConstants()` won't return values. – ernest_k Feb 29 '20 at 11:17
  • I agree color is not an enum (so perhaps my question shouldn't be tagged enum). But is there any way to construct an array of all predefined color types other than manually entering each one? – Surprised Feb 29 '20 at 11:20
  • 1
    Please add the definition of your class and indicate how your predefined instances are being created. It's hard to suggest anything without those. – ernest_k Feb 29 '20 at 11:23
  • The desired values are not ones that I have predefined, but those that are predefined in the java.awt.Color class. I have now included the constructor in which I want to make this call in the question. – Surprised Feb 29 '20 at 11:27
  • Just use `new Color[]{Color.WHITE, Color.LIGHT_GRAY, Color.GRAY, Color.DARK_GRAY, Color.BLACK, Color.RED, Color.PINK, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.MAGENTA, Color.CYAN, Color.BLUE}` – vbezhenar Feb 29 '20 at 12:02

1 Answers1

0

To fix your immediate error, you can do:

Color[] colors = Color.class.getEnumConstants();

But this only works if Color is an enum. According to your comments, Color refers to java.awt.Color, which is not an enum. The first way suggested by the linked answer is quite incorrect (Maybe it was 6 years ago?).

As far as I know, the best thing you can do here is to list them all out. There's not much - only 13. It's not like this number is going to change any time soon, as AWT is pretty old, they are unlikely to add new colours in at this stage.

For fun (this is only for fun), you can do it with reflection:

List<Color> colors = Arrays.stream(Color.class.getFields())
        // fields of type Color, and in all caps, snake case
        .filter(x -> x.getType() == Color.class && x.getName().matches("[A-Z_]+"))
        .map(x -> {
            try {
                return (Color)x.get(null);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
                return Color.BLACK;
            }
        }).collect(Collectors.toList());

Note that reflection is really slow though, which is why your best choice is to hardcode it.

Sweeper
  • 213,210
  • 22
  • 193
  • 313