コード例 #1
0
 public static NamedIcon cloneIcon(NamedIcon icon, PositionableLabel pos) {
   if (icon.getURL() != null) {
     return new NamedIcon(icon, pos);
   } else {
     NamedIcon clone = new NamedIcon(icon.getImage());
     clone.scale(icon.getScale(), pos);
     clone.rotate(icon.getDegrees(), pos);
     return clone;
   }
 }
コード例 #2
0
  /** Create an image of icon with text overlaid */
  protected NamedIcon makeTextOverlaidIcon(String text, NamedIcon ic) {
    String url = ic.getURL();
    NamedIcon icon = new NamedIcon(url, url);
    int textWidth = getFontMetrics(getFont()).stringWidth(text);
    int iconWidth = icon.getIconWidth();
    int textHeight = getFontMetrics(getFont()).getHeight();
    int iconHeight = icon.getIconHeight();

    int width = Math.max(textWidth, iconWidth);
    int height = Math.max(textHeight, iconHeight);
    int hOffset = Math.max((textWidth - iconWidth) / 2, 0);
    int vOffset = Math.max((textHeight - iconHeight) / 2, 0);

    if (_popupUtil != null) {
      if (_popupUtil.getFixedWidth() != 0) {
        switch (_popupUtil.getJustification()) {
          case PositionablePopupUtil.LEFT:
            hOffset = _popupUtil.getBorderSize();
            break;
          case PositionablePopupUtil.RIGHT:
            hOffset = _popupUtil.getFixedWidth() - width;
            hOffset += _popupUtil.getBorderSize();
            break;
          default:
            hOffset = Math.max((_popupUtil.getFixedWidth() - width) / 2, 0);
            hOffset += _popupUtil.getBorderSize();
            break;
        }
        width = _popupUtil.getFixedWidth() + 2 * _popupUtil.getBorderSize();
      } else {
        width += 2 * (_popupUtil.getMargin() + _popupUtil.getBorderSize());
        hOffset += _popupUtil.getMargin() + _popupUtil.getBorderSize();
      }
      if (_popupUtil.getFixedHeight() != 0) {
        vOffset = Math.max(vOffset + (_popupUtil.getFixedHeight() - height) / 2, 0);
        vOffset += _popupUtil.getBorderSize();
        height = _popupUtil.getFixedHeight() + 2 * _popupUtil.getBorderSize();
      } else {
        height += 2 * (_popupUtil.getMargin() + _popupUtil.getBorderSize());
        vOffset += _popupUtil.getMargin() + _popupUtil.getBorderSize();
      }
    }

    BufferedImage bufIm = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = bufIm.createGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(
        RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
    g2d.setRenderingHint(
        RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    if (_popupUtil != null) {
      if (_popupUtil.hasBackground()) {
        g2d.setColor(_popupUtil.getBackground());
        g2d.fillRect(0, 0, width, height);
      }
      if (_popupUtil.getBorderSize() != 0) {
        g2d.setColor(_popupUtil.getBorderColor());
        g2d.setStroke(new java.awt.BasicStroke(2 * _popupUtil.getBorderSize()));
        g2d.drawRect(0, 0, width, height);
      }
    }

    g2d.drawImage(
        icon.getImage(), AffineTransform.getTranslateInstance(hOffset, vOffset + 1), this);
    g2d.setFont(getFont());

    hOffset = Math.max((width - textWidth) / 2, 0);
    vOffset = Math.max((height - textHeight) / 2, 0) + getFontMetrics(getFont()).getAscent();
    g2d.setColor(getForeground());
    g2d.drawString(text, hOffset, vOffset);

    icon = new NamedIcon(bufIm);
    g2d.dispose();
    icon.setURL(url);
    return icon;
  }