下面列出了javax.swing.AbstractButton#isFocusPainted ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected Insets prepareButton(AbstractButton b) {
if (b.isFocusPainted()) {
// if painting the focus makes a button larger: then we need
// to acknowledge that so the getInsets method still
// right-aligns our labels and checkboxes correctly.
Dimension d1 = b.getPreferredSize();
b.setFocusPainted(false);
Dimension d2 = b.getPreferredSize();
b.setFocusPainted(true);
Insets negativeInsets = new Insets(0, 0, d2.width - d1.width,
d2.height - d1.height);
b.putClientProperty(PROPERTY_NEGATIVE_INSETS, negativeInsets);
return negativeInsets;
}
return null;
}
@Override
protected void defineShapes(JComponent jc, AbstractButton button,
Float state, Graphics2D g, int x, int y, int width, int height,
GeneralPath fillShape, GeneralPath strokeShape,
GeneralPath paintedPartitions, GeneralPath focusedPartitions) {
boolean isFocusPainted = button == null || button.isFocusPainted();
if (paintFocus == PaintFocus.OUTSIDE && isFocusPainted) {
x += focusRingSize;
y += focusRingSize;
width -= 2 * focusRingSize;
height -= 2 * focusRingSize;
}
Ellipse2D circle = new Ellipse2D.Float(x, y, width - 1, height - 1);
fillShape.append(circle, false);
strokeShape.append(circle, false);
}
@Override
public Dimension getPreferredSize(JComponent c) {
Dimension d = super.getPreferredSize(c);
/* Ensure that the width and height of the button is odd,
* to allow for the focus line if focus is painted
*/
AbstractButton b = (AbstractButton)c;
if (b.isFocusPainted()) {
if(d.width % 2 == 0) { d.width += 1; }
if(d.height % 2 == 0) { d.height += 1; }
}
return d;
}
public Dimension getPreferredSize(JComponent c) {
Dimension d = super.getPreferredSize(c);
/* Ensure that the width and height of the button is odd,
* to allow for the focus line if focus is painted
*/
AbstractButton b = (AbstractButton)c;
if (d != null && b.isFocusPainted()) {
if(d.width % 2 == 0) { d.width += 1; }
if(d.height % 2 == 0) { d.height += 1; }
}
return d;
}
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
String text = layout(b, g.getFontMetrics(), b.getWidth(), b.getHeight());
clearTextShiftOffset();
if (model.isArmed() && model.isPressed()) {
paintButtonPressed(g, b);
} else if (b.isRolloverEnabled() && model.isRollover()) {
paintButtonPressed(g, b);
}
if (b.getIcon() != null) {
paintIcon(g, c, iconRect);
}
if (b.isFocusPainted() && b.isFocusOwner()) {
paintFocus(g, b, viewRect, textRect, iconRect);
if (iconRect != null && iconRect.width > 0 && iconRect.height > 0) {
if (b.getIcon() != null) {
paintIcon(g, c, iconRect);
}
}
}
if (text != null && !text.equals("")) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g2, textRect);
} else {
paintText(g2, b, textRect, text);
}
}
}
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton)c;
ButtonModel model = b.getModel();
FontMetrics fm = g.getFontMetrics();
Insets i = c.getInsets();
viewRect.x = i.left;
viewRect.y = i.top;
viewRect.width = b.getWidth() - (i.right + viewRect.x);
viewRect.height = b.getHeight() - (i.bottom + viewRect.y);
textRect.x = textRect.y = textRect.width = textRect.height = 0;
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
Font f = c.getFont();
g.setFont(f);
// layout the text and icon
String text =
SwingUtilities.layoutCompoundLabel(
c,
fm,
b.getText(),
b.getIcon(),
b.getVerticalAlignment(),
b.getHorizontalAlignment(),
b.getVerticalTextPosition(),
b.getHorizontalTextPosition(),
viewRect,
iconRect,
textRect,
b.getText() == null ? 0 : b.getIconTextGap());
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);
}
Composite oldComposite = ((Graphics2D)g).getComposite();
if (model.isRollover()) {
((Graphics2D)g).setComposite(
AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
}
if (text != null && !text.equals("")) {
View v = (View)c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
textRect.x += getTextShiftOffset();
textRect.y += getTextShiftOffset();
v.paint(g, textRect);
textRect.x -= getTextShiftOffset();
textRect.y -= getTextShiftOffset();
} else {
paintText(g, b, textRect, text);
}
}
if (b.isFocusPainted() && b.hasFocus()) {
// paint UI specific focus
paintFocus(g, b, viewRect, textRect, iconRect);
}
((Graphics2D)g).setComposite(oldComposite);
}
@Override
public Insets getBorderInsets(Component c) {
JComponent jc = (JComponent) c;
AbstractButton button = (jc instanceof AbstractButton) ? (AbstractButton) jc
: null;
Insets i = new Insets(0, 0, 0, 0);
boolean isFocusPainted = button == null || button.isFocusPainted();
if (paintFocus == PaintFocus.OUTSIDE && isFocusPainted) {
i.top += focusRingSize;
i.left += focusRingSize;
i.bottom += focusRingSize;
i.right += focusRingSize;
}
if (buttonFill.getShadowHighlight(new ButtonState.Float(0, 0, 0, 0,
0)) != null) {
i.bottom = Math.max(1, i.bottom);
}
// 1 pixel for the stroke itself
i.top++;
i.left++;
i.bottom++;
i.right++;
int interior = cornerRadius;
if (paintFocus == PaintFocus.INSIDE && isFocusPainted)
interior = Math.max(interior, focusRingSize);
interior = Math.max(interior, 3);
int interiorSegment = Math.max(interior - 4, 2);
HorizontalPosition horizontalPosition = getHorizontalPosition(jc);
switch (horizontalPosition) {
case ONLY:
i.left += interior;
i.right += interior;
break;
case LEFT:
i.left += interior;
i.right += interiorSegment;
break;
case MIDDLE:
i.left += interiorSegment;
i.right += interiorSegment;
break;
case RIGHT:
i.left += interiorSegment;
i.right += interior;
}
VerticalPosition verticalPosition = getVerticalPosition(jc);
switch (verticalPosition) {
case ONLY:
i.top += interior;
i.bottom += interior;
break;
case TOP:
i.top += interior;
i.bottom += interiorSegment;
break;
case MIDDLE:
i.top += interiorSegment;
i.bottom += interiorSegment;
break;
case BOTTOM:
i.top += interiorSegment;
i.bottom += interior;
}
// TODO: maybe rethink how we suport margins? Instead of setting it once
// in installUI, we could update it with the "interior" calculations above
Insets margin = button == null ? null : button.getMargin();
if (margin != null) {
i.top += margin.top;
i.left += margin.left;
i.bottom += margin.bottom;
i.right += margin.right;
}
return i;
}
@Override
protected void defineShapes(JComponent jc, AbstractButton button,
ButtonState.Float state, Graphics2D g, int x, int y, int width,
int height, GeneralPath fillShape, GeneralPath strokeShape,
GeneralPath paintedPartitions, GeneralPath focusedPartitions) {
HorizontalPosition hp = getHorizontalPosition(jc);
VerticalPosition vp = getVerticalPosition(jc);
boolean isFocusPainted = button == null || button.isFocusPainted();
if (paintFocus == PaintFocus.OUTSIDE && isFocusPainted) {
if (hp == HorizontalPosition.ONLY
|| hp == HorizontalPosition.LEFT) {
x += focusRingSize;
width -= focusRingSize;
}
if (vp == VerticalPosition.ONLY || vp == VerticalPosition.TOP) {
y += focusRingSize;
height -= focusRingSize;
}
if (hp == HorizontalPosition.ONLY
|| hp == HorizontalPosition.RIGHT) {
width -= focusRingSize;
}
if (vp == VerticalPosition.ONLY
|| vp == VerticalPosition.BOTTOM) {
height -= focusRingSize;
}
}
ButtonShape buttonShape = new ButtonShape(cornerRadius,
cornerRadius);
int hp2 = hp.getPosition();
int vp2 = vp.getPosition();
buttonShape.getShape(fillShape, strokeShape, width - 1, height - 1,
hp2, vp2, false, null);
fillShape.transform(AffineTransform.getTranslateInstance(x, y));
strokeShape.transform(AffineTransform.getTranslateInstance(x, y));
Rectangle2D r = ShapeBounds.getBounds(fillShape);
if (hp == HorizontalPosition.RIGHT
|| hp == HorizontalPosition.MIDDLE) {
focusedPartitions.moveTo(r.getMinX(), r.getMinY());
focusedPartitions.lineTo(r.getMinX(), r.getMaxY());
}
if (hp == HorizontalPosition.LEFT
|| hp == HorizontalPosition.MIDDLE) {
focusedPartitions.moveTo(r.getMaxX(), r.getMinY());
focusedPartitions.lineTo(r.getMaxX(), r.getMaxY());
paintedPartitions.moveTo(r.getMaxX(), r.getMinY());
paintedPartitions.lineTo(r.getMaxX(), r.getMaxY());
}
if (vp == VerticalPosition.TOP || vp == VerticalPosition.MIDDLE) {
focusedPartitions.moveTo(r.getMinX(), r.getMaxY());
focusedPartitions.lineTo(r.getMaxX(), r.getMaxY());
paintedPartitions.moveTo(r.getMinX(), r.getMaxY());
paintedPartitions.lineTo(r.getMaxX(), r.getMaxY());
}
if (vp == VerticalPosition.BOTTOM || vp == VerticalPosition.MIDDLE) {
focusedPartitions.moveTo(r.getMinX(), r.getMinY());
focusedPartitions.lineTo(r.getMaxX(), r.getMinY());
}
}
@Override
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
Dimension size = b.getSize();
FontMetrics fm = g.getFontMetrics();
Insets i = c.getInsets();
Rectangle viewRect = new Rectangle(size);
viewRect.x += i.left;
viewRect.y += i.top;
viewRect.width -= i.right + viewRect.x;
viewRect.height -= i.bottom + viewRect.y;
Rectangle iconRect = new Rectangle();
Rectangle textRect = new Rectangle();
Font f = c.getFont();
g.setFont(f);
String text = SwingUtilities.layoutCompoundLabel(c, fm, b.getText(), b.getIcon(), b.getVerticalAlignment(),
b.getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect,
textRect, b.getText() == null ? 0 : b.getIconTextGap());
g.setColor(b.getBackground());
if (b.isContentAreaFilled()) {
if (RapidLookTools.isToolbarButton(b)) {
RapidLookTools.drawToolbarButton(g, b);
} else {
RapidLookTools.drawButton(b, g, RapidLookTools.createShapeForButton(b));
}
}
if (b.getIcon() != null) {
paintIcon(g, b, 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()) {
paintFocus(g, b, viewRect, textRect, iconRect);
}
if (!RapidLookTools.isToolbarButton(b)) {
if (b.isBorderPainted()) {
RapidLookTools.drawButtonBorder(b, g, RapidLookTools.createBorderShapeForButton(b));
}
}
}
/**
* Returns the current state of the passed in <code>AbstractButton</code>.
*
* @param c the button component.
*
* @return the button's state.
*/
private int getComponentState(JComponent c) {
int state = ENABLED;
if (!c.isEnabled()) {
state = DISABLED;
}
if (SeaGlassLookAndFeel.selectedUI == this) {
return SeaGlassLookAndFeel.selectedUIState | SynthConstants.ENABLED;
}
AbstractButton button = (AbstractButton) c;
ButtonModel model = button.getModel();
if (model.isPressed()) {
if (model.isArmed()) {
state = PRESSED;
} else {
state = MOUSE_OVER;
}
}
if (model.isRollover()) {
state |= MOUSE_OVER;
}
if (model.isSelected()) {
state |= SELECTED;
}
if (c.isFocusOwner() && button.isFocusPainted()) {
state |= FOCUSED;
}
if ((c instanceof JButton) && ((JButton) c).isDefaultButton()) {
state |= DEFAULT;
}
return state;
}
public void paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
String text = layout(b, g.getFontMetrics(), b.getWidth(), b.getHeight());
clearTextShiftOffset();
if (!model.isArmed() && !model.isPressed()) {
paintButtonBackground(g, b);
}
if (model.isArmed() && model.isPressed()) {
paintButtonPressed(g, b);
} else if (b.isRolloverEnabled() && model.isRollover()) {
paintButtonPressed(g, b);
}
if (b.getIcon() != null) {
paintIcon(g, c, iconRect);
}
if (b.isFocusPainted() && b.isFocusOwner()) {
paintFocus(g, b, viewRect, textRect, iconRect);
if (iconRect != null && iconRect.width > 0 && iconRect.height > 0) {
if (b.getIcon() != null) {
paintIcon(g, c, iconRect);
}
}
}
if (text != null && !text.equals("")) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
View v = (View) c.getClientProperty(BasicHTML.propertyKey);
if (v != null) {
v.paint(g2, textRect);
} else {
paintText(g2, b, textRect, text);
}
}
}