将圆更改为正方形以填充JavaFX中的正方形

我发现了一个惊人的动画,其中从圆形/椭圆形过渡到正方形,并且在动画结束之前,正方形会突出一点,然后将其自身调整回正确的大小。

来源:https://clementmihailescu.github.io/Pathfinding-Visualizer/#

我试图重新创建它,但无法获得从墨水滴到正方形的过渡效果。

package sample;

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.event.actionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Lighting;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Text;
import javafx.util.Duration;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception {
    Pane root = new Pane();
    Rectangle originalRectangle = new Rectangle(100,100);
    originalRectangle.setfill(Color.WHITE);
    originalRectangle.setStroke(Color.BLUE);
    Rectangle substituteRectangle = new Rectangle(25,25,50,50);
    substituteRectangle.setOpacity(0.0);
    substituteRectangle.setfill(Color.TURQUOISE);
    substituteRectangle.setStroke(Color.TURQUOISE);
    Rectangle yellowRectangle = new Rectangle(100,100);
    yellowRectangle.setfill(Color.YELLOW);
    root.getchildren().addAll(originalRectangle,substituteRectangle,yellowRectangle);


    FadeTransition fadeOutTransition = new FadeTransition(Duration.millis(3000),yellowRectangle);    //Make the duration as 1ms to get the instant 
    fadeOutTransition.setfromValue(1.0);
    fadeOutTransition.setToValue(0.0);

    FadeTransition fadeInTransition = new FadeTransition(Duration.millis(1),substituteRectangle);
    fadeInTransition.setfromValue(0.0);
    fadeInTransition.setToValue(0.8);

//To make the square protrute out a bit
    ScaleTransition scaleTransition = new ScaleTransition(Duration.seconds(4),substituteRectangle);
    scaleTransition.setToX(2.1);
    scaleTransition.setToY(2.1);

    ParallelTransition parallelTransition = new ParallelTransition(scaleTransition,fadeInTransition);

    fadeOutTransition.play();
    fadeOutTransition.setOnFinished(e -> {
        parallelTransition.setOnFinished(event -> {
            root.getchildren().removeAll(substituteRectangle,yellowRectangle);
            originalRectangle.setOpacity(1.0);
            originalRectangle.setfill(Color.RED);
        });
        parallelTransition.play();
    });
    Scene scene = new Scene(root,800,600);
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    Application.launch(args);
}
}
zz7630 回答:将圆更改为正方形以填充JavaFX中的正方形

实现这种动画的最简单的方法是使用this.$parent.$emit()作为剪辑简单地设置圆的半径并限制其大小。通常Rectangle提供的动画非常适合替换多个过渡。在这种情况下,它既可以在颜色之间进行动画处理,也可以用于Timeline的动画处理,这使它成为简化动画的不错选择:

DoubleProperty
本文链接:https://www.f2er.com/2549913.html

大家都在问