Greetings How to make a javaFX move a rectangle?

Here is the code: main.java

public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception{ Parent root = FXMLLoader.load(getClass().getResource("sample.fxml")); primaryStage.setTitle("Hello World"); primaryStage.setScene(new Scene(root)); primaryStage.show(); } public static void main(String[] args) { launch(args); } } 

controller.java

 public class Controller { @FXML Rectangle tube = new Rectangle(); public void onClick() throws InterruptedException { int x = 0; while(x < 100){ Double a = tube.getX(); Double b = tube.getY(); tube.setX(a-5); x++; Thread.sleep(10); } } } 

Sample form:

enter image description here

It is necessary that this rectangle rode to the end of the line to the left. How to do it?

UPD1 ::

 public void onClick() throws InterruptedException { takeFrom(); // ΠšΠΎΠ½Π΅Ρ‡Π½Π°Ρ позиция: double stopPositionX = 24; KeyFrame keyFrame = new KeyFrame( // Π‘Ρ‡ΠΈΡ‚Π°Π΅ΠΌ, сколько Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Π±ΡƒΠ΄Π΅Ρ‚ Π΄Π»ΠΈΡ‚ΡŒΡΡ анимация: Duration.millis((tube.getLayoutX() - stopPositionX) / 5 * 10), event -> { System.out.println("ΠŸΠ΅Ρ€Π΅ΠΌΠ΅Ρ‰Π΅Π½ΠΈΠ΅ Ρ‚Ρ€ΡƒΠ±Ρ‹ ΠΏΠΎ отводящСму Ρ€ΠΎΠ»ΡŒΠ³Π°Π½Π³Ρƒ ΠΎΠΊΠΎΠ½Ρ‡Π΅Π½ΠΎ"); }, // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ, ΠΊΠ°ΠΊΠΎΠ΅ свойство Π°Π½ΠΈΠΌΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ, Π΅Π³ΠΎ ΠΊΠΎΠ½Π΅Ρ‡Π½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΈ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ: new KeyValue(tube.layoutXProperty(), stopPositionX, Interpolator.LINEAR) ); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΈ запускаСм ΠΎΠ΄Π½ΠΎΡ€Π°Π·ΠΎΠ²Ρ‹ΠΉ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ для Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΠΈ ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Π΅Π²ΠΎΠΌΡƒ ΠΊΠ°Π΄Ρ€Ρƒ: Timeline timeline = new Timeline(); timeline.getKeyFrames().add(keyFrame); timeline.setCycleCount(1); timeline.play(); } public void takeFrom(){ // ΠšΠΎΠ½Π΅Ρ‡Π½Π°Ρ позиция: double stopPositionY = 216; KeyFrame keyFrame = new KeyFrame( // Π‘Ρ‡ΠΈΡ‚Π°Π΅ΠΌ, сколько Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Π±ΡƒΠ΄Π΅Ρ‚ Π΄Π»ΠΈΡ‚ΡŒΡΡ анимация: Duration.millis((tube.getLayoutY() - stopPositionY) / 2 * 10), event -> { System.out.println("Π”Π²ΠΈΠΆΠ΅Π½ΠΈΠ΅ Π·Π°ΠΊΠΎΠ½Ρ‡Π΅Π½ΠΎ"); }, // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ, ΠΊΠ°ΠΊΠΎΠ΅ свойство Π°Π½ΠΈΠΌΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ, Π΅Π³ΠΎ ΠΊΠΎΠ½Π΅Ρ‡Π½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΈ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ: new KeyValue(tube.layoutYProperty(), stopPositionY, Interpolator.LINEAR) ); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΈ запускаСм ΠΎΠ΄Π½ΠΎΡ€Π°Π·ΠΎΠ²Ρ‹ΠΉ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ для Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΠΈ ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Π΅Π²ΠΎΠΌΡƒ ΠΊΠ°Π΄Ρ€Ρƒ: Timeline timeline = new Timeline(); timeline.getKeyFrames().add(keyFrame); timeline.setCycleCount(1); timeline.play(); } 
  • Remember to use Math.abs() for time - otherwise you will get an exception if the result is negative (in the example of sub-rules). - oshatrk

1 answer 1

Option:

 public void onClick() { // ΠšΠΎΠ½Π΅Ρ‡Π½Π°Ρ позиция: double stopPositionX = 100; KeyFrame keyFrame = new KeyFrame( // Π‘Ρ‡ΠΈΡ‚Π°Π΅ΠΌ, сколько Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ Π±ΡƒΠ΄Π΅Ρ‚ Π΄Π»ΠΈΡ‚ΡŒΡΡ анимация: Duration.millis(Math.abs((tube.getLayoutX() - stopPositionX) / 5 * 10)), event -> { System.out.println("Анимация Π·Π°ΠΊΠΎΠ½Ρ‡Π΅Π½Π°"); }, // Π£ΠΊΠ°Π·Ρ‹Π²Π°Π΅ΠΌ, ΠΊΠ°ΠΊΠΎΠ΅ свойство Π°Π½ΠΈΠΌΠΈΡ€ΠΎΠ²Π°Ρ‚ΡŒ, Π΅Π³ΠΎ ΠΊΠΎΠ½Π΅Ρ‡Π½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΈ Π°Π»Π³ΠΎΡ€ΠΈΡ‚ΠΌ: new KeyValue(tube.layoutXProperty(), stopPositionX, Interpolator.LINEAR) ); // Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΈ запускаСм ΠΎΠ΄Π½ΠΎΡ€Π°Π·ΠΎΠ²Ρ‹ΠΉ Ρ‚Π°ΠΉΠΌΠ΅Ρ€ для Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΠΈ ΠΏΠΎ ΠΊΠ»ΡŽΡ‡Π΅Π²ΠΎΠΌΡƒ ΠΊΠ°Π΄Ρ€Ρƒ: Timeline timeline = new Timeline(); timeline.getKeyFrames().add(keyFrame); timeline.setCycleCount(1); timeline.play(); } 

The JavaFX stream constantly (60 times per second) checks whether it has job-timers and executes them.

And, of course, this only works if no one has blocked the flow of JavaFX using Thread.sleep() ...

Link to article: Creating Transitions and Timeline Animation in JavaFX: 1 Animation Basics


Addition to the comment:

For sequential execution of animations, use SequentialTransition , for example:

  KeyFrame keyFrame1 = new KeyFrame(...); Timeline timeline1 = new Timeline(keyFrame1); KeyFrame keyFrame2 = new KeyFrame(...); Timeline timeline2 = new Timeline(keyFrame2); SequentialTransition sequence = new SequentialTransition(timeline1, timeline2); sequence.play(); 
  • Works, please see the update? I need to first move the figure along the Y axis and then along the X axis - Mishustiq
  • that is, I need to perform one timer first and then another - Mishustiq