/**
*
* @author Patricia Bradford, www.CodeMonkeyCorner.com
*/
public class RoundedButtonUI extends BasicButtonUI {
private static final float arcwidth = 16.0f;
private static final float archeight = 16.0f;
protected final Color fc = new Color(205, 250, 7);
protected final Color ac = new Color(150, 229, 76);
protected Shape shape;
protected Shape border;
protected Shape base;
@Override
protected void installDefaults(AbstractButton b) {
UIManager.put("Button.background", new Color(85,85,85));
UIManager.put("Button.foreground", new Color(200,200,200));
super.installDefaults(b);
b.setContentAreaFilled(false);
b.setOpaque(false);
b.setBorderPainted(false);
initShape(b);
}
@Override
public void paint(Graphics g, JComponent c) {
Graphics2D g2 = (Graphics2D) g;
AbstractButton b = (AbstractButton) c;
ButtonModel model = b.getModel();
initShape(b);
//ContentArea
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
if (model.isArmed()) {
g2.setColor(c.getBackground());
g2.fill(shape);
} else if (b.isRolloverEnabled() && model.isRollover()) {
paintFocusAndRollover(g2, c, fc);
} else if (b.hasFocus()) {
paintFocusAndRollover(g2, c, fc);
} else {
g2.setColor(c.getBackground());
g2.fill(shape);
}
//Border
g2.setPaint(c.getForeground());
g2.draw(shape);
g2.setColor(c.getBackground());
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
super.paint(g2, c);
}
private void initShape(JComponent c) {
if (!c.getBounds().equals(base)) {
base = c.getBounds();
shape = new RoundRectangle2D.Float(0, 0, c.getWidth() - 1, c.getHeight() - 1,
arcwidth, archeight);
}
}
private void paintFocusAndRollover(Graphics2D g2, JComponent c, Color color) {
g2.setPaint(new GradientPaint(0, 0, color, c.getWidth() - 1, c.getHeight() - 1,
color.brighter(), true));
g2.fill(shape);
}
@Override
protected void paintText(Graphics g, AbstractButton b, Rectangle textRect, String text) {
ButtonModel model = b.getModel();
FontMetrics fm = SwingUtilities2.getFontMetrics(b, g);
int mnemonicIndex = b.getDisplayedMnemonicIndex();
/* Draw the Text */
if(model.isEnabled()) {
/*** paint the text normally */
if(model.isPressed())
g.setColor(b.getBackground());
else
g.setColor(b.getForeground());
SwingUtilities2.drawStringUnderlineCharAt(b, g,text, mnemonicIndex,
textRect.x + getTextShiftOffset(),
textRect.y + fm.getAscent() + getTextShiftOffset());
}
else {
/*** paint the text disabled ***/
g.setColor(b.getBackground().brighter());
SwingUtilities2.drawStringUnderlineCharAt(b, g,text, mnemonicIndex,
textRect.x, textRect.y + fm.getAscent());
g.setColor(b.getBackground().darker());
SwingUtilities2.drawStringUnderlineCharAt(b, g,text, mnemonicIndex,
textRect.x - 1, textRect.y + fm.getAscent() - 1);
}
}
@Override
protected void paintButtonPressed(Graphics g, AbstractButton c) {
Graphics2D g2 = (Graphics2D) g;
ButtonModel model = c.getModel();
//ContentArea
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g2.setColor(c.getForeground());
g2.fill(shape);
//Border
g2.setPaint(c.getBackground());
g2.draw(shape);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
}
}