JavaFX 8 - Printing 3d scene

Cheryl Long clong9993 at gmail.com
Tue Nov 11 01:16:40 UTC 2014


I posted the following question on Stack Overflow and it was suggested that
I ask it here.

I am trying to use the JavaFX 8 printing API and I am running into problems
when I try to print any scene/node that contains any sort of 3D content. I
have been searching both here and other sites for an answer and haven't
been able to find anything to help.

If the node contains only 2D content, the scene will print on my selected
printer without issue. But if I add a single 3D object to the scene, the
whole thing fails to print - without any error but without any content. If
I am printing to a printer, a blank page is "printed" and if I am printing
to a document printer, I get an empty document.

I have tried using the AWT printing mechanism to convert the node to a
WritableImage and then to a BufferedImage for printing. This works in that
it prints but we aren't happy with the quality that we get from it.

My print caller is fairly standard at this stage -

private void doPrintFX() {
    Printer printer = Printer.getDefaultPrinter();
    PageLayout pageLayout = printer.createPageLayout(
            Paper.A3, PageOrientation.LANDSCAPE, Printer.MarginType.DEFAULT);

    final double scaleFactor =
computeScaleFactor(pageLayout.getPrintableWidth(),
            pageLayout.getPrintableHeight(),
            sceneNode.getBoundsInParent().getWidth(),
            sceneNode.getBoundsInParent().getHeight());
    if (scaleFactor != 0) {
        sceneNode.getTransforms().add(new Scale(scaleFactor, scaleFactor));
    }

    PrinterJob job = PrinterJob.createPrinterJob(printer);
    if (job != null) {
        System.out.println("Printing to " + job.getPrinter().getName());
        boolean success = job.printPage(pageLayout, sceneNode);
        if (success) {
            job.endJob();
        }
    }}

I have scaled back the node to be a very simple scene containing a couple
of rectangles and a cylinder. The code I am currently using is:

    Cylinder cylinder = new Cylinder(50, 150);
    cylinder.setLayoutX(250);
    cylinder.setLayoutY(100);
    canvas.getDrawingPane().getChildren().add(cylinder);

    Rectangle item = new Rectangle(50, 50, 100, 50);
    item.setStroke(Color.BLACK);
    item.setFill(null);
    canvas.getDrawingPane().getChildren().add(item);

    item = new Rectangle(75, 75, 100, 50);
    item.setStroke(Color.BLACK);
    item.setFill(null);
    canvas.getDrawingPane().getChildren().add(item);

The canvas.getDrawingPane is a Pane and is what is being sent to the print
function.

Can anyone advise what might be wrong with what I am doing? Does the JavaFX
print API support printing of 3D nodes?

Thank you.


More information about the openjfx-dev mailing list