Eric Eric - 1 year ago 66
Java Question

Get Color using integer

The title is a bit confusing but I want to add an integer to a color method. I mean if I had three Colors and want to go through them with a for loop (Something like that below):

Color color1 = something;
Color color2 = something;
Color color3 = something;

for (int i = 1; i < 4; i++) {
int r = color(i).getRed();
int g = color(i).getGreen();
int b = color(i).getBlue();

Answer Source

You can store the three colors in an array and access that array inside your loop.

// take these three colors for example
Color[] colors = {Color.BLACK, Color.WHITE, Color.YELLOW};

for (int i=0; i<3; i++) {
    int r = colors[i].getRed();
    int g = colors[i].getGreen();
    int b = colors[i].getBlue();