Tuesday 7 November 2017

Bouncing Circles


  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
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
import java.util.Arrays;
import java.util.Random;
import java.util.function.DoubleSupplier;

import javafx.animation.AnimationTimer;
import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    final Color fill = new Color(1, 1, 1, 00.05);
    double width, height;
    Circle[] circles = new Circle[0];
    double[][] moveBy = new double[0][2];
    double[] radiusDivs = new double[0];

    @Override
    public void start(Stage stage) throws Exception {
        Random random = new Random();
        Pane pane = new Pane();

        pane.setStyle("-fx-background-color:derive(black, 30%);");

        Scene scene = new Scene(new BorderPane(pane), 300, 300);
        stage.setScene(scene);
        stage.show();

        DoubleSupplier randomFraction = () -> (random.nextBoolean() ? 1 : -1) * random.nextDouble();

        InvalidationListener resizeListener = e -> {
            double oldW = width;
            double oldH = height;

            width = stage.getWidth();
            height = stage.getHeight();

            double min = Math.min(width, height);
            double max = Math.max(width, height);
            int size = (int) Math.sqrt(max);
            int sizePrev = circles.length;
            circles = sizePrev == size ? circles : Arrays.copyOf(circles, size);
            radiusDivs = sizePrev == size ? radiusDivs : Arrays.copyOf(radiusDivs, size);
            moveBy = sizePrev == size ? moveBy : Arrays.copyOf(moveBy, size);

            if (sizePrev < size) {
                for (int i = sizePrev; i < size; i++) {
                    Circle c = circles[i] = new Circle(5, fill);
                    c.getStyleClass().add("circles");

                    double r = radiusDivs[i] = 0.5 + random.nextDouble();
                    c.setRadius((min / 10) * radiusDivs[i]);
                    c.setCenterX(r + random.nextInt((int) (width - r)));
                    c.setCenterY(r + random.nextInt((int) (height - r)));

                    moveBy[i] = new double[] { randomFraction.getAsDouble(), randomFraction.getAsDouble() };
                }
            }
            int limit = sizePrev > size ? size : sizePrev;
            for (int i = 0; i < limit; i++) {
                Circle c = circles[i];

                c.setRadius((min / 10) * radiusDivs[i]);
                c.setCenterX(width * (c.getCenterX() / oldW));
                c.setCenterY(height * (c.getCenterY() / oldH));
            }
            pane.getChildren().setAll(circles);
        };

        stage.widthProperty().addListener(resizeListener);
        stage.heightProperty().addListener(resizeListener);
        resizeListener.invalidated(null);

        new AnimationTimer() {

            @Override
            public void handle(long now) {
                for (int i = 0; i < circles.length; i++) {
                    Circle c = circles[i];
                    double x = c.getCenterX();
                    double y = c.getCenterY();
                    double r = c.getRadius();

                    if (x - r < 0)
                        moveBy[i][0] = random.nextDouble();
                    else if (x + r > width)
                        moveBy[i][0] = -1 * random.nextDouble();
                    if (y - r < 0)
                        moveBy[i][1] = random.nextDouble();
                    else if (y + r > height)
                        moveBy[i][1] = -1 * random.nextDouble();

                    c.setCenterX(x + moveBy[i][0]);
                    c.setCenterY(y + moveBy[i][1]);
                }
            }
        }.start();
    }
}

styled using hilite.me

1 comment:

  1. Harrah's Casino & Racetrack - Mapyro
    Welcome 춘천 출장샵 to 안양 출장샵 Harrah's 구리 출장샵 Casino & Racetrack. 나주 출장샵 We are not affiliated with any other 세종특별자치 출장샵 entity or brand, or any political subdivision in the state of Nevada.

    ReplyDelete