Example #1
0
  /*@Override*/ public void paintIcon(Component c, Graphics g, int x, int y) {
    final boolean expandToFit = (mWidth < 1);

    if (DEBUG.IMAGE && DEBUG.META)
      out(
          "paintIcon; onto="
              + GUI.name(c)
              + " painter="
              + GUI.name(mPainter)
              + "@"
              + Integer.toHexString((mPainter.hashCode())));

    if (mPainter == null) {
      // note this means repaint updates would stop in a new parent,
      // tho assuming it's loaded by then, regular paints would work fine.
      mPainter = c;
    }

    if (DrawBorder && !expandToFit) {
      g.setColor(Color.gray);
      g.drawRect(x, y, mWidth - 1, mHeight - 1);
    }

    if (mImage == null) {

      if (!isLoading /*&& mPreviewData != null*/) {
        synchronized (this) {
          if (!isLoading /*&& mPreviewData != null*/)
            VUE.invokeAfterAWT(ResourceIcon.this); // load the preview
        }
      }
      g.setColor(Color.gray);
      g.drawRect(x, y, mWidth - 1, mHeight - 1);
      return;
    }

    int fitWidth, fitHeight;
    final Dimension maxImageSize;

    if (expandToFit) {
      // fill the given component
      fitWidth = c.getWidth();
      fitHeight = c.getHeight();
      maxImageSize = c.getSize();
    } else {
      // paint at our fixed size
      fitWidth = mWidth;
      fitHeight = mHeight;

      if (DrawBorder)
        maxImageSize = new Dimension(fitWidth - BorderSpace * 2, fitHeight - BorderSpace * 2);
      else maxImageSize = new Dimension(fitWidth, fitHeight);

      if (DEBUG.IMAGE && DEBUG.META) out("paintIcon; into " + GUI.name(maxImageSize));
    }

    double zoomFit;
    if (mImage == NoImage && expandToFit) {
      zoomFit = 1;
    } else {
      Rectangle2D imageBounds;
      if (CropToSquare) {
        // square off image, then fit in icon (todo: better; crop to icon)
        int smallestAxis = mImageWidth > mImageHeight ? mImageHeight : mImageWidth;
        imageBounds = new Rectangle2D.Float(0, 0, smallestAxis, smallestAxis);
      } else {
        // fit entire image in icon
        imageBounds = new Rectangle2D.Float(0, 0, mImageWidth, mImageHeight);
      }
      zoomFit = ZoomTool.computeZoomFit(maxImageSize, 0, imageBounds, null, false);
      if (zoomFit > MaxZoom) zoomFit = MaxZoom;
    }

    final int drawW = (int) (mImageWidth * zoomFit + 0.5);
    final int drawH = (int) (mImageHeight * zoomFit + 0.5);

    int xoff = x;
    int yoff = y;

    // center if drawable area is bigger than image
    if (drawW != fitWidth) xoff += (fitWidth - drawW) / 2;
    if (drawH != fitHeight) yoff += (fitHeight - drawH) / 2;

    Shape oldClip = null;
    if (CropToSquare && !expandToFit) {
      oldClip = g.getClip();
      g.clipRect(x, y, mWidth, mHeight);
    }

    if (DEBUG.IMAGE && DEBUG.META)
      out("paintIcon; " + Util.tag(mImage) + " as " + drawW + "x" + drawH);
    g.drawImage(mImage, xoff, yoff, drawW, drawH, null);

    if (DEBUG.BOXES) {
      g.setColor(Color.green);
      ((Graphics2D) g)
          .setComposite(
              java.awt.AlphaComposite.getInstance(java.awt.AlphaComposite.SRC_OVER, 0.2f));
      g.drawRect(x, y, mWidth - 1, mHeight - 1);
      ((Graphics2D) g).setComposite(java.awt.AlphaComposite.SrcOver);
    }

    if (CropToSquare && !expandToFit) g.setClip(oldClip);
  }
Example #2
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;
    }