public void paint(Graphics g, JComponent c) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); String text = layout(b, SwingUtilities2.getFontMetrics(b, g), b.getWidth(), b.getHeight()); clearTextShiftOffset(); // perform UI specific press action, e.g. Windows L&F shifts text if (model.isArmed() && model.isPressed()) { paintButtonPressed(g, b); } // Paint the Icon if (b.getIcon() != null) { paintIcon(g, c, iconRect); } if (text != null && !text.equals("")) { View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, textRect); } else { paintText(g, b, textRect, text); } } if (b.isFocusPainted() && b.hasFocus()) { // paint UI specific focus paintFocus(g, b, viewRect, textRect, iconRect); } }
protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); Icon icon = b.getIcon(); Icon tmpIcon = null; if (icon == null) { return; } Icon selectedIcon = null; /* the fallback icon should be based on the selected state */ if (model.isSelected()) { selectedIcon = (Icon) b.getSelectedIcon(); if (selectedIcon != null) { icon = selectedIcon; } } if (!model.isEnabled()) { if (model.isSelected()) { tmpIcon = (Icon) b.getDisabledSelectedIcon(); if (tmpIcon == null) { tmpIcon = selectedIcon; } } if (tmpIcon == null) { tmpIcon = (Icon) b.getDisabledIcon(); } } else if (model.isPressed() && model.isArmed()) { tmpIcon = (Icon) b.getPressedIcon(); if (tmpIcon != null) { // revert back to 0 offset clearTextShiftOffset(); } } else if (b.isRolloverEnabled() && model.isRollover()) { if (model.isSelected()) { tmpIcon = (Icon) b.getRolloverSelectedIcon(); if (tmpIcon == null) { tmpIcon = selectedIcon; } } if (tmpIcon == null) { tmpIcon = (Icon) b.getRolloverIcon(); } } if (tmpIcon != null) { icon = tmpIcon; } if (model.isPressed() && model.isArmed()) { icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(), iconRect.y + getTextShiftOffset()); } else { icon.paintIcon(c, g, iconRect.x, iconRect.y); } }
private String layout(AbstractButton b, FontMetrics fm, int width, int height) { Insets i = b.getInsets(); viewRect.x = i.left; viewRect.y = i.top; viewRect.width = width - (i.right + viewRect.x); viewRect.height = height - (i.bottom + viewRect.y); textRect.x = textRect.y = textRect.width = textRect.height = 0; iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0; // layout the text and icon return SwingUtilities.layoutCompoundLabel( b, fm, b.getText(), b.getIcon(), b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect, b.getText() == null ? 0 : b.getIconTextGap()); }
public Rectangle getVisualBounds(JComponent c, int type, int width, int height) { Rectangle bounds = new Rectangle(0, 0, width, height); if (type == VisuallyLayoutable.CLIP_BOUNDS) { return bounds; } AbstractButton b = (AbstractButton) c; if (type == VisuallyLayoutable.COMPONENT_BOUNDS && b.getBorder() != null && b.isBorderPainted()) { Border border = b.getBorder(); if (border instanceof BackgroundBorder) { border = ((BackgroundBorder) border).getBackgroundBorder(); if (border instanceof VisualMargin) { InsetsUtil.subtractInto(((VisualMargin) border).getVisualMargin(c), bounds); } else if (border instanceof QuaquaButtonBorder) { InsetsUtil.subtractInto(((QuaquaButtonBorder) border).getVisualMargin(c), bounds); } } return bounds; } String text = b.getText(); boolean isEmpty = (text == null || text.length() == 0); if (isEmpty) { text = " "; } Icon icon = (b.isEnabled()) ? b.getIcon() : b.getDisabledIcon(); if ((icon == null) && (text == null)) { return null; } FontMetrics fm = c.getFontMetrics(c.getFont()); Insets insets = c.getInsets(viewInsets); viewR.x = insets.left; viewR.y = insets.top; viewR.width = width - (insets.left + insets.right); viewR.height = height - (insets.top + insets.bottom); iconR.x = iconR.y = iconR.width = iconR.height = 0; textR.x = textR.y = textR.width = textR.height = 0; String clippedText = layoutCL(b, fm, text, icon, viewR, iconR, textR); Rectangle textBounds = Fonts.getPerceivedBounds(text, c.getFont(), c); if (isEmpty) { textBounds.width = 0; } int ascent = fm.getAscent(); textR.x += textBounds.x; textR.width = textBounds.width; textR.y += ascent + textBounds.y; textR.height -= fm.getHeight() - textBounds.height; bounds.setBounds(textR); return bounds; }
/** * Returns the Icon used in calculating the preferred/minimum/maximum size. * * @param b specifies the {@code AbstractButton} used when calculating the * preferred/minimum/maximum size. * @return the Icon used in calculating the preferred/minimum/maximum size. */ protected Icon getSizingIcon(AbstractButton b) { Icon icon = getEnabledIcon(b, b.getIcon()); if (icon == null) { icon = getDefaultIcon(b); } return icon; }
@Override public Dimension getMinimumSize(JComponent c) { AbstractButton b = (AbstractButton) c; String style = (String) c.getClientProperty("Quaqua.Button.style"); if (style == null) { style = "push"; } if (style.equals("help")) { return getPreferredSize(c); } Dimension d = super.getMinimumSize(c); if (isFixedHeight(c)) { Dimension p = getPreferredSize(c); if (d != null && p != null) { d.height = Math.max(d.height, p.height); } } if (!QuaquaUtilities.isSmallSizeVariant(c) && style.equals("push") // && b.getIcon() == null && b.getText() != null) { if (d != null) { d.width = Math.max(d.width, UIManager.getInt("Button.minimumWidth")); } } return d; }
@Override protected void paintIcon(Graphics g, JComponent c, Rectangle iconRect) { AbstractButton b = (AbstractButton) c; ButtonModel model = b.getModel(); Icon icon = b.getIcon(); Icon tmpIcon = null; Icon shadowIcon = null; boolean borderHasPressedCue = borderHasPressedCue(b); if (icon == null) { return; } if (!model.isEnabled()) { if (model.isSelected()) { tmpIcon = (Icon) b.getDisabledSelectedIcon(); } else { tmpIcon = (Icon) b.getDisabledIcon(); } } else if (model.isPressed() && model.isArmed()) { tmpIcon = (Icon) b.getPressedIcon(); if (tmpIcon != null) { // revert back to 0 offset clearTextShiftOffset(); } else if (icon != null && icon instanceof ImageIcon && !borderHasPressedCue) { // Create an icon on the fly. // Note: This is only needed for borderless buttons, which // have no other way to provide feedback about the pressed // state. tmpIcon = new ImageIcon(HalfbrightFilter.createHalfbrightImage(((ImageIcon) icon).getImage())); shadowIcon = new ImageIcon(ShadowFilter.createShadowImage(((ImageIcon) icon).getImage())); } } else if (b.isRolloverEnabled() && model.isRollover()) { if (model.isSelected()) { tmpIcon = b.getRolloverSelectedIcon(); if (tmpIcon == null) { tmpIcon = b.getSelectedIcon(); } } else { tmpIcon = (Icon) b.getRolloverIcon(); } } else if (model.isSelected()) { tmpIcon = b.getSelectedIcon(); } if (tmpIcon != null) { icon = tmpIcon; } if (model.isPressed() && model.isArmed()) { if (shadowIcon != null) { shadowIcon.paintIcon( c, g, iconRect.x + getTextShiftOffset(), iconRect.y + getTextShiftOffset() + 1); } icon.paintIcon(c, g, iconRect.x + getTextShiftOffset(), iconRect.y + getTextShiftOffset()); } else { icon.paintIcon(c, g, iconRect.x, iconRect.y); } }
/** Returns the baseline for buttons. */ private static int getButtonBaseline(AbstractButton button, int height) { FontMetrics fm = button.getFontMetrics(button.getFont()); resetRects(button, height); String text = button.getText(); if (text != null && text.startsWith("<html>")) { return -1; } // NOTE: that we use "a" here to make sure we get a valid value, if // we were to pass in an empty string or null we would not get // back the right thing. SwingUtilities.layoutCompoundLabel( button, fm, "a", button.getIcon(), button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), viewRect, iconRect, textRect, text == null ? 0 : button.getIconTextGap()); if (isAqua()) { return textRect.y + fm.getAscent() + 1; } return textRect.y + fm.getAscent(); }
public Transferable createTransferable(JComponent comp) { // Clear image = null; Icon icon = null; if (comp instanceof JLabel) { JLabel label = (JLabel) comp; icon = label.getIcon(); } else if (comp instanceof AbstractButton) { AbstractButton button = (AbstractButton) comp; icon = button.getIcon(); } if (icon instanceof ImageIcon) { image = ((ImageIcon) icon).getImage(); return this; } return null; }
/** Returns the dimensions required to display the icon and label. */ private Dimension getContentSize(AbstractButton button) { Rectangle scratchIconRect = new Rectangle(); Rectangle scratchTextRect = new Rectangle(); Rectangle scratchViewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); FontMetrics fm = button.getFontMetrics(button.getFont()); SwingUtilities.layoutCompoundLabel( fm, button.getText(), button.getIcon(), button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), scratchViewRect, scratchIconRect, scratchTextRect, button.getIconTextGap()); Insets textInsets = getTextPadding(); scratchTextRect.y -= textInsets.top; scratchTextRect.x -= textInsets.left; scratchTextRect.width += textInsets.left + textInsets.right; scratchTextRect.height += textInsets.top + textInsets.bottom; Insets iconInsets = getIconPadding(); scratchIconRect.y -= iconInsets.top; scratchIconRect.x -= iconInsets.left; scratchIconRect.width += iconInsets.left + iconInsets.right; scratchIconRect.height += iconInsets.top + iconInsets.bottom; Rectangle sum = getSum(new Rectangle[] {scratchIconRect, scratchTextRect}); return new Dimension(sum.width, sum.height); }
/** {@inheritDoc} */ @Override public int getBaseline(JComponent c, int width, int height) { if (c == null) { throw new NullPointerException("Component must be non-null"); } if (width < 0 || height < 0) { throw new IllegalArgumentException("Width and height must be >= 0"); } AbstractButton b = (AbstractButton) c; String text = b.getText(); if (text == null || "".equals(text)) { return -1; } Insets i = b.getInsets(); Rectangle viewRect = new Rectangle(); Rectangle textRect = new Rectangle(); Rectangle iconRect = new Rectangle(); viewRect.x = i.left; viewRect.y = i.top; viewRect.width = width - (i.right + viewRect.x); viewRect.height = height - (i.bottom + viewRect.y); // layout the text and icon SynthContext context = getContext(b); FontMetrics fm = context.getComponent().getFontMetrics(context.getStyle().getFont(context)); context .getStyle() .getGraphicsUtils(context) .layoutText( context, fm, b.getText(), b.getIcon(), b.getHorizontalAlignment(), b.getVerticalAlignment(), b.getHorizontalTextPosition(), b.getVerticalTextPosition(), viewRect, iconRect, textRect, b.getIconTextGap()); View view = (View) b.getClientProperty(BasicHTML.propertyKey); int baseline; if (view != null) { baseline = BasicHTML.getHTMLBaseline(view, textRect.width, textRect.height); if (baseline >= 0) { baseline += textRect.y; } } else { baseline = textRect.y + fm.getAscent(); } return baseline; }
/** * Returns the Icon to use for painting the button. The icon is chosen with respect to the current * state of the button. * * @param b button the icon is associated with * @return an icon */ protected Icon getIcon(AbstractButton b) { Icon icon = b.getIcon(); ButtonModel model = b.getModel(); if (!model.isEnabled()) { icon = getSynthDisabledIcon(b, icon); } else if (model.isPressed() && model.isArmed()) { icon = getPressedIcon(b, getSelectedIcon(b, icon)); } else if (b.isRolloverEnabled() && model.isRollover()) { icon = getRolloverIcon(b, getSelectedIcon(b, icon)); } else if (model.isSelected()) { icon = getSelectedIcon(b, icon); } else { icon = getEnabledIcon(b, icon); } if (icon == null) { return getDefaultIcon(b); } return icon; }
public void paintIcon(Graphics2D g, ButtonInfo info) { AbstractButton button = info.button; Icon icon = button.getIcon(); ButtonModel model = button.getModel(); if (model.isRollover() && button.getRolloverIcon() != null) icon = button.getRolloverIcon(); if (model.isPressed() && button.getPressedIcon() != null) icon = button.getPressedIcon(); if (model.isSelected() && button.getSelectedIcon() != null) icon = button.getSelectedIcon(); if (model.isRollover() && model.isSelected() && button.getRolloverSelectedIcon() != null) icon = button.getRolloverSelectedIcon(); if (isEnabled(button) == false && button.getDisabledIcon() != null) icon = button.getDisabledIcon(); if (isEnabled(button) == false && model.isSelected() && button.getDisabledIcon() != null) icon = button.getDisabledSelectedIcon(); if (icon != null) { g.setComposite(isEnabled(button) ? AlphaComposite.SrcOver : SRC_OVER_TRANSLUCENT); icon.paintIcon(button, g, info.iconRect.x, info.iconRect.y); } }
@Override public void installUI(JComponent c) { AbstractButton button = (AbstractButton) c; ButtonInfo info = new ButtonInfo(button, this); button.putClientProperty(BUTTON_INFO_KEY, info); button.addMouseListener(info.basicListener); button.addMouseMotionListener(info.basicListener); button.addFocusListener(info.basicListener); button.addPropertyChangeListener(info.basicListener); button.addChangeListener(info.basicListener); button.addKeyListener(focusArrowListener); button.addComponentListener(componentListener); button.addKeyListener(keyArmingListener); button.setRequestFocusEnabled(false); button.setFocusable(true); button.addPropertyChangeListener(positionAndShapeListener); button.setOpaque(false); button.setRolloverEnabled(true); if (button.getIcon() != null) { Font font = UIManager.getFont("IconButton.font"); if (font != null) button.setFont(font); // miniature-ish } super.installUI(c); updateLayout(button, info); if (button.getFont() == null) { Font font = UIManager.getFont("Button.font"); if (font == null) { font = new Font("Default", 0, 13); } button.setFont(font); } }
protected void paintFocus( Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) { Rectangle focusRect = new Rectangle(); String text = b.getText(); boolean isIcon = b.getIcon() != null; // If there is text if (text != null && !text.equals("")) { if (!isIcon) { focusRect.setBounds(textRect); } else { focusRect.setBounds(iconRect.union(textRect)); } } // If there is an icon and no text else if (isIcon) { focusRect.setBounds(iconRect); } g.setColor(getFocusColor()); g.drawRect((focusRect.x - 1), (focusRect.y - 1), focusRect.width + 1, focusRect.height + 1); }
public static Dimension getPreferredSize(AbstractButton b) { String style = (String) b.getClientProperty("Quaqua.Button.style"); if (style == null) { style = "push"; } if (style.equals("help")) { Icon helpIcon = UIManager.getIcon("Button.helpIcon"); Insets insets = b.getInsets(); return new Dimension( helpIcon.getIconWidth() + insets.left + insets.right, helpIcon.getIconHeight() + insets.top + insets.bottom); } if (b.getComponentCount() > 0) { return null; } int textIconGap = Methods.invokeGetter(b, "getIconTextGap", 4); Icon icon = (Icon) b.getIcon(); String text = b.getText(); Font font = b.getFont(); FontMetrics fm = b.getFontMetrics(font); viewR.x = viewR.y = 0; viewR.width = Short.MAX_VALUE; viewR.height = Short.MAX_VALUE; iconR.x = iconR.y = iconR.width = iconR.height = 0; textR.x = textR.y = textR.width = textR.height = 0; SwingUtilities.layoutCompoundLabel( (JComponent) b, fm, text, icon, b.getVerticalAlignment(), b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewR, iconR, textR, (text == null ? 0 : textIconGap)); /* The preferred size of the button is the size of * the text and icon rectangles plus the buttons insets. */ Rectangle r = iconR.union(textR); // if (b.isBorderPainted()) { Insets insets = b.getInsets(); r.width += insets.left + insets.right; r.height += insets.top + insets.bottom; // } if (!QuaquaUtilities.isSmallSizeVariant(b) && style.equals("push") && b.getIcon() == null && b.getText() != null) { r.width = Math.max(r.width, UIManager.getInt("Button.minimumWidth")); } return r.getSize(); }
/** Calculates the preferred size of this button and UI. */ @Override public Dimension getPreferredSize(JComponent c) { AbstractButton button = (AbstractButton) c; ButtonCluster cluster = ButtonCluster.getCluster(button); Rectangle scratchIconRect = new Rectangle(); Rectangle scratchTextRect = new Rectangle(); Rectangle scratchViewRect = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE); FontMetrics fm = button.getFontMetrics(button.getFont()); SwingUtilities.layoutCompoundLabel( fm, button.getText(), button.getIcon(), button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), scratchViewRect, scratchIconRect, scratchTextRect, button.getIconTextGap()); Insets textInsets = getTextPadding(); scratchTextRect.y -= textInsets.top; scratchTextRect.x -= textInsets.left; scratchTextRect.width += textInsets.left + textInsets.right; scratchTextRect.height += textInsets.top + textInsets.bottom; Insets iconInsets = getIconPadding(); scratchIconRect.y -= iconInsets.top; scratchIconRect.x -= iconInsets.left; scratchIconRect.width += iconInsets.left + iconInsets.right; scratchIconRect.height += iconInsets.top + iconInsets.bottom; Rectangle sum = getSum(new Rectangle[] {scratchIconRect, scratchTextRect}); if (cluster != null && cluster.isStandardized()) { /** * If standardize: the dimensions of this button need to make room for all other buttons in * the cluster. */ AbstractButton[] buttons = cluster.getButtons(); for (int a = 0; a < buttons.length; a++) { ButtonUI ui = buttons[a].getUI(); if (ui instanceof FilledButtonUI) { FilledButtonUI fui = (FilledButtonUI) ui; Dimension contentSize = fui.getContentSize(buttons[a]); sum.width = Math.max(sum.width, contentSize.width); sum.height = Math.max(sum.height, contentSize.height); } } } Insets padding = getContentInsets(button); Shape customShape = (Shape) button.getClientProperty(SHAPE); if (customShape == null) { int minHeight = getPreferredHeight(); if (sum.height < minHeight) sum.height = minHeight; } int horizontalPosition = getHorizontalPosition(button); int verticalPosition = getVerticalPosition(button); Dimension size = shape.getPreferredSize(null, sum.width, sum.height, padding, customShape); if (customShape == null) { PaintFocus focus = getFocusPainting(button); if (focus == PaintFocus.OUTSIDE || focus == PaintFocus.BOTH) { if (horizontalPosition == POS_ONLY) { size.width += 2 * focusSize; } else if (horizontalPosition != POS_MIDDLE) { size.width += focusSize; } if (verticalPosition == POS_ONLY) { size.height += 2 * focusSize; } else if (horizontalPosition != POS_MIDDLE) { size.height += focusSize; } } } return size; }
@Override public void paint(Graphics g, JComponent c) { if (!(c.getBorder() instanceof MacIntelliJButtonBorder) && !isComboButton(c)) { super.paint(g, c); return; } int w = c.getWidth(); int h = c.getHeight(); if (isHelpButton(c)) { Icon icon = MacIntelliJIconCache.getIcon("helpButton", false, c.hasFocus()); int x = (w - icon.getIconWidth()) / 2; int y = (h - icon.getIconHeight()) / 2; icon.paintIcon(c, g, x, y); } else { AbstractButton b = (AbstractButton) c; String text = layout(b, SwingUtilities2.getFontMetrics(b, g), b.getWidth(), b.getHeight()); boolean isFocused = c.hasFocus(); if (isSquare(c)) { Icon icon = MacIntelliJIconCache.getIcon("browseButton"); int x = (c.getWidth() - icon.getIconWidth()) / 2; int y = (c.getHeight() - icon.getIconHeight()) / 2; icon.paintIcon(c, g, x, y); return; } else { int x = isFocused ? 0 : 2; int y = isFocused ? 0 : (h - viewRect.height) / 2; Icon icon; icon = getLeftIcon(b); icon.paintIcon(b, g, x, y); x += icon.getIconWidth(); int stop = w - (isFocused ? 0 : 2) - (getRightIcon(b).getIconWidth()); Graphics gg = g.create(0, 0, w, h); gg.setClip(x, y, stop - x, h); icon = getMiddleIcon(b); while (x < stop) { icon.paintIcon(b, gg, x, y); x += icon.getIconWidth(); } gg.dispose(); icon = getRightIcon(b); icon.paintIcon(b, g, stop, y); clearTextShiftOffset(); } // Paint the Icon if (b.getIcon() != null) { paintIcon(g, c, iconRect); } if (text != null && !text.isEmpty()) { View v = (View) c.getClientProperty(BasicHTML.propertyKey); if (v != null) { v.paint(g, textRect); } else { UISettings.setupAntialiasing(g); paintText(g, b, textRect, text); } } } }
protected void updateLayout(AbstractButton button, ButtonInfo buttonInfo) { Shape customShape = (Shape) button.getClientProperty(SHAPE); int width = button.getWidth(); int height = button.getHeight(); int horizontalPosition = getHorizontalPosition(button); int verticalPosition = getVerticalPosition(button); String key = width + " " + height + " " + horizontalPosition + " " + verticalPosition; button.putClientProperty("FilledButtonUI.validationKey", key); int dx = 0; int dy = 0; if (getFocusPainting(button) == PaintFocus.OUTSIDE || getFocusPainting(button) == PaintFocus.BOTH) { if (horizontalPosition == POS_LEFT || horizontalPosition == POS_ONLY) { dx += focusSize; width -= focusSize; } if (horizontalPosition == POS_RIGHT || horizontalPosition == POS_ONLY) { width -= focusSize; } if (verticalPosition == POS_TOP || verticalPosition == POS_ONLY) { dy += focusSize; height -= focusSize; } if (verticalPosition == POS_BOTTOM || verticalPosition == POS_ONLY) { height -= focusSize; } } else { if ((verticalPosition == POS_BOTTOM || verticalPosition == POS_ONLY) && fill.getShadowHighlight(button) != null) { height--; } } ButtonInfo info = getButtonInfo(button); boolean showSeparators = isShowingSeparators(button); shape.getShape( info.fill, info.stroke, width, height, horizontalPosition, verticalPosition, showSeparators, customShape); AffineTransform translation = AffineTransform.getTranslateInstance(dx, dy); info.fill.transform(translation); info.stroke.transform(translation); Font font = button.getFont(); if (font == null) font = new Font("Default", 0, 12); FontMetrics fm = button.getFontMetrics(font); info.viewRect.x = info.viewRect.y = info.textRect.x = info.textRect.y = info.textRect.width = info.textRect.height = 0; info.iconRect.x = info.iconRect.y = info.iconRect.width = info.iconRect.height = 0; info.viewRect.width = Short.MAX_VALUE; info.viewRect.height = Short.MAX_VALUE; SwingUtilities.layoutCompoundLabel( fm, button.getText(), button.getIcon(), button.getVerticalAlignment(), button.getHorizontalAlignment(), button.getVerticalTextPosition(), button.getHorizontalTextPosition(), info.viewRect, info.iconRect, info.textRect, button.getIconTextGap()); Insets textInsets = getTextPadding(); Insets iconInsets = getIconPadding(); Rectangle tempTextRect = new Rectangle(info.textRect); Rectangle tempIconRect = new Rectangle(info.iconRect); if (info.textRect.width > 0) { tempTextRect.y -= textInsets.top; tempTextRect.x -= textInsets.left; tempTextRect.width += textInsets.left + textInsets.right; tempTextRect.height += textInsets.top + textInsets.bottom; } if (info.iconRect.width > 0) { tempIconRect.y -= iconInsets.top; tempIconRect.x -= iconInsets.left; tempIconRect.width += iconInsets.left + iconInsets.right; tempIconRect.height += iconInsets.top + iconInsets.bottom; } Rectangle sum = getSum(new Rectangle[] {tempIconRect, tempTextRect}); Insets padding = getContentInsets(button); float centerX, centerY; if (button.getHorizontalAlignment() == SwingConstants.LEFT || button.getHorizontalAlignment() == SwingConstants.LEADING) { centerX = padding.left + sum.width / 2; } else if (button.getHorizontalAlignment() == SwingConstants.RIGHT || button.getHorizontalAlignment() == SwingConstants.TRAILING) { centerX = button.getWidth() - padding.right - sum.width / 2; } else { centerX = ((button.getWidth() - padding.left - padding.right)) / 2f; } // TODO: also take into account vertical alignment: centerY = ((button.getHeight() - padding.top - padding.bottom)) / 2f; float shiftX = centerX - (sum.width) / 2f - sum.x + padding.left; float shiftY = centerY - (sum.height) / 2f - sum.y + padding.top; if (customShape == null) { if (button.getVerticalAlignment() == SwingConstants.CENTER && button.getVerticalTextPosition() == SwingConstants.CENTER && info.textRect.width > 0) { int unusedAscent = getUnusedAscent(fm, font); int ascent = fm.getAscent() - unusedAscent; shiftY = (int) (-sum.y + centerY - ascent / 2 - unusedAscent + padding.top - textInsets.top); } } info.iconRect.setFrame( info.iconRect.x + shiftX, info.iconRect.y + shiftY, info.iconRect.width, info.iconRect.height); info.textRect.setRect( (int) (info.textRect.x + shiftX + .5f), (int) (info.textRect.y + shiftY + .5f), info.textRect.width, info.textRect.height); info.updateFillBounds(); }