Circle Button

This code defines a class that extends the JButton class and overrides a few methods to make a circular button.


This is a companion discussion topic for the original entry at https://happycoding.io/examples/java/circle-button

lovely code, how would one produce a circular button which can be sliced into a number of cuts like in a pizza, and each piece assigned to as a different button ?

Instead of subclassing JButton for each of your slices, I’d probably create a single component that did different things depending on where the user clicked.

Either way, at the core, you’re probably going to need to use trigonometry to draw your pizza buttons, and to detect which slice the mouse is on when it clicks.

If you search for “How do you check if a point is within a sector?” you’ll find a bunch of results, including this one which looks promising: