A może zrób raczej to na zasadzie UI. Dam przykład oczywiście mocno uproszczony:
class RoundBorder implements Border {
private final static Color BORDER_COLOR = Color.BLACK;
private final static float BORDER_WIDTH = 2f;
private final static int MARGIN = 5;
@Override
public Insets getBorderInsets(Component c) {
int margin = (int) Math.ceil(BORDER_WIDTH) + MARGIN;
return new Insets(margin,margin, margin, margin);
}
@Override
public boolean isBorderOpaque() {
return true;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2 = (Graphics2D) g;
Stroke orignalStroke = g2.getStroke();
Object oldAntialiasing = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
this.doPaintBorder(c, g2, x, y, width, height);
} finally {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntialiasing);
g2.setStroke(orignalStroke);
}
}
protected void doPaintBorder(Component c, Graphics2D g, int x, int y, int width, int height) {
g.setStroke(new BasicStroke(BORDER_WIDTH));
g.setColor(BORDER_COLOR);
g.drawOval(x + 1, y + 1, width - 3, height - 3);
}
}
class RoundButtonUI extends MetalButtonUI {
private final static Color PRESSED_BACKGROUND = Color.ORANGE;
private final static Color FOCUS_COLOR = Color.RED;
private final static Color BACKGROUND_COLOR = Color.WHITE;
public static ComponentUI createUI(JComponent component) {
return new RoundButtonUI();
}
@Override
public void installDefaults(AbstractButton b) {
super.installDefaults(b);
b.setBorder(new RoundBorder());
b.setBackground(BACKGROUND_COLOR);
}
@Override
public void update(Graphics g, JComponent c) {
AbstractButton button = (AbstractButton) c;
Graphics2D g2 = (Graphics2D) g;
if (button.isContentAreaFilled() && button.isEnabled()) {
this.paintBackground(g2, button);
this.paint(g2, c);
} else {
super.update(g, c);
}
}
@Override
protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect,
Rectangle textRect, Rectangle iconRect) {
Graphics2D g2 = (Graphics2D) g;
g2.setPaint(FOCUS_COLOR);
Stroke oldStroke = g2.getStroke();
Object oldAntialiasing = g2.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
try {
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setStroke(new BasicStroke(2f));
g2.drawOval(2, 2, b.getWidth() - 5, b.getHeight() - 5);
} finally {
g2.setStroke(oldStroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAntialiasing);
}
}
@Override
protected void paintButtonPressed(Graphics g, AbstractButton button) {
g.setColor(PRESSED_BACKGROUND);
g.fillOval(0, 0, button.getWidth() - 1, button.getHeight() - 1);
}
protected void paintBackground(Graphics2D g, AbstractButton button) {
g.setPaint(button.getBackground());
g.fillOval(0, 0, button.getWidth() - 1, button.getHeight() - 1);
}
@Override
public Dimension getPreferredSize(JComponent c) {
// dla wymuszenia kształtu koła
Dimension preferred = super.getPreferredSize(c);
int size = Math.max(preferred.width, preferred.height);
return new Dimension(size, size);
}
@Override
public Dimension getMinimumSize(JComponent c) {
// dla wymuszenia kształtu koła
Dimension minimum = super.getMinimumSize(c);
int size = Math.max(minimum.width, minimum.height);
return new Dimension(size, size);
}
}
public class Main extends JFrame {
private static final long serialVersionUID = -8450091917337048608L;
private JButton button1;
private JButton button2;
private Main() {
this.initComponents();
this.initDefaults();
this.createLayout();
}
private void initComponents() {
this.button1 = new JButton("TEST 1");
this.button1.setUI(new RoundButtonUI());
this.button2 = new JButton("TEST 2");
this.button2.setUI(new RoundButtonUI());
}
private void initDefaults() {
this.setTitle("Test");
this.setSize(160, 100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createLayout() {
this.getContentPane().setLayout(new FlowLayout());
this.getContentPane().add(this.button1);
this.getContentPane().add(this.button2);
}
private void showFrameInCenter() {
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Main main = new Main();
main.showFrameInCenter();
}
});
}
}