Make JavaFX wait and continue with code












7















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



I am trying to add a pumping effect to the heart. I have the method pumpOnce():



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


initHeart() draws the heart based on p.



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



But what can I use instead?










share|improve this question























  • You can use PauseTransition or Timeline with KeyFrames to build more complex/custom animation transitions.

    – Uluk Biy
    Oct 19 '14 at 19:00











  • Yeah I did see an answer of yours somewhere around Uluk, yet I am not really trying to do a transition, but basically a two frame animation. Can I use Timeline similar to Thread.sleep()?

    – Maverick283
    Oct 19 '14 at 19:04






  • 1





    A PauseTransition doesn't have to be associated with a node. You can just create one, use its setOnFinished handler for the code to execute after the pause, and call play() to start it.

    – James_D
    Oct 19 '14 at 19:43
















7















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



I am trying to add a pumping effect to the heart. I have the method pumpOnce():



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


initHeart() draws the heart based on p.



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



But what can I use instead?










share|improve this question























  • You can use PauseTransition or Timeline with KeyFrames to build more complex/custom animation transitions.

    – Uluk Biy
    Oct 19 '14 at 19:00











  • Yeah I did see an answer of yours somewhere around Uluk, yet I am not really trying to do a transition, but basically a two frame animation. Can I use Timeline similar to Thread.sleep()?

    – Maverick283
    Oct 19 '14 at 19:04






  • 1





    A PauseTransition doesn't have to be associated with a node. You can just create one, use its setOnFinished handler for the code to execute after the pause, and call play() to start it.

    – James_D
    Oct 19 '14 at 19:43














7












7








7


4






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



I am trying to add a pumping effect to the heart. I have the method pumpOnce():



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


initHeart() draws the heart based on p.



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



But what can I use instead?










share|improve this question














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



I am trying to add a pumping effect to the heart. I have the method pumpOnce():



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


initHeart() draws the heart based on p.



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



But what can I use instead?







javafx wait thread-sleep






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Oct 19 '14 at 18:55









Maverick283Maverick283

4801622




4801622













  • You can use PauseTransition or Timeline with KeyFrames to build more complex/custom animation transitions.

    – Uluk Biy
    Oct 19 '14 at 19:00











  • Yeah I did see an answer of yours somewhere around Uluk, yet I am not really trying to do a transition, but basically a two frame animation. Can I use Timeline similar to Thread.sleep()?

    – Maverick283
    Oct 19 '14 at 19:04






  • 1





    A PauseTransition doesn't have to be associated with a node. You can just create one, use its setOnFinished handler for the code to execute after the pause, and call play() to start it.

    – James_D
    Oct 19 '14 at 19:43



















  • You can use PauseTransition or Timeline with KeyFrames to build more complex/custom animation transitions.

    – Uluk Biy
    Oct 19 '14 at 19:00











  • Yeah I did see an answer of yours somewhere around Uluk, yet I am not really trying to do a transition, but basically a two frame animation. Can I use Timeline similar to Thread.sleep()?

    – Maverick283
    Oct 19 '14 at 19:04






  • 1





    A PauseTransition doesn't have to be associated with a node. You can just create one, use its setOnFinished handler for the code to execute after the pause, and call play() to start it.

    – James_D
    Oct 19 '14 at 19:43

















You can use PauseTransition or Timeline with KeyFrames to build more complex/custom animation transitions.

– Uluk Biy
Oct 19 '14 at 19:00





You can use PauseTransition or Timeline with KeyFrames to build more complex/custom animation transitions.

– Uluk Biy
Oct 19 '14 at 19:00













Yeah I did see an answer of yours somewhere around Uluk, yet I am not really trying to do a transition, but basically a two frame animation. Can I use Timeline similar to Thread.sleep()?

– Maverick283
Oct 19 '14 at 19:04





Yeah I did see an answer of yours somewhere around Uluk, yet I am not really trying to do a transition, but basically a two frame animation. Can I use Timeline similar to Thread.sleep()?

– Maverick283
Oct 19 '14 at 19:04




1




1





A PauseTransition doesn't have to be associated with a node. You can just create one, use its setOnFinished handler for the code to execute after the pause, and call play() to start it.

– James_D
Oct 19 '14 at 19:43





A PauseTransition doesn't have to be associated with a node. You can just create one, use its setOnFinished handler for the code to execute after the pause, and call play() to start it.

– James_D
Oct 19 '14 at 19:43












2 Answers
2






active

oldest

votes


















12














The JavaFX animations are probably the way to go, but the "thread philosophy" in JavaFX isn't hard to work with if you want to roll your own, or do other, more complicated things in background threads.



