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; }
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); }
/** * Paints the border for the specified component with the specified position and size. * * @param c the component for which this border is being painted * @param g the paint graphics * @param x the x position of the painted border * @param y the y position of the painted border * @param width the width of the painted border * @param height the height of the painted border */ public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { Border border = getBorder(); if (label == null) { if (border != null) { border.paintBorder(c, g, x, y, width, height); } return; } Rectangle grooveRect = new Rectangle( x + EDGE_SPACING, y + EDGE_SPACING, width - (EDGE_SPACING * 2), height - (EDGE_SPACING * 2)); Dimension labelDim = label.getPreferredSize(); int baseline = label.getBaseline(labelDim.width, labelDim.height); int ascent = Math.max(0, baseline); int descent = labelDim.height - ascent; int diff; Insets insets; if (border != null) { insets = border.getBorderInsets(c); } else { insets = new Insets(0, 0, 0, 0); } diff = Math.max(0, ascent / 2 + TEXT_SPACING - EDGE_SPACING); grooveRect.y += diff; grooveRect.height -= diff; compLoc.y = grooveRect.y + insets.top / 2 - (ascent + descent) / 2 - 1; int justification; if (c.getComponentOrientation().isLeftToRight()) { justification = LEFT; } else { justification = RIGHT; } switch (justification) { case LEFT: compLoc.x = grooveRect.x + TEXT_INSET_H + insets.left; break; case RIGHT: compLoc.x = (grooveRect.x + grooveRect.width - (labelDim.width + TEXT_INSET_H + insets.right)); break; } // If title is positioned in middle of border AND its fontsize // is greater than the border's thickness, we'll need to paint // the border in sections to leave space for the component's background // to show through the title. // if (border != null) { if (grooveRect.y > compLoc.y - ascent) { Rectangle clipRect = new Rectangle(); // save original clip Rectangle saveClip = g.getClipBounds(); // paint strip left of text clipRect.setBounds(saveClip); if (computeIntersection(clipRect, x, y, compLoc.x - 1 - x, height)) { g.setClip(clipRect); border.paintBorder( c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height); } // paint strip right of text clipRect.setBounds(saveClip); if (computeIntersection( clipRect, compLoc.x + labelDim.width + 1, y, x + width - (compLoc.x + labelDim.width + 1), height)) { g.setClip(clipRect); border.paintBorder( c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height); } // paint strip below text clipRect.setBounds(saveClip); if (computeIntersection( clipRect, compLoc.x - 1, compLoc.y + ascent + descent, labelDim.width + 2, y + height - compLoc.y - ascent - descent)) { g.setClip(clipRect); border.paintBorder( c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height); } // restore clip g.setClip(saveClip); } else { border.paintBorder(c, g, grooveRect.x, grooveRect.y, grooveRect.width, grooveRect.height); } label.setLocation(compLoc); label.setSize(labelDim); } }