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.51, “Printing with Graph2DPrinter” gives an overview on class Graph2DPrinter's central role in the printing process.

Figure 6.51. Printing with Graph2DPrinter

Printing with Graph2DPrinter.

Scaling and Clipping

Besides clipping, Graph2DPrinter also allows scaling the graph's visual representation for printing. API Excerpt 6.62, “Policy methods from class Graph2DPrinter” lists the methods that are used to control both clipping and scaling policy.

API Excerpt 6.62. Policy methods from class Graph2DPrinter

// Clipping policy getter and setter methods. 
byte getClipType()
void setClipType(byte clipType)

// Scaling policy getter and setter methods. 
byte getScalingType()
void setScalingType(byte scalingType)

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 methods from API Excerpt 6.63, “Methods from class Graph2DPrinter” can be used to control the necessary parameters.

API Excerpt 6.63. Methods from class Graph2DPrinter

// Getter and setter for the number of pages. 
int getPosterColumns()
void setPosterColumns(int columns)
int getPosterRows()
void setPosterRows(int rows)

// Getter and setter for the scaling factor. 
double getScalingFactor()
void setScalingFactor(double scalingFactor)

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 method from API Excerpt 6.64, “Title bar and footer support”. 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.

API Excerpt 6.64. Title bar and footer support

// Getter methods. 
Graph2DPrinter.TitleDrawable getTitleDrawable()
Graph2DPrinter.FooterDrawable getFooterDrawable()

// Setter methods for title bar and footer drawables. 
void setTitleDrawable(Graph2DPrinter.TitleDrawable title)
void setFooterDrawable(Graph2DPrinter.FooterDrawable 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.31, “Opening a dedicated "printing" view” demonstrates how a dedicated "printing" view is opened that shows a given rectangular part of the graph.

Example 6.31. 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());
  
  // Replace the currently active view containing the graph with the "printing" 
  // view. 
  graph.setCurrentView(printingView);
  
  return originalView;
}

Example 6.32, “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.32. 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);
}

Tutorial Demo Code

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.