Thursday 9 November 2017

Floating Boxes




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Random;

import javafx.animation.Animation;
import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.RotateTransition;
import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FloatingBoxes extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    final Random random = new Random();
    final int orbitalCounts = 15;

    @Override
    public void start(Stage stage) throws Exception {
        Group root = new Group();

        for (int i = 0; i < 25; i++)
            unit(root);

        CheckBox cb = new CheckBox("show path");
        root.getChildren().add(0, cb);
        cb.setOnAction(e -> {
            Color color = cb.isSelected() ? Color.BLACK : Color.TRANSPARENT;
            root.getChildren().stream()
            .filter(n -> n instanceof Circle)
            .forEach(c -> ((Circle) c).setStroke(color));
        });

        Scene scene = new Scene(root, 500, 500, true);
        scene.setCamera(new PerspectiveCamera(false));
        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    private void unit(Group root) {
        Circle c = new Circle(250, 250, 100 + random.nextInt(150), null);
        c.setStroke(Color.BLACK);
        c.setRotate(random.nextInt(360));
        c.setRotationAxis(new Point3D(random.nextInt(360), random.nextInt(360), random.nextInt(360)));
        c.setStroke(Color.TRANSPARENT);
        c.setStrokeWidth(0.2);

        int size = 10 + random.nextInt(10);
        Box box = new Box(size, size, size);
        box.setMaterial(new PhongMaterial(Color.hsb(random.nextInt(360), 0.5, 1)));
        box.setRotate(random.nextInt(360));
        box.setRotationAxis(new Point3D(random.nextInt(360), random.nextInt(360), random.nextInt(360)));

        PathTransition pt = new PathTransition(Duration.seconds(5), c, box);
        RotateTransition rt = new RotateTransition(Duration.seconds(2), box);
        rt.setToAngle(360 + box.getRotate());

        pt.setDelay(Duration.millis(random.nextInt(1000)));
        pt.setCycleCount(Animation.INDEFINITE);
        rt.setCycleCount(Animation.INDEFINITE);
        pt.setInterpolator(Interpolator.LINEAR);
        rt.setInterpolator(Interpolator.LINEAR);
        rt.play();
        pt.play();

        root.getChildren().addAll(box, c);
    }
}
styled using hilite.me

No comments:

Post a Comment