The following code will pause and change the value in a label (full disclosure, I'm reusing 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 basic JavaFX background tool is the Task, any JavaFX application that actually does anything will probably be littered with these all over. Learn how to use them.






share|improve this answer































    6














    Dave's solution is great for general purpose off thread based work in JavaFX.



    If you wish to use the animation facilities of JavaFX, the solutions below demonstrate this using a Timeline or a ScaleTransition. The timeline implements a discrete scale of the UI element, so every quarter of a second the UI element is scaled larger or back to it's original size. The scale transition implements a smooth scale of the UI element, so the UI element gradually gets larger then smaller using an interpolated scale factor with the default easing 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.
    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26454149%2fmake-javafx-wait-and-continue-with-code%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      12














      The JavaFX animations are probably the way to go, but the "thread philosophy" in JavaFX isn't hard to work with if you want to roll your own, or do other, more complicated things in background threads.



      The following code will pause and change the value in a label (full disclosure, I'm reusing 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 basic JavaFX background tool is the Task, any JavaFX application that actually does anything will probably be littered with these all over. Learn how to use them.






      share|improve this answer




























        12














        The JavaFX animations are probably the way to go, but the "thread philosophy" in JavaFX isn't hard to work with if you want to roll your own, or do other, more complicated things in background threads.



        The following code will pause and change the value in a label (full disclosure, I'm reusing 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 basic JavaFX background tool is the Task, any JavaFX application that actually does anything will probably be littered with these all over. Learn how to use them.






        share|improve this answer


























          12












          12








          12







          The JavaFX animations are probably the way to go, but the "thread philosophy" in JavaFX isn't hard to work with if you want to roll your own, or do other, more complicated things in background threads.



          The following code will pause and change the value in a label (full disclosure, I'm reusing 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 basic JavaFX background tool is the Task, any JavaFX application that actually does anything will probably be littered with these all over. Learn how to use them.






          share|improve this answer













          The JavaFX animations are probably the way to go, but the "thread philosophy" in JavaFX isn't hard to work with if you want to roll your own, or do other, more complicated things in background threads.



          The following code will pause and change the value in a label (full disclosure, I'm reusing 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 basic JavaFX background tool is the Task, any JavaFX application that actually does anything will probably be littered with these all over. Learn how to use them.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Oct 19 '14 at 19:34









          DaveBDaveB

          89978




          89978

























              6














              Dave's solution is great for general purpose off thread based work in JavaFX.



              If you wish to use the animation facilities of JavaFX, the solutions below demonstrate this using a Timeline or a ScaleTransition. The timeline implements a discrete scale of the UI element, so every quarter of a second the UI element is scaled larger or back to it's original size. The scale transition implements a smooth scale of the UI element, so the UI element gradually gets larger then smaller using an interpolated scale factor with the default easing 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.
              }





              share|improve this answer




























                6














                Dave's solution is great for general purpose off thread based work in JavaFX.



                If you wish to use the animation facilities of JavaFX, the solutions below demonstrate this using a Timeline or a ScaleTransition. The timeline implements a discrete scale of the UI element, so every quarter of a second the UI element is scaled larger or back to it's original size. The scale transition implements a smooth scale of the UI element, so the UI element gradually gets larger then smaller using an interpolated scale factor with the default easing 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.
                }





                share|improve this answer


























                  6












                  6








                  6







                  Dave's solution is great for general purpose off thread based work in JavaFX.



                  If you wish to use the animation facilities of JavaFX, the solutions below demonstrate this using a Timeline or a ScaleTransition. The timeline implements a discrete scale of the UI element, so every quarter of a second the UI element is scaled larger or back to it's original size. The scale transition implements a smooth scale of the UI element, so the UI element gradually gets larger then smaller using an interpolated scale factor with the default easing 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.
                  }





                  share|improve this answer













                  Dave's solution is great for general purpose off thread based work in JavaFX.



                  If you wish to use the animation facilities of JavaFX, the solutions below demonstrate this using a Timeline or a ScaleTransition. The timeline implements a discrete scale of the UI element, so every quarter of a second the UI element is scaled larger or back to it's original size. The scale transition implements a smooth scale of the UI element, so the UI element gradually gets larger then smaller using an interpolated scale factor with the default easing 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.
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 21 '14 at 0:51









                  jewelseajewelsea

                  112k8264314




                  112k8264314






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f26454149%2fmake-javafx-wait-and-continue-with-code%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                      SQL update select statement

                      'app-layout' is not a known element: how to share Component with different Modules