public int print(Graphics g, PageFormat printFormat, int page)
{
int result = NO_SUCH_PAGE;
// Disables double-buffering before printing
RepaintManager currentManager = RepaintManager
.currentManager(mxGraphComponent.this);
currentManager.setDoubleBufferingEnabled(false);
// Gets the current state of the view
mxGraphView view = graph.getView();
// Stores the old state of the view
boolean eventsEnabled = view.isEventsEnabled();
mxPoint translate = view.getTranslate();
// Disables firing of scale events so that there is no
// repaint or update of the original graph while pages
// are being printed
view.setEventsEnabled(false);
// Uses the view to create temporary cell states for each cell
mxTemporaryCellStates tempStates = new mxTemporaryCellStates(view,
1 / pageScale);
try
{
view.setTranslate(new mxPoint(0, 0));
mxGraphics2DCanvas canvas = createCanvas();
canvas.setGraphics((Graphics2D) g);
canvas.setScale(1 / pageScale);
view.revalidate();
mxRectangle graphBounds = graph.getGraphBounds();
Dimension pSize = new Dimension((int) Math.ceil(graphBounds.getX()
+ graphBounds.getWidth()) + 1, (int) Math.ceil(graphBounds
.getY() + graphBounds.getHeight()) + 1);
int w = (int) (printFormat.getImageableWidth());
int h = (int) (printFormat.getImageableHeight());
int cols = (int) Math.max(
Math.ceil((double) (pSize.width - 5) / (double) w), 1);
int rows = (int) Math.max(
Math.ceil((double) (pSize.height - 5) / (double) h), 1);
if (page < cols * rows)
{
int dx = (int) ((page % cols) * printFormat.getImageableWidth());
int dy = (int) (Math.floor(page / cols) * printFormat
.getImageableHeight());
g.translate(-dx + (int) printFormat.getImageableX(), -dy
+ (int) printFormat.getImageableY());
g.setClip(dx, dy, (int) (dx + printFormat.getWidth()),
(int) (dy + printFormat.getHeight()));
graph.drawGraph(canvas);
result = PAGE_EXISTS;
}
}
finally
{
view.setTranslate(translate);
tempStates.destroy();
view.setEventsEnabled(eventsEnabled);
// Enables double-buffering after printing
currentManager.setDoubleBufferingEnabled(true);
}
return result;
}