FXML tutorial (23) – animacja kształtu w FXML
listing23_animation.fxml
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.layout.StackPane?> <?import javafx.scene.shape.Circle?> <StackPane xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="codes.Listing23_Controller"> <Circle fx:id="circle" centerX="150" centerY="150" fill="deepskyblue"/> </StackPane>
Listing23_Controller.java
package codes; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.shape.Circle; import javafx.util.Duration; import java.net.URL; import java.util.ResourceBundle; public class Listing23_Controller implements Initializable { @FXML private Circle circle ; @Override public void initialize(URL location, ResourceBundle resources) { KeyValue kv1 = new KeyValue(circle.radiusProperty(), 10); KeyFrame kf1 = new KeyFrame(Duration.millis(0), kv1); //- KeyValue kv2 = new KeyValue(circle.radiusProperty(), 80); KeyFrame kf2 = new KeyFrame(Duration.millis(1500), kv2); //- KeyValue kv3 = new KeyValue(circle.radiusProperty(), 10); KeyFrame kf3 = new KeyFrame(Duration.millis(3000), kv3); //- KeyValue kv4 = new KeyValue(circle.radiusProperty(), 0); KeyFrame kf4 = new KeyFrame(Duration.millis(4500), kv4); //- Timeline timeline = new Timeline(); timeline.getKeyFrames().addAll(kf1, kf2, kf3, kf4); timeline.setCycleCount(1); timeline.play(); } }
Listing23_animation
package codes; import javafx.application.Application; import javafx.fxml.FXMLLoader; import javafx.scene.Scene; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import java.net.URL; public class Listing23_animation extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage stage) { try { URL fxmlUrl = this.getClass().getClassLoader() .getResource("resources/listing23_animation.fxml"); StackPane root = fxmlUrl != null ? FXMLLoader.load(fxmlUrl) : new StackPane(); Scene scene = new Scene(root, 300, 300); stage.setScene(scene); stage.setTitle("Szachownica"); stage.show(); } catch (Exception e) { e.printStackTrace(); } } }
Po uruchomieniu klasy zobaczymy animację elementu Circle
.