Ejemplo n.º 1
0
  @Override
  public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
    Graphics2D g2;

    if (pageIndex > 0) return Printable.NO_SUCH_PAGE;

    g2 = (Graphics2D) g;

    g2.translate(pf.getImageableX(), pf.getImageableY());

    Dimension imageSize = calcDimension(image, pf.getImageableWidth(), pf.getImageableHeight());

    g2.drawImage(
        image,
        (int) (pf.getImageableWidth() / 2 - imageSize.width / 2),
        (int) (pf.getImageableHeight() / 2 - imageSize.height / 2),
        imageSize.width,
        imageSize.height,
        this);

    g.setXORMode(Color.white);

    Font font = g2.getFont().deriveFont(6);
    g2.setFont(font);
    int fileNameWidth = g2.getFontMetrics(font).stringWidth(fileName);

    if (fileNameWidth > imageSize.width) {
      pathStrings = fileName.split("\\\\");
      result = pathStrings[0] + "\\...\\" + pathStrings[pathStrings.length - 1];
      resultWidth = g.getFontMetrics(font).stringWidth(result);

      // TODO:
      // Add another if for truncating to just a few letters if needed.
    } else {
      result = fileName;
      resultWidth = g.getFontMetrics(font).stringWidth(result);
    }

    g.drawString(
        result,
        (int) (pf.getImageableWidth() / 2 + imageSize.width / 2 - resultWidth - 5),
        (int) (pf.getImageableHeight() / 2 + imageSize.height / 2 - 5));
    return Printable.PAGE_EXISTS;
  }
Ejemplo n.º 2
0
  @Override
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    if (image != null) {
      Dimension imageSize = calcDimension(image);
      System.out.println(imageSize + " or " + imageSize.getWidth());
      g.drawImage(
          image,
          getWidth() / 2 - imageSize.width / 2,
          getHeight() / 2 - imageSize.height / 2,
          imageSize.width,
          imageSize.height,
          this);

      g.setXORMode(Color.white);

      Font font = g.getFont().deriveFont(8);
      g.setFont(font);
      int fileNameWidth = g.getFontMetrics(font).stringWidth(fileName);

      if (fileNameWidth > imageSize.width) {
        pathStrings = fileName.split("\\\\");
        result = pathStrings[0] + "\\...\\" + pathStrings[pathStrings.length - 1];
        resultWidth = g.getFontMetrics(font).stringWidth(result);

        // TODO:
        // Add another if for truncating to just a few letters if needed.
      } else {
        result = fileName;
        resultWidth = g.getFontMetrics(font).stringWidth(result);
      }

      g.drawString(
          result,
          getWidth() / 2 + imageSize.width / 2 - resultWidth - 5,
          getHeight() / 2 + imageSize.height / 2 - 5);
    } else g.drawImage(image, 0, 0, this);
  }