Make JavaFX wait and continue with code

I am basically trying to make a short effect using JavaFX. I have a heart shape (added from two circles and a polygon) that I can vary in size using the double p value. "Standard size" will be p = 1.0; .

I'm trying to add a beat to my heart. I have a pumpOnce() method:

 public void pumpOnce(){ p = p + 1; initHeart(); //Here goes what ever it takes to make stuff working!! p = p - 1; initHeart(); } 

initHeart() draws a heart based on p .

I found out that Thread.sleep(); or similar methods will not work due to the philosophy of threads in JavaFX.

But what can I use instead?

+5
source share
2 answers

JavaFX animations are probably the way to go, but the "thread philosophy" in JavaFX is not difficult to work if you want to flip your own or do other more complex things in the background thread.

The following code pauses and changes the value in the label (full disclosure, I reuse the code I wrote for another question):

 import javafx.application.Application; import javafx.concurrent.Task; import javafx.concurrent.WorkerStateEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class HelloWorld extends Application { private static Label label; public static void main(String[] args) { launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Hello World!"); label = new Label(); label.setText("Waiting..."); StackPane root = new StackPane(); root.getChildren().add(label); primaryStage.setScene(new Scene(root, 300, 250)); primaryStage.show(); Task<Void> sleeper = new Task<Void>() { @Override protected Void call() throws Exception { try { Thread.sleep(5000); } catch (InterruptedException e) { } return null; } }; sleeper.setOnSucceeded(new EventHandler<WorkerStateEvent>() { @Override public void handle(WorkerStateEvent event) { label.setText("Hello World"); } }); new Thread(sleeper).start(); } } 

The main JavaFX background tool is Task, any JavaFX application that actually does anything is likely to be inundated with this. Learn how to use them.

+10
source

The Dave solution is great for threading in JavaFX.

If you want to use JavaFX animations, the solutions below demonstrate this using a timeline or scaling. The timeline implements a discrete scale for the user interface element, so every quarter of a second the user interface element is scaled more or returned to its original size. A scale transition implements a smooth scale for the UI element, so the user interface element gradually becomes larger than smaller using an interpolated scale factor with a standard attenuation interpolator .

 import javafx.animation.*; import javafx.application.Application; import javafx.beans.property.*; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; import javafx.stage.Stage; import javafx.util.Duration; public class BeatingHeart extends Application { public static void main(String[] args) { launch(args); } public void start(Stage stage) { ImageView heart = new ImageView(HEART_IMAGE_LOC); animateUsingTimeline(heart); // animateUsingScaleTransition(heart); StackPane layout = new StackPane(heart); layout.setPrefWidth(heart.getImage().getWidth() * 2); layout.setPrefHeight(heart.getImage().getHeight() * 2); Scene scene = new Scene(layout); stage.setScene(scene); stage.show(); } private void animateUsingTimeline(ImageView heart) { DoubleProperty scale = new SimpleDoubleProperty(1); heart.scaleXProperty().bind(scale); heart.scaleYProperty().bind(scale); Timeline beat = new Timeline( new KeyFrame(Duration.ZERO, event -> scale.setValue(1)), new KeyFrame(Duration.seconds(0.5), event -> scale.setValue(1.1)) ); beat.setAutoReverse(true); beat.setCycleCount(Timeline.INDEFINITE); beat.play(); } private void animateUsingScaleTransition(ImageView heart) { ScaleTransition scaleTransition = new ScaleTransition( Duration.seconds(1), heart ); scaleTransition.setFromX(1); scaleTransition.setFromY(1); scaleTransition.setFromZ(1); scaleTransition.setToX(1.1); scaleTransition.setToY(1.1); scaleTransition.setToZ(1.1); scaleTransition.setAutoReverse(true); scaleTransition.setCycleCount(Animation.INDEFINITE); scaleTransition.play(); } private static final String HEART_IMAGE_LOC = "http://icons.iconarchive.com/icons/mirella-gabriele/valentine/128/Heart-red-icon.png"; // icon obtained from: http://www.iconarchive.com/show/valentine-icons-by-mirella-gabriele/Heart-red-icon.html // icon license: Free for non-commercial use, commercial use not allowed. } 
+3
source

Source: https://habr.com/ru/post/1205041/


All Articles