import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class NextButton
extends JButton {
Shape shape;
public NextButton() {
// This allows us to paint a round background.
setContentAreaFilled(false);
}
public NextButton(
String label) {
super(label);
// These statements enlarge the button so that it
// becomes a circle rather than an oval.
/* Dimension size = getPreferredSize();
size.width = size.height = Math.max(size.width,
size.height);
setPreferredSize(size);*/
// This call causes the JButton not to paint
// the background.
// This allows us to paint a round background.
setContentAreaFilled(false);
}
// Paint the round background and label.
protected void paintComponent(Graphics g) {
if (getModel().isArmed()) {
// You might want to make the highlight color
// a property of the RoundButton class.
g.setColor(Color.lightGray);
this.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else {
g.setColor(getBackground());
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
if (isEnabled()) {
g.setColor(new Color(255, 240, 220));
}
int[] xpoints = {
0, getSize().width / 2, getSize().width / 2, getSize().width - 1, getSize().width / 2,
getSize().width / 2, 0
};
int[] ypoints = {
getSize().height / 4, getSize().height / 4, 0, getSize().height / 2 - 1,
getSize().height - 1, getSize().height * 3 / 4, getSize().height * 3 / 4
};
int npoints = 7;
g.fillPolygon(xpoints, ypoints, npoints);
// This call will paint the label and the
// focus rectangle.
super.paintComponent(g);
}
//************************************************************************************************************************************
// Paint the border of the button using a simple stroke.
protected void paintBorder(Graphics g) {
g.setColor(getForeground());
int[] xpoints = {
0, getSize().width / 2, getSize().width / 2, getSize().width - 1, getSize().width / 2,
getSize().width / 2, 0
};
int[] ypoints = {
getSize().height / 4, getSize().height / 4, 0, getSize().height / 2 - 1,
getSize().height - 1, getSize().height * 3 / 4, getSize().height * 3 / 4
};
int npoints = 7;
g.drawPolygon(xpoints, ypoints, npoints);
}
//************************************************************************************************************************************
// Hit detection.
public boolean contains(int x, int y) {
// If the button has changed size,
// make a new shape object.
if (shape == null || !shape.getBounds().equals(getBounds())) {
int[] xpoints = {
0, getSize().width / 2, getSize().width / 2, getSize().width - 1,
getSize().width / 2, getSize().width / 2, 0
};
int[] ypoints = {
getSize().height / 4, getSize().height / 4, 0, getSize().height / 2 - 1,
getSize().height - 1, getSize().height * 3 / 4, getSize().height * 3 / 4
};
int npoints = 7;
shape = new Polygon(xpoints, ypoints, npoints);
}
return shape.contains(x, y);
}
//************************************************************************************************************************************
}
This is just an example how you can customize the button shapes, so you can change the xpoints, ypoints and npoints to get your desired shape.