下面列出了javax.swing.ButtonModel#isPressed ( ) 实例代码,或者点击链接到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);
}
}
}
public void paint(Graphics g, JComponent c)
{
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
paintBg(g, (AbstractButton) c);
// 设置组件偏移,以达到视觉上的按下和弹起效果
// Set the component offsets to achieve visual depress and bounce
if(model.isPressed() && model.isArmed() && b.getIcon() == null)
{
g.translate(2, 1);
}
super.paint(g, c);
if(model.isPressed() && model.isArmed() && b.getIcon() == null)
{
g.translate(-2, -1);
}
}
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);
}
}
}
}
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);
}
}
}
protected Color getArrowColor(ButtonModel model)
{
if(model.isPressed())
{
return highlight;
}
return normal;
}
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 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);
}
}
}
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
g2.setFont(getFont());
FontMetrics fm = g2.getFontMetrics();
ButtonModel mdl = getModel();
String s = getText();
g2.setComposite(AlphaComposite.SrcOver.derive(0.85f));
if (!mdl.isEnabled()) {
g2.setColor(new Color(0x808080));
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(new Color(0x000000));
} else
if (mdl.isPressed() || mdl.isSelected()) {
if (mdl.isRollover()) {
g2.setColor(new Color(0xE0E0E0));
} else {
g2.setColor(new Color(0xFFFFFF));
}
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(new Color(0x000000));
} else {
if (mdl.isRollover()) {
g2.setColor(new Color(0x000000));
} else {
g2.setColor(new Color(0x202020));
}
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 10, 10);
g2.setColor(new Color(0xFFFFFFFF));
}
int x = (getWidth() - fm.stringWidth(s)) / 2;
int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent() + fm.getLeading();
g2.drawString(s, x, y);
}
public void paintIcon(Component c, Graphics g, int x, int y)
{
JCheckBox cb = (JCheckBox) c;
ButtonModel model = cb.getModel();
//选中时
if(model.isSelected())
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_normal().getImage(), x, y, null);
}
}
//未选中时
else
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_unchecked_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_unchecked_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getCheckBoxButtonIcon_unchecked_normal().getImage(), x, y, null);
}
}
}
public void paintIcon(Component c, Graphics g, int x, int y) {
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
//选中时
if(model.isSelected())
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_normal().getImage(), x, y, null);
}
}
//未选中时
else
{
//处于禁用状态
if(!model.isEnabled())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_unchecked_disable().getImage(), x, y, null);
else
{
//处于被按住状态
if(model.isPressed())
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_unchecked_pressed().getImage(), x, y, null);
else
g.drawImage(__IconFactory__.getInstance().getRadioButtonIcon_unchecked_normal().getImage(), x, y, null);
}
}
}
@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);
}
}
/**
* Returns the amount to shift the text/icon when painting.
*
* @param state DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
protected int getTextShiftOffset(SeaGlassContext state) {
AbstractButton button = (AbstractButton) state.getComponent();
ButtonModel model = button.getModel();
if (model.isArmed() && model.isPressed() && button.getPressedIcon() == null) {
return state.getStyle().getInt(state, getPropertyPrefix() + "textShiftOffset", 0);
}
return 0;
}
private static String toString(ButtonModel model) {
return "isArmed = " + model.isArmed()
+ ", isEnabled = " + model.isEnabled()
+ ", isPressed = " + model.isPressed()
+ ", isSelected = " + model.isSelected();
}
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);
}
}
}
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);
}
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
JMenuItem b = (JMenuItem) c;
ButtonModel bm = b.getModel();
g.translate(x, y);
boolean isSelected = bm.isSelected();
boolean isEnabled = bm.isEnabled();
boolean isPressed = bm.isPressed();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// drawing background section
if (!isEnabled) {
g2.setColor(Colors.RADIOBUTTON_BORDER_DISABLED);
} else {
if (isPressed) {
g2.setColor(Colors.RADIOBUTTON_BORDER_FOCUS);
} else {
g2.setColor(Colors.RADIOBUTTON_BORDER);
}
}
g2.setStroke(RADIO_STROKE);
Shape circle = new Ellipse2D.Double(0, 0, 9, 9);
g2.draw(circle);
// drawing sphere
if (isSelected) {
if (isEnabled) {
g2.setColor(Colors.RADIOBUTTON_CHECKED);
} else {
g2.setColor(Colors.RADIOBUTTON_CHECKED_DISABLED);
}
circle = new Ellipse2D.Double(3, 3, 4, 4);
g2.fill(circle);
}
g.translate(-x, -y);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
JRadioButton radioButton = (JRadioButton) c;
ButtonModel bm = radioButton.getModel();
int w = c.getWidth();
int h = c.getHeight();
if (h < 0 || w < 0) {
return;
}
g.translate(x, y);
boolean isSelected = bm.isSelected();
boolean isEnabled = bm.isEnabled();
boolean isPressed = bm.isPressed();
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// drawing background section
if (!isEnabled) {
g2.setColor(Colors.RADIOBUTTON_BACKGROUND_DISABLED);
} else {
g2.setColor(Colors.RADIOBUTTON_BACKGROUND);
}
Shape circle = new Ellipse2D.Double(2, 2, 12, 12);
g2.fill(circle);
if (!isEnabled) {
g2.setColor(Colors.RADIOBUTTON_BORDER_DISABLED);
} else {
if (isPressed) {
g2.setColor(Colors.RADIOBUTTON_BORDER_FOCUS);
} else {
g2.setColor(Colors.RADIOBUTTON_BORDER);
}
}
g2.setStroke(RADIO_STROKE);
circle = new Ellipse2D.Double(1, 1, 14, 14);
g2.draw(circle);
// drawing sphere
if (isSelected) {
if (isEnabled) {
g2.setColor(Colors.RADIOBUTTON_CHECKED);
} else {
g2.setColor(Colors.RADIOBUTTON_CHECKED_DISABLED);
}
circle = new Ellipse2D.Double(4, 4, 9, 9);
g2.fill(circle);
}
g.translate(-x, -y);
}
/**
* 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;
}
@Override
public void paint(Graphics g1, JComponent c) {
Graphics2D g = DrawHelper.enableAntialiasing(g1);
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
boolean down;
if (c instanceof JToggleButton) {
down = ((JToggleButton) c).isSelected();
} else {
down = model.isArmed() && model.isPressed();
}
BufferedImage bg;
BufferedImage[] border;
float scale = this.scale;
if (down) {
border = BORDER_DOWN;
scale /= 2;
bg = backgroundImagePressed;
} else {
bg = backgroundImage;
border = BORDER_NORMAL;
}
// Draw background
g.drawImage(bg, 0, 0, c);
BorderHelper.drawBorder(g1, c, border, scale);
FontMetrics fm = g.getFontMetrics();
int y = (b.getHeight() - fm.getAscent() - fm.getDescent()) / 2 + fm.getAscent();
int x = textPaddingLeftRight;
if (down) {
x += 1;
y += 1;
}
g.setFont(c.getFont());
// draw shadow
g.setColor(Color.BLACK);
g.drawString(b.getText(), x + 1, y + 1);
g.setColor(c.getForeground());
g.drawString(b.getText(), x, y);
}