LoginSignup
0
1

More than 5 years have passed since last update.

JavaFXでアニメーション作製(力技)

Posted at

ソースコード

いきなりソースコードを投下します。


import javafx.animation.FadeTransition;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class NodeCalc extends Application {
    public void start(Stage stage) {
        final Group root = new Group();
        Scene scene = new Scene(root, 650, 700);
        stage.setScene(scene);
        stage.show();
        int x1 = 30, x2 = 100, x3 = 200, x4 = 300, x5 = 450, x6 = 200, x7 = 250, x8 = 500, x9 = 550, x10 = 340,
                x11 = 360, x12 = 100, x13 = 375;
        int y1 = 500, y2 = 200, y3 = 40, y4 = 300, y5 = 200, y6 = 550, y7 = 600, y8 = 500, y9 = 600, y10 = 400,
                y11 = 200, y12 = 400, y13 = 500;
        Circle c1 = new Circle(x1, y1, 20, Color.BLACK);
        Circle c2 = new Circle(x2, y2, 20, Color.BLACK);
        Circle c3 = new Circle(x3, y3, 20, Color.BLACK);
        Circle c4 = new Circle(x4, y4, 20, Color.BLACK);
        Circle c5 = new Circle(x5, y5, 20, Color.BLACK);
        Circle c6 = new Circle(x6, y6, 20, Color.BLACK);
        Circle c7 = new Circle(x7, y7, 20, Color.BLACK);
        Circle c8 = new Circle(x8, y8, 20, Color.BLACK);
        Circle c9 = new Circle(x9, y9, 20, Color.BLACK);
        Circle c10 = new Circle(x10, y10, 20, Color.BLACK);
        Circle c11 = new Circle(x11, y11, 20, Color.BLACK);
        Circle c12 = new Circle(x12, y12, 20, Color.BLACK);
        Circle c13 = new Circle(x13, y13, 20, Color.BLACK);
        Line l1 = new Line(x1, y1, x2, y2);
        Line l2 = new Line(x3, y3, x2, y2);
        Line l3 = new Line(x3, y3, x4, y4);
        Line l4 = new Line(x4, y4, x10, y10);
        Line l5 = new Line(x5, y5, x10, y10);
        Line l6 = new Line(x6, y6, x7, y7);
        Line l7 = new Line(x7, y7, x13, y13);
        Line l8 = new Line(x8, y8, x13, y13);
        Line l9 = new Line(x9, y9, x13, y13);
        Line l10 = new Line(x10, y10, x13, y13);
        Line l11 = new Line(x11, y11, x4, y4);
        Line l12 = new Line(x12, y12, x4, y4);
        // なぜかrootにうわがかれる
        // Group vanish1 = new Group();
        // vanish1.getChildren().addAll(c5,c6,c8,c9,c11,c12,l1,l5,l6,l8,l9,l11,l12);
        // Group vanish2 = new Group();
        // vanish2.getChildren().addAll(c2,c7,l2,l7);
        root.getChildren().addAll(c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, l1, l2, l3, l4, l5, l6, l7,
                l8, l9, l10, l11, l12);
        FadeTransition fadein = new FadeTransition(new Duration(4000));
        fadein.setNode(root);
        fadein.setToValue(1.0);
        Timeline timeline = new Timeline();
        timeline.setAutoReverse(true);
        timeline.setCycleCount(10);
        // 初期状態
        KeyFrame key1 = new KeyFrame(new Duration(0), new KeyValue(root.opacityProperty(), 1.0));
        // 2s後完全消滅
        KeyFrame key2 = new KeyFrame(new Duration(2000), new KeyValue(c1.opacityProperty(), 0));
        KeyFrame key3 = new KeyFrame(new Duration(2000), new KeyValue(l1.opacityProperty(), 0));
        KeyFrame key4 = new KeyFrame(new Duration(2000), new KeyValue(c5.opacityProperty(), 0));
        KeyFrame key5 = new KeyFrame(new Duration(2000), new KeyValue(c6.opacityProperty(), 0));
        KeyFrame key6 = new KeyFrame(new Duration(2000), new KeyValue(c8.opacityProperty(), 0));
        KeyFrame key7 = new KeyFrame(new Duration(2000), new KeyValue(c9.opacityProperty(), 0));
        KeyFrame key8 = new KeyFrame(new Duration(2000), new KeyValue(c11.opacityProperty(), 0));
        KeyFrame key9 = new KeyFrame(new Duration(2000), new KeyValue(c12.opacityProperty(), 0));
        KeyFrame key10 = new KeyFrame(new Duration(2000), new KeyValue(l1.opacityProperty(), 0));
        KeyFrame key11 = new KeyFrame(new Duration(2000), new KeyValue(l5.opacityProperty(), 0));
        KeyFrame key12 = new KeyFrame(new Duration(2000), new KeyValue(l6.opacityProperty(), 0));
        KeyFrame key13 = new KeyFrame(new Duration(2000), new KeyValue(l8.opacityProperty(), 0));
        KeyFrame key14 = new KeyFrame(new Duration(2000), new KeyValue(l9.opacityProperty(), 0));
        KeyFrame key15 = new KeyFrame(new Duration(2000), new KeyValue(l11.opacityProperty(), 0));
        KeyFrame key16 = new KeyFrame(new Duration(2000), new KeyValue(l12.opacityProperty(), 0));
        // 6s後完全消滅
        KeyFrame key17 = new KeyFrame(new Duration(6000), new KeyValue(c2.opacityProperty(), 0));
        KeyFrame key18 = new KeyFrame(new Duration(6000), new KeyValue(c7.opacityProperty(), 0));
        KeyFrame key19 = new KeyFrame(new Duration(6000), new KeyValue(l2.opacityProperty(), 0));
        KeyFrame key20 = new KeyFrame(new Duration(6000), new KeyValue(l7.opacityProperty(), 0));
        // 10s後完全消滅
        KeyFrame key21 = new KeyFrame(new Duration(10000), new KeyValue(c3.opacityProperty(), 0));
        KeyFrame key22 = new KeyFrame(new Duration(10000), new KeyValue(c13.opacityProperty(), 0));
        KeyFrame key23 = new KeyFrame(new Duration(10000), new KeyValue(l3.opacityProperty(), 0));
        KeyFrame key24 = new KeyFrame(new Duration(10000), new KeyValue(l10.opacityProperty(), 0));
        timeline.getKeyFrames().add(key1);
        timeline.getKeyFrames().add(key2);
        timeline.getKeyFrames().add(key3);
        timeline.getKeyFrames().add(key3);
        timeline.getKeyFrames().add(key4);
        timeline.getKeyFrames().add(key5);
        timeline.getKeyFrames().add(key6);
        timeline.getKeyFrames().add(key7);
        timeline.getKeyFrames().add(key8);
        timeline.getKeyFrames().add(key9);
        timeline.getKeyFrames().add(key10);
        timeline.getKeyFrames().add(key11);
        timeline.getKeyFrames().add(key12);
        timeline.getKeyFrames().add(key13);
        timeline.getKeyFrames().add(key14);
        timeline.getKeyFrames().add(key15);
        timeline.getKeyFrames().add(key16);
        timeline.getKeyFrames().add(key17);
        timeline.getKeyFrames().add(key18);
        timeline.getKeyFrames().add(key19);
        timeline.getKeyFrames().add(key20);
        timeline.getKeyFrames().add(key21);
        timeline.getKeyFrames().add(key22);
        timeline.getKeyFrames().add(key23);
        timeline.getKeyFrames().add(key24);
        timeline.play();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

これを実行すると、直線でつながれたノードが表示されて、外側から消えていく様子を見ることができます。
実行して出てきた画面を保存する術を知らないので、ここで張り付けることはかないませんでした。
また、中盤でコメントアウトしている、「vanish1」「vanish2」はなぜかその後設定した「root」にうわがかれてしまいました。課題です。

書いている途中で思ったのですが、「key1」「key2」・・・「key24」をtimelineにaddするための繰り返し処理(for文的なやつ)てどうやって書くんでしょう。。。。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1