Skip to content

Instantly share code, notes, and snippets.

@skrb
Last active December 29, 2015 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrb/7729009 to your computer and use it in GitHub Desktop.
Save skrb/7729009 to your computer and use it in GitHub Desktop.
import javafx.animation.AnimationTimer;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.CubicCurve;
import javafx.scene.shape.StrokeLineCap;
import javafx.stage.Stage;
public class ControlPointsAnimation extends Application {
@Override
public void start(Stage stage) {
Group root = new Group();
drawAnimatedCurves(root);
Scene scene = new Scene(root, 400, 200);
stage.setTitle("Line Animation");
stage.setScene(scene);
stage.show();
}
private void drawAnimatedCurves(Group parent) {
// ベジェ曲線 1本目
final CubicCurve curve1 = new CubicCurve();
curve1.setStartX(40.0); curve1.setStartY(100.0);
curve1.setEndX(360.0); curve1.setEndY(100.0);
curve1.setStroke(Color.BLACK);
curve1.setStrokeWidth(10.0);
curve1.setStrokeLineCap(StrokeLineCap.ROUND);
curve1.setFill(null);
parent.getChildren().add(curve1);
// ベジェ曲線 2本目
final CubicCurve curve2 = new CubicCurve();
curve2.setStartX(40.0); curve2.setStartY(100.0);
curve2.setEndX(360.0); curve2.setEndY(100.0);
curve2.setStroke(Color.BLACK);
curve2.setStrokeWidth(10.0);
curve2.setStrokeLineCap(StrokeLineCap.ROUND);
curve2.setFill(null);
parent.getChildren().add(curve2);
// ベジェ曲線の制御点を円運動させるアニメーション
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long t) {
double sin1 = 150 * Math.sin(t/400_000_000.0);
double cos1 = 150 * Math.cos(t/400_000_000.0);
// もう一方の制御点は180度ずらす
double sin2 = 150 * Math.sin(t/400_000_000.0 + Math.PI);
double cos2 = 150 * Math.cos(t/400_000_000.0 + Math.PI);
double controlX11 = 40.0 + sin1;
double controlY11 = 100.0 + cos1;
curve1.setControlX1(controlX11);
curve1.setControlY1(controlY11);
double controlX12 = 360.0 + sin2;
double controlY12 = 100.0 + cos2;
curve1.setControlX2(controlX12);
curve1.setControlY2(controlY12);
double controlX21 = 40.0 + sin2;
double controlY21 = 100.0 + cos2;
curve2.setControlX1(controlX21);
curve2.setControlY1(controlY21);
double controlX22 = 360.0 + sin1;
double controlY22 = 100.0 + cos1;
curve2.setControlX2(controlX22);
curve2.setControlY2(controlY22);
}
};
timer.start();
}
public static void main(String... args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment