下面列出了javax.swing.ButtonModel#isArmed ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
static void paintIcon(Graphics g, SeaGlassMenuItemLayoutHelper lh,
MenuItemLayoutHelper.LayoutResult lr) {
if (lh.getIcon() != null) {
Icon icon;
JMenuItem mi = lh.getMenuItem();
ButtonModel model = mi.getModel();
if (!model.isEnabled()) {
icon = mi.getDisabledIcon();
} else if (model.isPressed() && model.isArmed()) {
icon = mi.getPressedIcon();
if (icon == null) {
// Use default icon
icon = mi.getIcon();
}
} else {
icon = mi.getIcon();
}
if (icon != null) {
Rectangle iconRect = lr.getIconRect();
SynthIcon.paintIcon(icon, lh.getContext(), g, iconRect.x,
iconRect.y, iconRect.width, iconRect.height);
}
}
}
@Override
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor) {
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
if (model.isArmed() || (menuItem instanceof JMenu && model.isSelected())) {
paintButtonPressed(g, menuItem);
} else {
g.setColor(this.colorBg);
// g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());
// g.fillRect(0, 0, menuItem.getWidth(), menuItem.getHeight());//(0,
// 0, gap + 1, menuItem.getHeight());
// g.drawLine(gap + 1, 0, gap + 1, menuItem.getHeight());
// if (menuItem.getIcon() != null) {
// int gap = menuItem.getIcon().getIconWidth() + 2;
// g.setColor(this.darkColor);
// g.drawLine(gap, 0, gap, menuItem.getHeight());
// g.setColor(this.lightColor);
// g.drawLine(gap + 1, 0, gap + 1, menuItem.getHeight());
// }
}
g.setColor(oldColor);
}
/**
* <pre>
* 根据按钮状态, 获取当前状态下图片信息。
*
* According to the button state, access to the current
* state of the picture information.
* </pre>
*
* @param c <code>CheckBoxMenuItem</code> object.
* @param model <code>ButtonModel</code>
* @return <code>Image</code> when is selected return current image, otherwise return null.
*/
public Image getPreImg(Component c, ButtonModel model)
{
if (!model.isSelected())
{
return null;
}
if (model.isArmed())
{
return getRollverImg();
}
else
{
return getNormalImg();
}
}
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 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);
}
public static void drawMenuItemBackground(Graphics g, JMenuItem menuItem) {
Color oldColor = g.getColor();
ButtonModel model = menuItem.getModel();
int w = menuItem.getWidth();
int h = menuItem.getHeight();
if (model.isArmed() || model.isSelected() && menuItem instanceof JMenu) {
g.setColor(Colors.MENU_ITEM_BACKGROUND_SELECTED);
g.fillRect(0, 0, w, h);
} else if (!(menuItem instanceof JMenu && ((JMenu) menuItem).isTopLevelMenu())) {
drawMenuItemFading(menuItem, g);
}
g.setColor(oldColor);
}
@Override
protected void paintBackground(Graphics g, JMenuItem menuItem, Color bgColor)
{
// see parent!
ButtonModel model = menuItem.getModel();
Color oldColor = g.getColor();
int menuWidth = menuItem.getWidth();
int menuHeight = menuItem.getHeight();
Graphics2D g2 = (Graphics2D)g;
if (model.isArmed()
|| (menuItem instanceof JMenu && model.isSelected()))
{
//菜单项的样式绘制(用NinePatch图来填充)
__Icon9Factory__.getInstance().getBgIcon_ItemSelected()
.draw(g2, 0, 0, menuWidth, menuHeight);
}
else
{
if(!enforceTransparent)
{
g.setColor(menuItem.getBackground());
g.fillRect(0, 0, menuWidth, menuHeight);
}
}
g.setColor(oldColor);
}
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);
}
}
}
}
private static String toString(ButtonModel model) {
return "isArmed = " + model.isArmed()
+ ", isEnabled = " + model.isEnabled()
+ ", isPressed = " + model.isPressed()
+ ", isSelected = " + model.isSelected();
}
@Override
protected void paintComponent(Graphics g) {
// Set the foreground on the fly to ensure the text is painted
// with the proper color in super.paintComponent
ButtonModel model = getModel();
if (model.isArmed()) {
super.setForeground(activeForeground);
} else if (visited) {
super.setForeground(visitedForeground);
} else {
super.setForeground(normalForeground);
}
super.paintComponent(g);
if (drawUnderline) {
Insets insets = getInsets();
viewRect.x = insets.left;
viewRect.y = insets.top;
viewRect.width = getWidth() - insets.left - insets.right;
viewRect.height = getHeight() - insets.top - insets.bottom;
int baseline = getBaseline(viewRect.width, viewRect.height);
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
textRect.x = textRect.y = textRect.width = textRect.height = 0;
SwingUtilities.layoutCompoundLabel(g.getFontMetrics(), getText(),
getIcon(), getVerticalAlignment(), getHorizontalAlignment(),
getVerticalTextPosition(), getHorizontalTextPosition(),
viewRect, iconRect, textRect, getIconTextGap());
// getBaseline not returning correct results, so workaround for now
if (UIManager.getLookAndFeel().getName().equals("Nimbus")) {
baseline += 7;
} else {
baseline += 3;
}
g.setColor(getForeground());
g.drawLine(textRect.x,
baseline,
textRect.x + textRect.width,
baseline);
}
}
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
protected void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
ButtonModel m = getModel();
Insets insets = getInsets();
int width = getWidth() - insets.left - insets.right;
int height = getHeight() - insets.top - insets.bottom;
tileStretchPaint(g2,this,(BufferedImage) getImage(m.isArmed()), sourceInsets);
if (getModel().isRollover()) {
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(buttonHighlight,
insets.left + 2, insets.top + 2,
width - 4, height - 4, null);
}
FontMetrics fm = getFontMetrics(getFont());
TextLayout layout = new TextLayout(getText(),
getFont(),
g2.getFontRenderContext());
Rectangle2D bounds = layout.getBounds();
int x = (int) (getWidth() - insets.left - insets.right -
bounds.getWidth()) / 2;
//x -= 2;
int y = (getHeight() - insets.top - insets.bottom -
fm.getMaxAscent() - fm.getMaxDescent()) / 2;
y += fm.getAscent() - 1;
if (m.isArmed()) {
x += 1;
y += 1;
}
g2.setColor(shadowColor);
layout.draw(g2,
x + (int) Math.ceil(shadowOffsetX),
y + (int) Math.ceil(shadowOffsetY));
g2.setColor(getForeground());
layout.draw(g2, x, y);
}
@Override
protected void paintComponent(Graphics g) {
// Set the foreground on the fly to ensure the text is painted
// with the proper color in super.paintComponent
ButtonModel model = getModel();
if (model.isArmed()) {
super.setForeground(activeForeground);
} else if (visited) {
super.setForeground(visitedForeground);
} else {
super.setForeground(normalForeground);
}
super.paintComponent(g);
if (drawUnderline) {
Insets insets = getInsets();
viewRect.x = insets.left;
viewRect.y = insets.top;
viewRect.width = getWidth() - insets.left - insets.right;
viewRect.height = getHeight() - insets.top - insets.bottom;
int baseline = getBaseline(viewRect.width, viewRect.height);
iconRect.x = iconRect.y = iconRect.width = iconRect.height = 0;
textRect.x = textRect.y = textRect.width = textRect.height = 0;
SwingUtilities.layoutCompoundLabel(g.getFontMetrics(), getText(),
getIcon(), getVerticalAlignment(), getHorizontalAlignment(),
getVerticalTextPosition(), getHorizontalTextPosition(),
viewRect, iconRect, textRect, getIconTextGap());
// getBaseline not returning correct results, so workaround for now
if (UIManager.getLookAndFeel().getName().equals("Nimbus")) {
baseline += 7;
} else {
baseline += 3;
}
g.setColor(getForeground());
g.drawLine(textRect.x,
baseline,
textRect.x + textRect.width,
baseline);
}
}
/**
* 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;
}
/**
* 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, 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);
}
}
}
/**
* 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);
}