public void rotate(int deg) { log.debug("rotate({}) with _rotateText {}, _text {}, _icon {}", deg, _rotateText, _text, _icon); _degrees = deg; if (_rotateText || deg == 0) { if (deg == 0) { // restore unrotated whatever _rotateText = false; if (_text) { log.debug(" super.setText(\"{}\");", _unRotatedText); super.setText(_unRotatedText); if (_popupUtil != null) { setOpaque(_popupUtil.hasBackground()); _popupUtil.setBorder(true); } if (_icon) { String url = _namedIcon.getURL(); _namedIcon = new NamedIcon(url, url); } else { _namedIcon = null; } super.setIcon(_namedIcon); } else { _namedIcon.rotate(deg, this); super.setIcon(_namedIcon); } } else { if (_text & _icon) { // update text over icon _namedIcon = makeTextOverlaidIcon(_unRotatedText, _namedIcon); } else if (_text) { // update text only icon image _namedIcon = makeTextIcon(_unRotatedText); } _namedIcon.rotate(deg, this); super.setIcon(_namedIcon); setOpaque(false); // rotations cannot be opaque } } else { if (deg != 0) { // first time text or icon is rotated from horizontal if (_text && _icon) { // text overlays icon e.g. LocoIcon _namedIcon = makeTextOverlaidIcon(_unRotatedText, _namedIcon); super.setText(null); _rotateText = true; setOpaque(false); } else if (_text) { _namedIcon = makeTextIcon(_unRotatedText); super.setText(null); _rotateText = true; setOpaque(false); } if (_popupUtil != null) { _popupUtil.setBorder(false); } _namedIcon.rotate(deg, this); super.setIcon(_namedIcon); } else if (_namedIcon != null) { _namedIcon.rotate(deg, this); super.setIcon(_namedIcon); } } updateSize(); }
/** * create a text image whose bit map can be rotated * * @param text * @return */ private NamedIcon makeTextIcon(String text) { if (text == null || text.equals("")) { text = " "; } int width = getFontMetrics(getFont()).stringWidth(text); int height = getFontMetrics(getFont()).getHeight(); int hOffset = 0; int vOffset = getFontMetrics(getFont()).getAscent(); 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 + 2, height + 2, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = bufIm.createGraphics(); g2d.setFont(getFont()); 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.setColor(getForeground()); g2d.drawString(text, hOffset, vOffset); NamedIcon icon = new NamedIcon(bufIm); g2d.dispose(); return icon; }
/** 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; }