下面列出了javax.swing.ButtonModel#isRollover ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected Color getBackground( JComponent c ) {
if( !c.isEnabled() )
return disabledBackground;
// toolbar button
if( isToolBarButton( c ) ) {
ButtonModel model = ((AbstractButton)c).getModel();
if( model.isPressed() )
return toolbarPressedBackground;
if( model.isRollover() )
return toolbarHoverBackground;
// use background of toolbar
return c.getParent().getBackground();
}
boolean def = isDefaultButton( c );
return buttonStateColor( c,
getBackgroundBase( c, def ),
null,
isCustomBackground( c.getBackground() ) ? null : (def ? defaultFocusedBackground : focusedBackground),
def ? defaultHoverBackground : hoverBackground,
def ? defaultPressedBackground : pressedBackground );
}
public void paint(Graphics g, JComponent c) {
try {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
AbstractButton b = (AbstractButton) c;
ButtonModel bm = b.getModel();
if (bm.isRollover()) {
paintButtonRollOver(g2, b);
} else {
paintButtonNormal(g2, b);
}
super.paint(g2, c);
} catch (Exception e) {
}
}
/**
* Returns the Icon to use in painting the button.
*
* @param b the button.
*
* @return the 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 paint(Graphics g, JComponent c) {
AbstractButton b = (AbstractButton) c;
ButtonModel bm = b.getModel();
if (bm.isRollover()) {
paintButtonRollOver(g, b);
} else {
paintButtonNormal(g, b);
}
super.paint(g, c);
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton)c;
ButtonModel model = b.getModel();
boolean isPressed;
boolean isRollover;
boolean isEnabled;
isPressed = model.isPressed() && model.isArmed();
isRollover = b.isRolloverEnabled() && model.isRollover();
isEnabled = b.isEnabled();
if (!isEnabled) {
paintDisabled(b, g, x, y, width, height);
} else {
if (isPressed) {
paintPressed(b, g, x, y, width, height);
} else if (isRollover) {
paintRollover(b, g, x, y, width, height);
} else {
paintNormal(b, g, x, y, width, height);
}
}
}
}
/**
* Create a ButtonState that captures a ButtonModel's current state.
*/
public Boolean(ButtonModel model) {
isArmed = model.isArmed();
isEnabled = model.isEnabled();
isPressed = model.isPressed();
isRollover = model.isRollover();
isSelected = model.isSelected();
}
/**
* Create a ButtonState that captures a ButtonModel's current state.
*/
public Float(ButtonModel model) {
isArmed = model.isArmed() ? 1 : 0;
isEnabled = model.isEnabled() ? 1 : 0;
isPressed = model.isPressed() ? 1 : 0;
isRollover = model.isRollover() ? 1 : 0;
isSelected = model.isSelected() ? 1 : 0;
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
AbstractButton cb = (AbstractButton) c;
ButtonModel model = cb.getModel();
boolean isPressed = (model.isArmed() && model.isPressed());
boolean isRollver = (model.isRollover() && cb.isRolloverEnabled());
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawOval(g2d, x, y, (isRollver || isPressed));
if(model.isSelected())
{
fillOval(g2d, x, y);
}
else if(isRollver && isPressed)
{
drawOvalShadow(g2d, x, y);
}
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
}
@Override
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
JMenu menu = (JMenu) menuItem;
ButtonModel buttonmodel = menu.getModel();
int w = menu.getWidth();
int h = menu.getHeight();
Color oldColor = g.getColor();
if (!menu.isContentAreaFilled() || !menu.isOpaque()) {
// do nothing
} else {
if (menu.isTopLevelMenu()) {
if (buttonmodel.isSelected()) {
CachedPainter.drawMenuBackground(menuItem, g, 0, 0, w, h);
} else if (buttonmodel.isRollover() && buttonmodel.isEnabled()) {
g.setColor(Colors.MENUBAR_BACKGROUND_HIGHLIGHT);
g.fillRect(0, 0, w, h);
} else {
if (menuItem.getParent() instanceof JMenuBar) {
((MenuBarUI) ((JMenuBar) menuItem.getParent()).getUI()).update(g, menuItem);
}
}
} else {
if (!menuItem.getModel().isSelected()) {
RapidLookTools.drawMenuItemFading(menuItem, g);
} else {
RapidLookTools.drawMenuItemBackground(g, menuItem);
}
}
}
g.setColor(oldColor);
}
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);
}
}
}
@Override
public void paint(Graphics graphics, JComponent button) {
paintBackground(graphics, button);
// Restore normal look after pressing ends, if needed
if (button instanceof AbstractButton) {
ButtonModel model = ((AbstractButton) button).getModel();
if (!model.isPressed()) {
// Try to avoid switching borders if the button has none or custom
// borders
if (button.getBorder().equals(style.getBorderDown())) {
button.setBorder(style.getBorder());
}
}
if (model.isRollover()) {
hilite(graphics, button);
}
if (button instanceof JButton) {
if (((JButton) button).isDefaultButton()) {
Insets insets = button.getInsets();
graphics.setColor(style.getShadowColor());
int width = button.getWidth() - insets.right - insets.left - 3;
int height = button.getHeight() - insets.top - insets.bottom - 3;
graphics.drawRect(insets.left + 1, insets.right + 1, width, height);
}
}
}
super.paint(graphics, button);
}
/**
* Draws this component.
*
* @param g Where to draw to.
*/
@Override
public void paint(Graphics g) {
// use antialiasing
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
int width = getWidth();
int height = getHeight();
int x = 0;
int y = 0;
int margin = margin();
// draw border
getBorder().paintBorder(this, g, 0, 0, width - 1, height - 1);
// draw the color
g.setColor(color);
g.fillRoundRect(x + margin, y + margin
, width - (2 * margin), height - (2 * margin), 5, 5);
// draw effect as need
ButtonModel model = getModel();
if (model.isPressed()) {
g.setColor(ROLLOVER_COLOR);
g.fillRoundRect(x + margin, y + margin
, width - (2 * margin), height - (2 * margin), 5, 5);
g.fillRoundRect(x + margin, y + margin
, width - (2 * margin), height - (2 * margin), 5, 5);
}
else if (model.isRollover()) {
g.setColor(ROLLOVER_COLOR);
g.fillRoundRect(x + margin, y + margin
, width - (2 * margin), height - (2 * margin), 5, 5);
}
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton)c;
ButtonModel model = b.getModel();
boolean isPressed;
boolean isRollover;
boolean isEnabled;
isPressed = model.isPressed() && model.isArmed();
isRollover = b.isRolloverEnabled() && model.isRollover();
isEnabled = b.isEnabled();
if (!isEnabled) {
paintDisabled(b, g, x, y, width, height);
} else {
if (isPressed) {
paintPressed(b, g, x, y, width, height);
} else if (isRollover) {
paintRollover(b, g, x, y, width, height);
} else {
paintNormal(b, g, x, y, width, height);
}
}
}
}
protected void paintText(Graphics g, JComponent com, Rectangle rect,
String s) {
JLinkButton bn = (JLinkButton) com;
ButtonModel bnModel = bn.getModel();
// Color color = bn.getForeground();
// Object obj = null;
if (bnModel.isEnabled()) {
if (bnModel.isPressed()) {
bn.setForeground(bn.getActiveLinkColor());
} else if (bn.isLinkVisited()) {
bn.setForeground(bn.getVisitedLinkColor());
} else {
bn.setForeground(bn.getLinkColor());
}
} else {
if (bn.getDisabledLinkColor() != null) {
bn.setForeground(bn.getDisabledLinkColor());
}
}
super.paintText(g, com, rect, s);
int behaviour = bn.getLinkBehavior();
boolean drawLine = false;
if (behaviour == JLinkButton.HOVER_UNDERLINE) {
if (bnModel.isRollover()) {
drawLine = true;
}
} else if (behaviour == JLinkButton.ALWAYS_UNDERLINE || behaviour == JLinkButton.SYSTEM_DEFAULT) {
drawLine = true;
}
if (!drawLine) {
return;
}
FontMetrics fm = g.getFontMetrics();
int x = rect.x + getTextShiftOffset();
int y = (rect.y + fm.getAscent() + fm.getDescent() + getTextShiftOffset()) - 1;
if (bnModel.isEnabled()) {
g.setColor(bn.getForeground());
g.drawLine(x, y, (x + rect.width) - 1, y);
} else {
g.setColor(bn.getBackground().brighter());
g.drawLine(x, y, (x + rect.width) - 1, y);
}
}
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);
}
/**
* Paint classic text.
*
* @param b the b
* @param g the g
* @param x the x
* @param y the y
* @param text the text
* @param mnemIndex the mnem index
*/
static void paintClassicText(AbstractButton b, Graphics g, int x, int y,
String text, int mnemIndex) {
ButtonModel model = b.getModel();
/* Draw the Text */
Color color = b.getForeground();
if(model.isEnabled()) {
/*** paint the text normally */
if(!(b instanceof JMenuItem && model.isArmed())
&& !(b instanceof JMenu && (model.isSelected() || model.isRollover()))) {
/* We shall not set foreground color for selected menu or
* armed menuitem. Foreground must be set in appropriate
* Windows* class because these colors passes from
* BasicMenuItemUI as protected fields and we can't
* reach them from this class */
g.setColor(b.getForeground());
}
// SwingUtilities2.drawStringUnderlineCharAt(b, g,text, mnemIndex, x, y);
MySwingUtilities2.drawStringUnderlineCharAt(b
, g,text, mnemIndex, x, y);//* modified by Jack Jiang 为了非公开api的兼容性
} else { /*** paint the text disabled ***/
color = UIManager.getColor("Button.shadow");
Color shadow = UIManager.getColor("Button.disabledShadow");
if(model.isArmed()) {
color = UIManager.getColor("Button.disabledForeground");
} else {
if (shadow == null) {
shadow = b.getBackground().darker();
}
g.setColor(shadow);
// SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex,
// x + 1, y + 1);
MySwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex,
x + 1, y + 1);//* modified by Jack Jiang 为了非公开api的兼容性
}
if (color == null) {
color = b.getBackground().brighter();
}
g.setColor(color);
// SwingUtilities2.drawStringUnderlineCharAt(b, g, text, mnemIndex, x, y);
MySwingUtilities2.drawStringUnderlineCharAt(
b, g, text, mnemIndex, x, y);//* modified by Jack Jiang 为了非公开api的兼容性
}
}
public void paint(Graphics g)
{
// NLXPStyle xp = NLXPStyle.getXP();
// if (xp != null)
{
ButtonModel model = getModel();
// Skin skin = xp.getSkin(scrollbar, Part.SBP_ARROWBTN);
// State state = null;
// // normal, rollover, pressed, disabled
// if (model.isArmed() && model.isPressed())
// {
// switch (direction)
// {
// case NORTH: state = State.UPPRESSED; break;
// case SOUTH: state = State.DOWNPRESSED; break;
// case WEST: state = State.LEFTPRESSED; break;
// case EAST: state = State.RIGHTPRESSED; break;
// }
// }
// else if (!model.isEnabled())
// {
// switch (direction) {
// case NORTH: state = State.UPDISABLED; break;
// case SOUTH: state = State.DOWNDISABLED; break;
// case WEST: state = State.LEFTDISABLED; break;
// case EAST: state = State.RIGHTDISABLED; break;
// }
// }
// else if (model.isRollover() || model.isPressed())
// {
// switch (direction) {
// case NORTH: state = State.UPHOT; break;
// case SOUTH: state = State.DOWNHOT; break;
// case WEST: state = State.LEFTHOT; break;
// case EAST: state = State.RIGHTHOT; break;
// }
// }
// else
// {
// switch (direction)
// {
// case NORTH: state = State.UPNORMAL; break;
// case SOUTH: state = State.DOWNNORMAL; break;
// case WEST: state = State.LEFTNORMAL; break;
// case EAST: state = State.RIGHTNORMAL; break;
// }
// }
//原实现(windows样式)
// skin.paintSkin(g, 0, 0, getWidth(), getHeight(), state);
//2012-01-10 by js实现自定义样式
Graphics2D g2 = (Graphics2D)g;
switch(direction)
{
case NORTH:
if(model.isRollover())
__Icon9Factory__.getInstance().getScrollBarArrow_toTop_rover().draw(g2, 0, 0,getWidth(), getHeight());
else
__Icon9Factory__.getInstance().getScrollBarArrow_toTop().draw(g2, 0, 0,getWidth(), getHeight());
break;
case SOUTH:
if(model.isRollover())
__Icon9Factory__.getInstance().getScrollBarArrow_toBottom_rover().draw(g2, 0, 0,getWidth(), getHeight());
else
__Icon9Factory__.getInstance().getScrollBarArrow_toBottom().draw(g2, 0, 0,getWidth(), getHeight());
break;
case WEST:
if(model.isRollover())
__Icon9Factory__.getInstance().getScrollBarArrow_toLeft_rover().draw(g2, 0, 0,getWidth(), getHeight());
else
__Icon9Factory__.getInstance().getScrollBarArrow_toLeft().draw(g2, 0, 0,getWidth(), getHeight());
break;
case EAST:
if(model.isRollover())
__Icon9Factory__.getInstance().getScrollBarArrow_toRight_rover().draw(g2, 0, 0,getWidth(), getHeight());
else
__Icon9Factory__.getInstance().getScrollBarArrow_toRight().draw(g2, 0, 0,getWidth(), getHeight());
break;
}
}
// else
// {
// super.paint(g);
// }
}
/**
* Method which renders the text of the current menu item.
* <p>
* @param g Graphics context
* @param menuItem Current menu item to render
* @param textRect Bounding rectangle to render the text.
* @param text String to render
* @since 1.4
*/
protected void paintText(Graphics g, JMenuItem menuItem,
Rectangle textRect, String text)
{
//================= commet by Jack Jiang START
// if (WindowsMenuItemUI.isVistaPainting()) {
// WindowsMenuItemUI.paintText(accessor, g, menuItem, textRect, text);
// return;
// }
//================= commet by Jack Jiang END
JMenu menu = (JMenu)menuItem;
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
// Only paint rollover if no other menu on menubar is selected
boolean paintRollover = model.isRollover();
if (paintRollover && menu.isTopLevelMenu()) {
MenuElement[] menus = ((JMenuBar)menu.getParent()).getSubElements();
for (int i = 0; i < menus.length; i++) {
if (((JMenuItem)menus[i]).isSelected()) {
paintRollover = false;
break;
}
}
}
if ((model.isSelected() &&
(
// WindowsLookAndFeel.isClassicWindows() ||
!menu.isTopLevelMenu()))
||
(
// BEXPStyle.getXP() != null &&
(paintRollover ||model.isArmed() ||model.isSelected())
)
)
{
g.setColor(selectionForeground); // Uses protected field.
}
//================= add by Jack Jiang START
//特殊处理顶级菜单项(就是直接放在JMenuBar上的那一层),使之在被选中或rover等状态时保持黑色(或其它颜色)
//,目的是为了配合整个菜单项的L&F效果,并没有过多的用途,此颜色可提取作为UIManager的自定义属性哦
if(menu.isTopLevelMenu())
g.setColor(new Color(35,35,35));//用MaxOS X的经典黑
//================= add by Jack Jiang END
// WindowsGraphicsUtils.paintText(g, menuItem, textRect, text, 0);
WinUtils.paintText(g, menuItem, textRect, text, 0);
g.setColor(oldColor);
}
/**
* 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);
}
}
}