package com.bbn.openmap.image; import com.bbn.openmap.MapBean; import java.awt.Component; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.print.PageFormat; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import javax.swing.RepaintManager; /** * AbstractMapBeanPrinter is a generic class for creating print jobs * with MapBeans in it. This class can be subclassed to add extra * printing code, for istance to add text labels, map thumbnails, or any * other "extra" printable in addition to the map itself. * * Any subclass must implement the print(...) method specified in interface * Printable. * * Subclasses may use the following functions in the implementation of the * print method: * * alignPaper(...) - aligns the paper so that (0,0) represents the upper left * corner of the printable area. * * drawMapBean(...) - draws the MapBean to a specified area of the print with * the specified size. * * Protected variable MapBeanToBePrinted may also be accessed, perhaps to * manipulate an off-screen image. * * @author Brian Sperlongano */ public abstract class AbstractMapBeanPrinter implements Printable { protected MapBean MapBeanToBePrinted; /** * Constructor * * @param mapBean MapBean to be printed * */ public AbstractMapBeanPrinter(MapBean mapBean) { MapBeanToBePrinted = mapBean; } /** * Sends the print job to the printer * * @author Brian Sperlongano */ public void print() { PrinterJob printJob = PrinterJob.getPrinterJob(); printJob.setPrintable(this); if (printJob.printDialog()) try { printJob.print(); } catch(PrinterException pe) { System.out.println("Error printing: " + pe); } } /** * Aligns the Graphics object to the page's printable area. * * @author Brian Sperlongano */ public void alignPaper(Graphics g, PageFormat pageFormat) { g.translate((int)(pageFormat.getImageableX()), (int)(pageFormat.getImageableY())); } /** * Draws a MapBean onto a Graphics object for printing. * * @param g Graphics object to draw to * @param pageFormat Format of the page to print to * @param x Image X position, percentage * @param y Image Y position, precentage * @param width Image width, percentage * @param height Image height, percentage * @param proportional if true, aspect ratio of the MapBean will * be maintained and will leave whitespace * beneath the image or to the right if the * aspect ratios are dissimilar. If false, the * image will scale completely */ public void drawMapBean( Graphics g, PageFormat pageFormat, double x, double y, double width, double height, boolean proportional) { Graphics2D g2d = (Graphics2D)g; double pageHeight = pageFormat.getImageableHeight(); double pageWidth = pageFormat.getImageableWidth(); double mapHeight = MapBeanToBePrinted.getHeight(); double mapWidth = MapBeanToBePrinted.getWidth(); //set the (X,Y) position g2d.translate(x*pageWidth,y*pageHeight); //determine the scale double scaleX = width * pageWidth / mapWidth; double scaleY = height * pageHeight / mapHeight; if(proportional) { //maintain the aspect ratio double scale = scaleX > scaleY ? scaleY : scaleX; //transform the graphics object for drawing g2d.scale(scale, scale); } else { g2d.scale(scaleX, scaleY); } // Do the work now ... disableDoubleBuffering(MapBeanToBePrinted); MapBeanToBePrinted.paint(g2d); enableDoubleBuffering(MapBeanToBePrinted); //revert the transforms g2d.scale(1/scaleX, 1/scaleY); g2d.translate(-x*pageWidth,-y*pageHeight); } /** * The speed and quality of printing suffers dramatically if * any of the containers have double buffering turned on. * So this turns if off globally. */ protected static void disableDoubleBuffering(Component c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(false); } /** Re-enables double buffering globally. */ protected static void enableDoubleBuffering(Component c) { RepaintManager currentManager = RepaintManager.currentManager(c); currentManager.setDoubleBufferingEnabled(true); } }