转换翻译JavaFX行

时间:2019-07-29 13:12:17

标签: javafx line translation

所以我有一个显示的行,现在我想通过动画将其移动到新位置。 TranslateTransition仅使用一对X和Y坐标,而一条Line具有两组坐标,一组用于Line的起点,另一组用于Line的终点。反正有为行指定TranslateTransition吗?

我知道绑定线(我不容易弄清楚如何在我的情况下应用),但是我可以将通用的TranslateTransition解决方案应用于线(我知道新的起点和终点坐标,希望线移动到..,所以如果可能的话,这可能是最简单的方法...)

1 个答案:

答案 0 :(得分:0)

此示例显示了如何通过TranslateTransition使用Line

import javafx.animation.ParallelTransition;
import javafx.animation.Timeline;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 *
 * @author blj0011
 */
public class JavaFXTestingGround extends Application{

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {

        Line line = new Line(10, 10, 10, 40);        
        TranslateTransition translateTransition = new TranslateTransition(Duration.seconds(6),line);
        translateTransition.setFromX(10);
        translateTransition.setToX(380);
        translateTransition.setCycleCount(Timeline.INDEFINITE);
        translateTransition.setAutoReverse(true);        

        Line line1 = new Line(10, 60, 10, 90);
        Line line2 = new Line(10, 60, 40, 60);
        Line line3 = new Line(40, 60, 40, 90);
        Line line4 = new Line(10, 90, 40, 90);
        Group group = new Group(line1, line2, line3, line4);
        TranslateTransition translateTransition2 = new TranslateTransition(Duration.seconds(6),group);
        translateTransition2.setFromX(10);
        translateTransition2.setToX(380);
        translateTransition2.setCycleCount(Timeline.INDEFINITE);
        translateTransition2.setAutoReverse(true);        

        ParallelTransition parallelTransition = new ParallelTransition(translateTransition, translateTransition2);
        parallelTransition.play();

        Pane root = new Pane(line, group);
        root.setPrefSize(500, 500);


        // Show the stage

        primaryStage.setScene(new Scene(root));
        primaryStage.setTitle("TableColumnGrow Sample");
        primaryStage.show();


    }
}

更新的代码:我在示例中添加了Group