package edu.bloomu.ch4a;

import java.util.concurrent.ThreadLocalRandom;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;

/**
 * Draws five randomly colored ovals rotated through different angles.
 *
 * @author Drue Coles
 */
public class RandomOvals extends Application {

    @Override
    public void start(Stage stage) {
        StackPane root = new StackPane();
        final int n = 500;
        Scene scene = new Scene(root, n, n, Color.BLACK);

        // The ovals will have the same width. Each oval's height will be a random 
        // fraction of its width.
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        double min = 0.25;
        double max = 0.75;
        final int gap = n / 30;
        final int width = n - gap;
        root.getChildren().add(randomOval(width, width * rand.nextDouble(min, max)));
        root.getChildren().add(randomOval(width, width * rand.nextDouble(min, max)));
        root.getChildren().add(randomOval(width, width * rand.nextDouble(min, max)));
        root.getChildren().add(randomOval(width, width * rand.nextDouble(min, max)));
        root.getChildren().add(randomOval(width, width * rand.nextDouble(min, max)));

        stage.setTitle("Random Ovals");
        stage.setScene(scene);
        stage.show();
    }

    /**
     * Returns a randomly colored oval rotated through a random angle.
     */
    private static Ellipse randomOval(double width, double height) {

        // random bright opaque color
        ThreadLocalRandom rand = ThreadLocalRandom.current();
        double r = rand.nextDouble();
        double g = rand.nextDouble();
        double b = rand.nextDouble();
        Color color = new Color(r, g, b, 1).brighter().brighter();

        Ellipse e = new Ellipse(width / 2, height / 2);
        e.setStroke(color);
        e.setFill(null);
        e.setStrokeWidth(8);
        e.setRotate(rand.nextInt(360));
        return e;
    }

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