Skip to content

Instantly share code, notes, and snippets.

@aterai
Created July 27, 2015 11:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aterai/47948951d3602f63e3af to your computer and use it in GitHub Desktop.
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class BevelRoundedCornerButtonTest {
public JComponent makeUI() {
JPanel p = new JPanel();
p.add(new JButton("Default JButton"));
p.add(new BevelRoundedCornerButton("Rounded Corner Button"));
return p;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new BevelRoundedCornerButtonTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
class BevelRoundedCornerButton extends JButton {
private static final int ARC_WIDTH = 16;
private static final int ARC_HEIGHT = 16;
protected static final int FOCUS_STROKE = 2;
protected Shape shape;
protected Shape border;
protected Shape base;
public BevelRoundedCornerButton(String text) {
super(text);
}
@Override public void updateUI() {
super.updateUI();
setContentAreaFilled(false);
setFocusPainted(false);
setBackground(new Color(250, 250, 250));
initShape();
}
protected void initShape() {
if (!getBounds().equals(base)) {
base = getBounds();
int a = FOCUS_STROKE / 2;
int w = getWidth() - 1;
int h = getHeight() - 1;
shape = new RoundRectangle2D.Float(
0, 0, w, h, ARC_WIDTH, ARC_HEIGHT);
border = new RoundRectangle2D.Float(
a, a, w - a, h - a, ARC_WIDTH, ARC_HEIGHT);
}
}
@Override protected void paintComponent(Graphics g) {
initShape();
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
if (getModel().isArmed()) {
g2.setColor(new Color(230, 230, 230));
g2.fill(shape);
} else {
g2.setColor(getBackground());
g2.fill(shape);
}
g2.dispose();
super.paintComponent(g);
}
@Override protected void paintBorder(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(2f));
Rectangle r = border.getBounds();
r.grow(ARC_WIDTH + FOCUS_STROKE / 2, FOCUS_STROKE / 2);
Path2D.Double lb = new Path2D.Double();
lb.moveTo(r.x + r.width, r.y);
lb.lineTo(r.x + r.width, r.y + r.height);
lb.lineTo(r.x, r.y + r.height);
lb.closePath();
g2.setColor(Color.BLACK);
g2.setClip(lb);
g2.draw(border);
g2.setColor(Color.WHITE);
Area area = new Area(shape);
area.subtract(new Area(lb));
g2.setClip(area);
g2.draw(border);
g2.dispose();
}
@Override public boolean contains(int x, int y) {
return shape == null ? false : shape.contains(x, y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment