Skip to content

Instantly share code, notes, and snippets.

@tsumnia
Last active July 17, 2021 15:51
Show Gist options
  • Save tsumnia/2c896956dbfa8a501925133a3e6da507 to your computer and use it in GitHub Desktop.
Save tsumnia/2c896956dbfa8a501925133a3e6da507 to your computer and use it in GitHub Desktop.
JavaFX Animations
import javafx.animation.*;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.stage.*;
import javafx.util.*;
public class CircleSpinnyThingies extends Application {
Pane pane = new Pane();
Transition master;
int numCircles = 100;
int width = 1800;
int height = 1000;
int bigSize = 20;
int smallSize = 6;
int offset = 5;
@Override
public void start(Stage stage) throws Exception {
for (int outer = 0; outer <= numCircles; outer++) {
for (int i = 0; i <= numCircles; i++) {
Circle big = new Circle(bigSize);
big.setFill(null);
big.setStroke(Color.BLACK);
big.setOpacity(0.0);
Circle small = new Circle(smallSize, Color.BLACK);
int bigX = i * (bigSize + offset);
int bigY = outer * (bigSize + offset);
int smallX = bigX;
int smallY = bigY - bigSize;
big.setCenterX(bigX);
big.setCenterY(bigY);
small.setCenterX(smallX);
small.setCenterY(smallY);
PathTransition pt = new PathTransition(Duration.millis(3500), big);
pt.setNode(small);
pt.setInterpolator(Interpolator.LINEAR);
pt.setOrientation(PathTransition.OrientationType.ORTHOGONAL_TO_TANGENT);
pt.setCycleCount(Timeline.INDEFINITE);
pt.setDelay(Duration.millis((50 * i) + (50 * outer)));
pt.play(); // Start animation
pane.getChildren().addAll(big, small);
}
}
Scene scene = new Scene(pane, width, height);
stage.setScene(scene);
stage.setMaximized(true);
stage.setTitle("Circle Spinny Thingie");
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
import java.util.ArrayList;
import javafx.animation.*;
import javafx.application.*;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.effect.*;
import javafx.scene.layout.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.stage.*;
import javafx.util.Duration;
public class SpinnyStar extends Application {
Pane pane = new Pane();
int width = 500;
int height = 500;
int cx = width / 2; //origin X
int cy = height / 2; // origin y
double rotate = 0;
int r = 25;
ArrayList<Hexagram> hexagrams = new ArrayList<Hexagram>();
@Override
public void start(Stage stage) throws Exception {
pane.setBackground(new Background(new BackgroundFill(Color.BLACK, CornerRadii.EMPTY, Insets.EMPTY)));
if (hexagrams.size() == 0) {
Hexagram hexagram = new Hexagram(r);
hexagrams.add(hexagram);
}
EventHandler<ActionEvent> eventHandler = e -> {
pane.getChildren().clear();
ArrayList<Hexagram> hexList= (ArrayList<Hexagram>) hexagrams.clone();
double circleSize = 5.0;
for(Hexagram h : hexList) {
Group g = new Group();
h.increaseRadius();
Point2D[] hexPoints = h.getPoints();
for(Point2D point : hexPoints) {
double x = point.getX();
double y = point.getY();
Circle c = new Circle(x, y, circleSize, Color.AQUA);
c.setEffect(new BoxBlur(2, 2, 3));
g.getChildren().add(c);
}
h.increaseRotation();
g.setRotate(h.getRotate());
pane.getChildren().add(g);
//System.out.println(h.getRadius());
if(h.getRadius() == 50) {
//System.out.println("New Hexagram");
Hexagram hexagram = new Hexagram(r);
hexagrams.add(hexagram);
}
if(h.getRadius() > 1000) {
hexagrams.remove(hexagrams.indexOf(h));
}
}
//r++;
};
Timeline animation = new Timeline(
new KeyFrame(Duration.millis(25), eventHandler));
animation.setCycleCount(Timeline.INDEFINITE);
animation.play();
Scene scene = new Scene(pane, width, height);
stage.setScene(scene);
stage.setMaximized(true);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
class Hexagram extends Circle {
Point2D[] points = new Point2D[36];
public Hexagram() {
this(1.0);
}
public void increaseRotation() {
double rotate = super.getRotate() == 360 ? 0 : super.getRotate()-0.4;
super.setRotate(rotate);
//updatePoints();
}
public void increaseRadius() {
double radius = super.getRadius();
super.setRadius(radius+0.5);
updatePoints();
}
private void updatePoints() {
for (int i = 0; i < points.length; i++) {
int angle = (360 / points.length) * i;
double maths = angle % (360 / 6);
maths = Math.abs(30 - maths); // distance from 45 degrees
maths = super.getRadius() - (super.getRadius() * (maths/90) );
double X = cx + maths * Math.sin(Math.toRadians(angle));
double Y = cy + maths * Math.cos(Math.toRadians(angle));
//System.out.printf("Point %2d:\t%.2f,%.2f\n", i, X, Y);
Point2D p = new Point2D(X, Y);
points[i] = p;
}
}
public Hexagram(double radius) {
super(radius);
//System.out.printf("Origin:\t(%2d,%-2d)\n", cx, cy);
for (int i = 0; i < points.length; i++) {
int angle = (360 / points.length) * i;
double maths = angle % (360 / 6);
maths = Math.abs(30 - maths); // distance from 45 degrees
maths = super.getRadius() - (super.getRadius() * (maths/90) );
double X = cx + maths * Math.sin(Math.toRadians(angle));
double Y = cy + maths * Math.cos(Math.toRadians(angle));
//System.out.printf("Point %2d:\t%.2f,%.2f\n", i, X, Y);
Point2D p = new Point2D(X, Y);
points[i] = p;
}
}
public Point2D[] getPoints() {
return points;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment