Example usage for javafx.scene.layout Pane snapshot

List of usage examples for javafx.scene.layout Pane snapshot

Introduction

In this page you can find the example usage for javafx.scene.layout Pane snapshot.

Prototype

public WritableImage snapshot(SnapshotParameters params, WritableImage image) 

Source Link

Document

Takes a snapshot of this node and returns the rendered image when it is ready.

Usage

From source file:snpviewer.SnpViewer.java

public void convertSampleViewToImage(final SnpFile s, final Pane pane, final String chrom, final String path) {
    WritableImage image;/*from w ww.  j a  va  2s  .  c o m*/
    try {
        image = pane.snapshot(null, null);
    } catch (IllegalStateException ex) {
        Dialogs.showErrorDialog(null, "Error while attempting to convert" + " view to image file",
                "PNG conversion failed", "SNP Viewer", ex);
        return;
    }

    final DrawPaneToPng drawToPng = new DrawPaneToPng(image);
    drawToPng.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent t) {
            File pngFile;
            if (path != null && path.length() > 0) {
                pngFile = new File(s.getOutputDirectoryName() + "/" + path + "/" + chrom + ".png");
            } else {
                pngFile = new File(s.getOutputDirectoryName() + "/" + chrom + ".png");
            }
            try {
                if (!pngFile.getParentFile().exists()) {
                    boolean madeDir = pngFile.getParentFile().mkdir();
                    if (madeDir == false) {
                        Dialogs.showErrorDialog(null, "Unable to make sub directory"
                                + " when converting dynamic view to image file. Please " + "check permissions.",
                                "PNG conversion failed", "SNP Viewer");
                        return;
                    }
                }
                Files.copy(drawToPng.getImageFile().toPath(), pngFile.toPath(), REPLACE_EXISTING);
                BufferedImage bufferedImage = ImageIO.read(pngFile);
                Image image = SwingFXUtils.toFXImage(bufferedImage, null);

                ImageView chromImage = new ImageView(image);
                // chromImage.setCache(true);
                for (Iterator it = pane.getChildren().iterator(); it.hasNext();) {
                    Object line = it.next();
                    if (line instanceof Line) {
                        /*Line l = (Line) line;
                        l.startXProperty().unbind();
                        l.endXProperty().unbind();
                        l.endYProperty().unbind();*/
                    } else if (line instanceof ImageView) {
                        ImageView l = (ImageView) line;
                        l.fitHeightProperty().unbind();
                        l.fitWidthProperty().unbind();
                    }
                }
                pane.getChildren().clear();
                pane.getChildren().add(chromImage);

                chromImage.fitWidthProperty().bind(pane.widthProperty());
                chromImage.fitHeightProperty().bind(pane.heightProperty());
            } catch (IOException ex) {
                Dialogs.showErrorDialog(null,
                        "IOException while attempting to convert" + " dynamic view to image file",
                        "PNG conversion failed", "SNP Viewer", ex);
            }
        }
    });
    drawToPng.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent t) {
            Dialogs.showErrorDialog(null, "Error attempting to convert" + " dynamic view to image file",
                    "PNG conversion failed", "SNP Viewer");

        }
    });
    drawToPng.start();
}