JavaFX : Canvas to Image in non GUI Thread


Question

I have to visualize lot of data (real-time) and I am using JavaFX 2.2. So I have decided to "pre-visualize" data before they are inserted into GUI thread.

In my opinion the fastest way to do it (with antialliasing etc.) is let some NON GUI thread to generate image/bitmap and then put in GUI thread (so the UI is still responsive for user).

But I can't find way how to conver Canvas to Image and then use:

Image imageToDraw = convert_tmpCanvasToImage(tmpCanvas);

Platform.runLater(new Runnable() {

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

Thx for some usable answers. :-)

btw: I have made test app to show my problem.

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);
}
}
1
11
9/1/2012 4:39:49 PM

use Canvas' snapshot(...) method to create a WritableImage from the Canvas' content. ^^ Works fine for me.

2
6/20/2017 8:56:28 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon