Example usage for javafx.scene Scene snapshot

List of usage examples for javafx.scene Scene snapshot

Introduction

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

Prototype

public WritableImage snapshot(WritableImage image) 

Source Link

Document

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

Usage

From source file:de.ifsr.adam.ImageGenerator.java

/**
 * Takes a snapshot of the Pane and prints it to a file.
 *
 * @return True if no IOException occurred
 *//*from ww w  .  ja va  2  s .c o  m*/
private boolean printToFile(String fileName, Pane pane) {
    Group root = new Group();
    Scene printScene = new Scene(root);

    printScene.getStylesheets().add(this.stylesheetURI.toString());

    ((Group) printScene.getRoot()).getChildren().addAll(pane);
    WritableImage image = printScene.snapshot(null);
    File outFile = new File(fileName + "." + formatName);

    try {
        ImageIO.write(SwingFXUtils.fromFXImage(image, null), formatName, outFile);
        return true;
    } catch (IOException e) {
        log.error(e);
        return false;
    }
}

From source file:de.ifsr.adam.ImageGenerator.java

/**
 * Takes a snapshot of the Pane and prints it to a pdf.
 *
 * @return True if no IOException occurred
 */// w w w  .j  a v a 2s . c om
private boolean printToPDF(String filePath, Pane pane) {
    //Scene set up
    Group root = new Group();
    Scene printScene = new Scene(root);
    printScene.getStylesheets().add(this.stylesheetURI.toString());

    //Snapshot generation
    ArrayList<WritableImage> images = new ArrayList<>();
    try {
        ObservableList<Node> panes = pane.getChildren();
        for (int i = 0; i < panes.size(); i++) {
            GridPane gridPane = (GridPane) panes.get(i);
            ((Group) printScene.getRoot()).getChildren().clear();
            ((Group) printScene.getRoot()).getChildren().addAll(gridPane);
            images.add(printScene.snapshot(null));
            panes.add(i, gridPane);
        }
    } catch (Exception e) {
        log.error(e);
        return false;
    }

    //PDF Setup
    File outFile = new File(filePath + "." + "pdf");
    Iterator<WritableImage> iterImages = images.iterator();
    PDDocument doc = new PDDocument();

    try {

        while (iterImages.hasNext()) {
            //Page setup
            PDPage page = new PDPage();
            doc.addPage(page);
            PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, false);
            //Image setup
            BufferedImage bufImage = SwingFXUtils.fromFXImage(iterImages.next(), null);
            PDPixelMap pixelMap = new PDPixelMap(doc, bufImage);

            int width = (int) (page.getMediaBox().getWidth());
            int height = (int) (page.getMediaBox().getHeight());
            Dimension dim = new Dimension(width, height);
            contentStream.drawXObject(pixelMap, 0, 0, dim.width, dim.height);
            contentStream.close();
        }

        doc.save(outFile);
        return true;
    } catch (IOException | COSVisitorException e) {
        log.error(e);
        return false;
    }
}