Class Graph2DPrinter provides all functionality that is necessary to print a graph. It supports printing the complete graph or only the part that is visible in the Graph2DView containing it. Figure 6.66, “Printing with Graph2DPrinter” gives an overview on class Graph2DPrinter's central role in the printing process.
By default, Graph2DPrinter uses fractional metrics to achieve exact font sizes and font spacing for printing. Rendering hints in general can be specified using the following setter method:
RenderingHints getRenderingHints() void setRenderingHints(RenderingHints renderingHints) |
|
Description | Getter and setter method for rendering hints. |
Rendering hints that are associated with the graph's Graph2DView and that control aspects of the rendering process of the view, like, e.g., whether expensive features such as gradients should be reduced to a single color in order to save resources, will be honored by the view when preparing the graph's visual representation before printing.
Besides clipping, Graph2DPrinter also allows scaling the graph's visual representation for printing. The following methods can be used to control both clipping and scaling policy:
byte getClipType() void setClipType(byte clipType) |
|
Description | Clipping policy getter and setter methods. |
byte getScalingType() void setScalingType(byte scalingType) |
|
Description | Scaling policy getter and setter methods. |
There are two scaling policies supported: either the number of desired pages in both horizontal and vertical direction is given, then the graph will be scaled automatically to fit the available space. Or, alternatively, the graph's scaling is chosen to be fixed, then the number of necessary pages will be inferred instead. Using either scaling policy, the following methods can be used to control the necessary parameters:
int getPosterColumns() void setPosterColumns(int columns) int getPosterRows() void setPosterRows(int rows) |
|
Description | Getter and setter for the number of pages. |
double getScalingFactor() void setScalingFactor(double scalingFactor) |
|
Description | Getter and setter for the scaling factor. |
Adding a title bar to the top of a printed page can be accomplished by setting an implementation of interface Graph2DPrinter.TitleDrawable with a Graph2DPrinter using the corresponding from the following methods. Likewise, by setting an implementation of interface Graph2DPrinter.FooterDrawable with a Graph2DPrinter a footer can be added to the bottom of a printed page.
Classes Graph2DPrinter.DefaultTitleDrawable and Graph2DPrinter.DefaultFooterDrawable are the default TitleDrawable and FooterDrawable implementations that are initially set with a Graph2DPrinter object. They offer attributes to control title and footer text, the fonts and text colors thereof, and also background colors for both title bar and footer.
Graph2DPrinter.TitleDrawable getTitleDrawable() Graph2DPrinter.FooterDrawable getFooterDrawable() |
|
Description | Title bar and footer getter methods. |
void setTitleDrawable(Graph2DPrinter.TitleDrawable title) void setFooterDrawable(Graph2DPrinter.FooterDrawable footer) |
|
Description | Setter methods for title bar and footer drawables. |
Class PrintPreviewPanel provides a print preview component that can be used in conjunction with Graph2DPrinter to create a preview of the printing.
The functionality offered by class Graph2DPrinter can be mixed with the same technique used to do an image export of a graph. In particular, Example 6.50, “Opening a dedicated "printing" view” demonstrates how a dedicated "printing" view is opened that shows a given rectangular part of the graph.
Example 6.50. Opening a dedicated "printing" view
Graph2DView replaceCurrentWithPrintingView(Graph2D graph, Rectangle r) { // Save the currently active view. Graph2DView originalView = (Graph2DView)graph.getCurrentView(); // Create a new Graph2DView instance with the graph. This will be the // dedicated view for printing. Graph2DView printingView = new Graph2DView(graph); // Set the dimension of the view to the given rectangle. printingView.fitRectangle(r); // Use the Graph2DRenderer instance of the currently active view. (Optional.) printingView.setGraph2DRenderer(originalView.getGraph2DRenderer()); // Use the rendering hints of the currently active view. (Optional.) printingView.setRenderingHints(originalView.getRenderingHints()); // Replace the currently active view containing the graph with the "printing" // view. graph.setCurrentView(printingView); return originalView; }
Example 6.51, “Using class ViewPortConfigurator to set up a view for printing” shows how this "printing" view is configured using class ViewPortConfigurator. Except for the clipping type all configurator settings are left to their respective default values, i.e., a zoom level of 100%, and an additional margin of 15 pixels around the view's clipping.
Example 6.51. Using class ViewPortConfigurator to set up a view for printing
void configurePrintingView(Graph2DView printingView) { ViewPortConfigurator vpc = new ViewPortConfigurator(); // Register the view with the configurator instance. vpc.setGraph2DView(printingView); // Only a part of the graph should be printed, hence set the clipping type // accordingly. vpc.setClipType(ViewPortConfigurator.CLIP_VIEW); // Configure the printing view using mainly default values, i.e., zoom level // 100%, and 15 pixel margin around the view's clipping. vpc.configure(printingView); }
The usage of the print preview is explained in the tutorial demo PrintPreviewDemo.java. The tutorial demo application ViewActionDemo.java shows the usage of Graph2DPrinter in general.
Copyright ©2004-2012, yWorks GmbH. All rights reserved. |