import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

import static javafx.scene.paint.Color.*;

/**
 * Each cell of a 3x3 grid is filled with a unique color selected at random from a 
 * small list of possible colors.
 *
 * @author Drue Coles
 */
public class ColorfulGrid2 extends Application {

    private static final Color[] colors = {
        DARKSLATEGRAY, DEEPPINK, CRIMSON, GREEN, INDIGO, LIME, MAGENTA, MINTCREAM,
        ORANGE, PLUM, PURPLE, YELLOW
    };

    @Override
    public void start(Stage primaryStage) {

        Pane root = new Pane();
        final int size = 500; // length and width of scene
        Scene scene = new Scene(root, size, size, BLACK);

        final int gap = 5; // separation between adjacent cells
        int cellsize = (size - 4 * gap) / 3; // size of each cell

        List<Color> listOfColors = new ArrayList<>(Arrays.asList(colors));
        Collections.shuffle(listOfColors);

        // draw 3x3 grid of square cells
        final int rows = 3;
        final int cols = 3;
        int indexOfColor = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {

                // top-left corner of cell (i, j)
                int x = gap + j * (gap + cellsize);
                int y = gap + i * (gap + cellsize);
                Rectangle cell = new Rectangle(x, y, cellsize, cellsize);

                cell.setFill(listOfColors.get(indexOfColor));
                indexOfColor++;
                root.getChildren().add(cell);
            }
        }

        primaryStage.setTitle("Colorful Grid");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}