Javafx simple PathTransition animation

I use JavaFX to create a card game, and I try to add simple animations.

I have an HBox with several ImageViews in them. Each image has a right edge of -80 so that the images match each other.

Now I want to animate the map when it is added. I want to place it somewhere on the screen (the hand of an opponent player) and move it to a position that would be without animation.

I tried this:

// Animation
Path path = new Path();
MoveTo moveTo = new MoveTo(200, 200);
moveTo.setAbsolute(true);
path.getElements().add(moveTo);
LineTo lineTo = new LineTo(0, 0);
path.getElements().add(lineTo);
PathTransition pathTransition = new PathTransition();
pathTransition.setDuration(Duration.millis(4000));
pathTransition.setPath(path);
pathTransition.setNode(imageView);
pathTransition.setCycleCount(1);
pathTransition.play();

But it does not work as I want. I do not know how to handle the coordinators and cannot find a solution to my problem.

I hope someone can help me with my problem

+1
source share
1 answer

Path Parent. Path , , Rectangle . Pane, " , ". , . StackPane, :

StackPane root = new StackPane();
root.setAlignment(Pos.TOP_LEFT)

image

import javafx.animation.PathTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.LineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.util.Duration;

/** @see http://stackoverflow.com/a/31443755/230513 */
public class AnimationTest extends Application {

    private static final int SIZE = 256;
    private static final int RECT = SIZE / 4;
    private static final int R2 = RECT / 2;

    @Override
    public void start(Stage stage) {
        stage.setTitle("Animation Test");
        Rectangle rect = new Rectangle(0, 0, RECT, RECT);
        rect.setArcHeight(16);
        rect.setArcWidth(16);
        rect.setFill(Color.GOLD);
        Pane root = new Pane();
        root.getChildren().add(rect);
        Scene scene = new Scene(root, SIZE, SIZE);
        stage.setScene(scene);
        stage.show();

        Path path = new Path();
        path.getElements().add(new MoveTo(R2, R2));
        path.getElements().add(new LineTo(SIZE - R2, SIZE - R2));
        PathTransition pathTransition = new PathTransition();
        pathTransition.setDuration(Duration.millis(1000));
        pathTransition.setPath(path);
        pathTransition.setNode(rect);
        pathTransition.setCycleCount(Timeline.INDEFINITE);
        pathTransition.setAutoReverse(true);
        pathTransition.play();
    }

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

}
+1

All Articles