Printing a Graph's Visual Representation

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.

Figure 6.66. Printing with Graph2DPrinter

Printing with Graph2DPrinter.

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.

Scaling and Clipping

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.

Title Bar and Footer Support

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.

Printing Preview

Class PrintPreviewPanel provides a print preview component that can be used in conjunction with Graph2DPrinter to create a preview of the printing.

Creating a Dedicated Printing View

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. 

  // Use the Graph2DRenderer instance of the currently active view. (Optional.) 
  // Use the rendering hints of the currently active view. (Optional.)
  // Replace the currently active view containing the graph with the "printing" 
  // view. 
  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. 
  // Only a part of the graph should be printed, hence set the clipping type 
  // accordingly. 
  // Configure the printing view using mainly default values, i.e., zoom level 
  // 100%, and 15 pixel margin around the view's clipping. 

Tutorial Demo Code

The usage of the print preview is explained in the tutorial demo The tutorial demo application shows the usage of Graph2DPrinter in general.