import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.util.Duration;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class HelloOnTop extends Application {

    private Stage createStage(Color sceneColor) {
        Stage stage = new Stage();

        Group root = new Group();
        Scene scene = new Scene(root, 600, 450);
        scene.setFill(sceneColor);

        Rectangle rect = new Rectangle();
        rect.setX(25);
        rect.setY(40);
        rect.setWidth(100);
        rect.setHeight(50);
        rect.setFill(Color.BLACK);

        root.getChildren().add(rect);
        stage.setScene(scene);

        return stage;
    }

    @Override public void start(Stage primaryStage) {
        // Ignore primary stage and create a new one

        // The following fails intermittently on Linux
        Platform.runLater(() -> {
            Stage stage1 = createStage(Color.RED);
            stage1.setAlwaysOnTop(true);
            stage1.setTitle("AlwaysOnTop Stage");
            stage1.show();
            System.err.println("RED stage showing (should stay on top)");
        });

        Platform.runLater(() -> {
            Stage stage2 = createStage(Color.GREEN);
            stage2.setTitle("Normal Stage");
            stage2.show();
            System.err.println("GREEN stage showing (should stay behind)");
        });

        // The following works
/*
        KeyFrame kf1 = new KeyFrame(Duration.millis(10), e -> {
            Stage stage1 = createStage(Color.RED);
            stage1.setAlwaysOnTop(true);
            stage1.setTitle("AlwaysOnTop Stage");
            stage1.show();
            System.err.println("RED stage showing (should stay on top)");
        });
        KeyFrame kf2 = new KeyFrame(Duration.millis(20), e -> {
            Stage stage2 = createStage(Color.GREEN);
            stage2.setTitle("Normal Stage");
            stage2.show();
            System.err.println("GREEN stage showing (should stay behind)");
        });
        Timeline t = new Timeline(kf1, kf2);
        t.play();
*/
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(args);
    }
}
