JavaFX 飞行动画

JavaFX 飞行动画_第1张图片
 

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FlyToPointTest extends Application {

    private String flyToObject = null;

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

    @Override
    public void start(Stage primaryStage) {
        ObservableList options = FXCollections.observableArrayList();
        options.add("红圆");
        options.add("绿圆");
        options.add("蓝圆");

        ComboBox circleChooseComboBox = new ComboBox<>();
        circleChooseComboBox.setItems(options);
        circleChooseComboBox.setTranslateX(10);
        circleChooseComboBox.setTranslateY(0);

        circleChooseComboBox.getSelectionModel().selectedItemProperty().addListener((ObservableValue arg0, String arg1, String arg2) -> {
            if (arg2 != null) {
                flyToObject = arg2;
            }
        });

        Circle circleA = new Circle(50, 50, 5);
        circleA.setFill(Color.RED);

        Circle circleB = new Circle(400, 200, 5);
        circleB.setFill(Color.GREEN);

        Circle circleC = new Circle(300, 300, 5);
        circleC.setFill(Color.BLUE);

        Button button = new Button("飞起来");
        button.setTranslateX(100);
        button.setTranslateY(0);

        Pane pane = new Pane();
        pane.setPrefWidth(800);
        pane.setPrefHeight(400);

        Pane childPane = new Pane();
        childPane.setPrefHeight(350);
        childPane.setPrefWidth(780);
        childPane.setTranslateX(10);
        childPane.setTranslateY(40);
        childPane.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT,null,null)));

        Rectangle rectangle = new Rectangle();
        rectangle.setStroke(Color.GRAY);
        rectangle.setHeight(350);
        rectangle.setWidth(780);
        rectangle.setTranslateX(10);
        rectangle.setTranslateY(40);
        rectangle.setFill(null);

        childPane.getChildren().addAll(circleA, circleB, circleC);

        double middleXInPane = pane.getPrefWidth() / 2;
        double childPaneStartTranslateY = childPane.getTranslateY();
        double middleYInPane = childPaneStartTranslateY + childPane.getPrefHeight() / 2;
        Line horizontalLine = new Line(middleXInPane - 10, middleYInPane, middleXInPane + 10, middleYInPane);
        horizontalLine.setStroke(Color.GRAY);
        Line verticalLine = new Line(middleXInPane, middleYInPane - 10, middleXInPane, middleYInPane + 10);
        verticalLine.setStroke(Color.GRAY);
        pane.getChildren().addAll(horizontalLine, verticalLine, childPane, rectangle, button, circleChooseComboBox);

        /*childPane.setBorder((new Border(new BorderStroke(Color.GRAY,
                new BorderStrokeStyle(StrokeType.OUTSIDE, StrokeLineJoin.ROUND, StrokeLineCap.ROUND, 10, 0, null),
                CornerRadii.EMPTY, new BorderWidths(3)))));*/

        Timeline timeline = new Timeline();
        button.setOnAction(event -> {
            Circle circle = null;
            if(flyToObject == null){
                Alert alert = new Alert(Alert.AlertType.WARNING, "你未选择圆", ButtonType.CLOSE);
                alert.show();
                return;
            }

            switch (flyToObject) {
                case "红圆":
                    circle = circleA;
                    break;
                case "绿圆": {
                    circle = circleB;
                    break;
                }
                case "蓝圆": {
                    circle = circleC;
                    break;
                }
            }

            timeline.getKeyFrames().clear();
            // 圆和中心点的距离
            double distanceX = middleXInPane - (childPane.getTranslateX() + circle.getCenterX());
            double distanceY = middleYInPane - (childPane.getTranslateY() + circle.getCenterY());
            if (circle != null) {
                timeline.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(childPane.translateXProperty(), childPane.getTranslateX())),
                        new KeyFrame(new Duration(1000), new KeyValue(childPane.translateXProperty(), childPane.getTranslateX() + distanceX)));
                timeline.getKeyFrames().addAll(new KeyFrame(new Duration(0), new KeyValue(childPane.translateYProperty(), childPane.getTranslateY())),
                        new KeyFrame(new Duration(1000), new KeyValue(childPane.translateYProperty(), childPane.getTranslateY() + distanceY)));
            }

            timeline.play();
        });

        primaryStage.setResizable(false);
        primaryStage.setScene(new Scene(pane, 800, 400));
        primaryStage.show();
    }
}

 

你可能感兴趣的:(Java,#,JavaFX)