Mav*_*283 9 javafx wait thread-sleep
基本上我试图使用JavaFX做一个简短的效果.我有一个心脏的形状(从两个圆圈和一个多边形加起来),我可以使用double值改变大小p."标准尺寸"将是p = 1.0;.
我试图给心脏增加一个泵效应.我有方法pumpOnce():
public void pumpOnce(){
p = p + 1;
initHeart();
//Here goes what ever it takes to make stuff working!!
p = p - 1;
initHeart();
}
Run Code Online (Sandbox Code Playgroud)
initHeart()吸取内心的基础p.
我发现Thread.sleep();由于JavaFX中的线程原理,或类似的方法不起作用.
但是我可以用什么呢?
小智 16
JavaFX的动画是可能的路要走,但"线哲学"在JavaFX是不是很难用,如果你想推出自己的,还是在后台线程其他更复杂的东西的工作.
以下代码将暂停并更改标签中的值(完全公开,我正在重用我为另一个问题编写的代码):
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();
}
}
Run Code Online (Sandbox Code Playgroud)
基本的JavaFX后台工具是Task,任何实际做任何事情的JavaFX应用程序都可能会被这些全部乱七八糟.了解如何使用它们.
Dave的解决方案非常适合JavaFX中基于线程的通用工作.
如果您希望使用JavaFX的动画工具,下面的解决方案使用时间轴或ScaleTransition演示这一点.时间轴实现了UI元素的离散比例,因此UI元素的每四分之一秒缩放更大或恢复到原始大小.缩放过渡实现了UI元素的平滑缩放,因此UI元素使用带有默认缓动插值器的插值缩放因子逐渐变大然后变小.
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.
}
Run Code Online (Sandbox Code Playgroud)