gpt4 book ai didi

image - JavaFX : Canvas to Image in non GUI Thread

转载 作者:行者123 更新时间:2023-12-04 02:25:22 24 4
gpt4 key购买 nike

我必须可视化大量数据(实时)并且我使用的是 JavaFX 2.2。所以我决定在将数据插入 GUI 线程之前“预可视化”数据。

在我看来,最快的方法(使用抗锯齿等)是让一些非 GUI 线程生成图像/位图,然后放入 GUI 线程(因此 UI 仍然对用户有响应)。

但我找不到如何将 Canvas 转换为 Image 然后使用的方法:

Image imageToDraw = convert_tmpCanvasToImage(tmpCanvas);

Platform.runLater(new Runnable() {

@Override
public void run() {
canvas.getGraphicsContext2D().drawImage(imageToDraw, data.offsetX, data.offsetY);
}
});

感谢一些有用的答案。 :-)

顺便说一句:我已经制作了测试应用程序来显示我的问题。
package canvasandthreads02;

import java.util.Random;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class CanvasAndThreads02 extends Application {

@Override
public void start(Stage primaryStage) {
Button btn = new Button();
btn.setText("Paint");


final AnchorPane root = new AnchorPane();
final Canvas canvas = new Canvas(900, 800);
canvas.setLayoutX(50);
canvas.setLayoutY(50);
root.getChildren().add(canvas);
root.getChildren().add(btn);

Scene scene = new Scene(root, 900, 800);

primaryStage.setTitle("Painting in JavaFX");
primaryStage.setScene(scene);
primaryStage.show();

btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Start painting");
/**
* Start Thread where some data will be visualized
*/
new Thread(new PainterThread(canvas, new DataToPaint())).start();
}
});
}

private class PainterThread implements Runnable{
private final DataToPaint data;
private final Canvas canvas;
public PainterThread(Canvas canvas, DataToPaint data){
this.canvas = canvas;
this.data = data;
}

@Override
public void run() {
long currentTimeMillis = System.currentTimeMillis();

Canvas tmpCanvas = new Canvas(data.width, data.height);
GraphicsContext graphicsContext2D = tmpCanvas.getGraphicsContext2D();
graphicsContext2D.setFill(data.color;);
for (int i = 0; i < data.height; i++) {
for (int j = 0; j < data.width; j++) {
graphicsContext2D.fillRect(j, i, 1, 1); //draw 1x1 rectangle
}
}

/**
* And now I need still in this Thread convert tmpCanvas to Image,
* or use some other method to put result to Main GIU Thread using Platform.runLater(...);
*/
final Image imageToDraw = convert_tmpCanvasToImage(tmpCanvas);

System.out.println("Canvas painting: " + (System.currentTimeMillis()-currentTimeMillis));
Platform.runLater(new Runnable() {

@Override
public void run() {
//Start painting\n Canvas painting: 430 \n Time to convert:62
//long currentTimeMillis1 = System.currentTimeMillis();
//Image imageToDraw = tmpCanvas.snapshot(null, null);
//System.out.println("Time to convert:" + (System.currentTimeMillis()-currentTimeMillis1));
canvas.getGraphicsContext2D().drawImage(imageToDraw, data.offsetX, data.offsetY);
}
});
}
}

private class DataToPaint{
double offsetX = 0;
double offsetY = 0;
Color color;
int width = 500;
int height = 250;

public DataToPaint(){
Random rand = new Random();
color = new Color(rand.nextDouble(), rand.nextDouble(), rand.nextDouble(), rand.nextDouble());
offsetX = rand.nextDouble() * 20;
offsetY = rand.nextDouble() * 20;
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
}

最佳答案

使用 Canvas 的 snapshot(...) 方法从 Canvas 的内容创建 WritableImage。 ^^
对我来说很好用。

关于image - JavaFX : Canvas to Image in non GUI Thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12229487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com