JavaFX:使用服务类定期重新绘制ImageView。图像保持不变
我在JavaFX中遇到了多线程问题。我使用来自javafx.concurrent
包的Service
类定期重新计算并更改ImageView
中的图像。
为此,我有一个方法,它是从JavaFX应用程序的start()
方法调用的:
public void startRepaintingThread() {
for (GameWindow gamewindow : gameWindows) {
RepaintingLoopService service = new RepaintingLoopService(gamewindow);
service.setOnSucceeded((eh) -> {
gamewindow.setImage(service.getValue());
service.reset();
service.start();
});
service.start();
}
}
这里GameWindow
是ImageView
的一个简单子类RepaintingLoopService
是javafx.concurrent.Service
的一个子类,它执行一些复杂的逻辑来重新计算图像,并将新图像作为其值返回。现在,我使用调试器和日志验证了这段代码中的service.getValue()
确实返回了正确重新计算的图像,因此重新计算逻辑是正确的,但在UI中图像仍然保持不变!或者,更准确地说:在一些非常罕见的情况下,它实际上发生了变化,但在95%的情况下,它仍然是一个静态图像(第一个绘制的),因此它似乎取决于某些种族条件或其他……我想,也许你知道什么可能是错误的?我将全局变量gameWindows
设置为volatile
,以及多个线程使用的所有其他全局变量。也许我用错了Service
的方法
===========================================
我确实可以建立一个完整的小例子来重现这个错误。请参阅下面的代码。此应用程序中的Service
会定期重新绘制图像,从白色图像开始,每次添加一行黑色像素。
再说一次:它似乎可以工作……有时:对我来说,它在我第一次运行它时工作得很好,但在我启动程序的任何时候,在添加前2或3条黑线后,UI中的图像不再改变…
代码如下:
import javafx.application.Application;
import javafx.concurrent.Service;
import javafx.concurrent.Task;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class BugFix extends Application {
private static final int WINDOW_WIDTH = 800;
private static final int WINDOW_HEIGHT = 800;
/**
* Time in ms between repainting attempts
**/
private static final long REPAINTING_TIME = 100;
private ImageView imageView;
private RepaintingService service = new RepaintingService();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
setUpStage(primaryStage);
startService();
}
private void setUpStage(Stage stage) {
Group group = new Group();
imageView = new ImageView(new WritableImage(WINDOW_WIDTH, WINDOW_HEIGHT));
group.getChildren().add(imageView);
stage.setScene(new Scene(group, WINDOW_WIDTH, WINDOW_HEIGHT));
stage.show();
}
private void startService() {
service.setOnSucceeded((eh) -> {
imageView.setImage(service.getValue());
int firstWhiteLine = findFirstWhiteLineInImage(service.getValue());
System.out.println("First white line in received image: " + firstWhiteLine);
try {
Thread.sleep(REPAINTING_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.reset();
service.start();
});
service.start();
}
/**
* For debug purposes: Do find the number of the first line with white pixels
* in the given image.
**/
private int findFirstWhiteLineInImage(Image repaintedImage) {
for (int line = 0; line < repaintedImage.getHeight(); line++) {
if (Color.WHITE.equals(repaintedImage.getPixelReader().getColor(0, line))) {
return line;
}
}
return -1;
}
/**
* A service to periodically repaint the image,
* starting off with a white image and with each repainting adding a black line of pixels.
**/
private class RepaintingService extends Service<Image> {
private volatile WritableImage image = new WritableImage(WINDOW_WIDTH, WINDOW_HEIGHT);
private int blackLinesCount = 0;
@Override
protected Task<Image> createTask() {
return new Task<Image>() {
@Override
protected Image call() {
repaintImage();
blackLinesCount++;
return image;
}
};
}
/**
* Repaints the image with the upper n lines being black
* and the remaining lines being white.
**/
private void repaintImage() {
for (int line = 0; line < WINDOW_HEIGHT; line++) {
for (int column = 0; column < WINDOW_HEIGHT; column++) {
Color color = line <= blackLinesCount ? Color.BLACK : Color.WHITE;
image.getPixelWriter().setColor(column, line, color);
}
}
}
}
}
在任何情况下,程序的控制台输出总是相同的:
First white line in received image: 1
First white line in received image: 2
First white line in received image: 3
First white line in received image: 4
First white line in received image: 5
(等等……)因此,这同样意味着,从service.getValue()
读取的图像始终是正确重新绘制的图像。但是由于某些原因,它并不(总是)显示在UI中,尽管该图像在同一行中被传递给imageView.setImage()
# 1 楼答案
代码中的问题是,场景图中活动的节点(imageView)的属性(此处:图像中的像素)在fx应用程序线程外更新。这有效地阻止了ui本身的更新
解决方案是让背景线程返回其正在处理的图像的副本:
一种实用方法-仅从another answer中选取,以演示其效果: