Example #1
0
    public int print(Graphics gc, PageFormat format, int pageIndex)
        throws java.awt.print.PrinterException {
      if (pageIndex > 0) {
        out("page " + pageIndex + " requested, ending print job.");
        return Printable.NO_SUCH_PAGE;
      }

      Dimension page =
          new Dimension(
              (int) format.getImageableWidth() - 1, (int) format.getImageableHeight() - 1);

      Graphics2D g = (Graphics2D) gc;

      out(
          "asked to render page "
              + pageIndex
              + " in "
              + outpf(format)
              + " w/transform "
              + g.getTransform());

      // note: supposedly, perhaps on Windows with JVM's 1.5 and newer, the
      // transform scale provided can actually allow us to derive the DPI of the
      // print device, which we could use for image rendering optimization's
      // during prints.  Mac OS X Snow Leopard w/JVM 1.6 always reports a 1.0
      // scale though.  And operations like "print preview" or "print to PDF"
      // wouldn't have a fixed DPI anyway.

      if (DEBUG.CONTAINMENT) {
        g.setColor(Color.lightGray);
        g.fillRect(0, 0, 9999, 9999);
      }

      g.translate(format.getImageableX(), format.getImageableY());

      // Don't need to clip if printing whole map, as computed zoom
      // should have made sure everything is within page size
      // if (!isPrintingView())
      // g.clipRect(0, 0, page.width, page.height);

      if (DEBUG.CONTAINMENT) {
        // g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        // draw border outline of page
        g.setColor(Color.gray);
        g.setStroke(VueConstants.STROKE_TWO);
        g.drawRect(0, 0, page.width, page.height);
        // g.setComposite(AlphaComposite.Src);
      }

      // compute zoom & offset for visible map components
      Point2D.Float offset = new Point2D.Float();
      // center vertically only if landscape mode
      // if (format.getOrientation() == PageFormat.LANDSCAPE)
      // TODO: allow horizontal centering, but not vertical centering (handle in computeZoomFit)
      double scale = ZoomTool.computeZoomFit(page, 5, bounds, offset, true);
      out("rendering at scale " + scale);
      // set up the DrawContext
      DrawContext dc =
          new DrawContext(
              g, scale, -offset.x, -offset.y,
              null, // frame would be the PageFormat offset & size rectangle
              focal, false); // todo: absolute links shouldn't be spec'd here

      dc.setMapDrawing();
      dc.setPrintQuality();

      if (isPrintingView() && map == focal)
        g.clipRect(
            (int) Math.floor(bounds.getX()),
            (int) Math.floor(bounds.getY()),
            (int) Math.ceil(bounds.getWidth()),
            (int) Math.ceil(bounds.getHeight()));

      if (DEBUG.CONTAINMENT) {
        g.setColor(Color.red);
        g.setStroke(VueConstants.STROKE_TWO);
        g.draw(bounds);
      }

      // render the map
      if (map == focal) map.draw(dc);
      else focal.draw(dc);

      out("page " + pageIndex + " rendered.");
      return Printable.PAGE_EXISTS;
    }