読者です 読者をやめる 読者になる 読者になる

Printing on JavaFX 1.1

JavaFX 1.1 doesn't have the printing feature. Although, we can use Java printing features if wanting to print.

When printing in Java, we should make a class implemented java.awt.Printable or java.awt.Pegeable, and call print method of the class.

The type of the print method's first argument is java.awt.Graphics, so call print (printAll) or paint method of the target GUI component class (ex. JTable).

The big problem is how to get Component/JComponent object from JavaFX Node object.

Node class is corresponding to SGNode class on the scenegraph project, and JSGPanel class has SGNode. Node class defines a protected method impl_getSGNode() to get the SGNode object, and SGNode class deines getPanel() method to get the JSGPanel objet.

You must add Scenario.jar into class path, because SGNode and JSGPanel are defined in Scenario.jar.

The following is a simple printing sample. When clicking a button, application shows the print dialog, and then prints.

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import javax.swing.JComponent;

import javafx.ext.swing.SwingButton;
import javafx.ext.swing.SwingLabel;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

class FXPrintable extends Printable {
    public-init var comp: JComponent;

    public override function print(g: Graphics,
                                    pageformat: PageFormat,
                                    index: Integer): Integer {
        // set offset of the imaggeable area.
        var offsetX = pageformat.getImageableX();
        var offsetY = pageformat.getImageableX();
        var g2d = g as Graphics2D;
        g2d.translate(offsetX, offsetY);

        if (index == 0) {
            comp.paint(g2d);
            return Printable.PAGE_EXISTS;
        } else {
            return Printable.NO_SUCH_PAGE;
        }
    }
};

class PrintableNode extends Group {
    public function print(): Void {
        var printable = FXPrintable {
            comp: this.impl_getSGNode().getPanel()
        }
        // show print dialog and then print
        var job = PrinterJob.getPrinterJob();
        job.setPrintable(printable);
        job.print();
    }
};

var node: PrintableNode = PrintableNode {
    content: [
        Rectangle {
            x: 10, y: 10
            width: 140, height: 90
            fill: Color.RED
        },
        SwingLabel {
            translateX: 100 translateY: 100
            text: "Label"
        },
        SwingButton {
            translateX: 20 translateY: 120
            text: "Print"
            action: function() {
                node.print();
            }
        }
    ]
};

var stage = Stage {
    title : "Printing Test"
    scene: Scene {
        width: 200
        height: 200
        content: node
    }
};