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
This code defines a class that extends the JButton
class and overrides a few methods to make a circular 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:
@Kevin I’m doing my best to add an incon to that button with the method void setIcon(Icon i){} that belongs to JButton. I also tried to override the method and to call the one of the superclass but it does not work. Can you guess why? May you help me please, I would really appreciate that.
Thanks a lot!
Hey @MPadrin, nice to meet you.
Can you be a little more specific? When you say it doesn’t work, what happens? Do you have any error messages at all?
My best guess is that your code is looking for the icon file in a different place than it’s located. Try printing out the full path of the icon file you’re loading, and then compare that to where it’s actually located.
If that doesn’t help, can you post your code along with your directory structure?
Sure, and sorry for being this late.
When I run the code the image is not printed, I just have a button with a white background. If I change the class from CircleButton to JButton the image is correctly uploaded.
CircleButton JButtonMusic=new CircleButton("");
//JButtonMusic.setContentAreaFilled(false);
JButtonMusic.setFocusPainted(false);
JButtonMusic.setBorderPainted(false);
JButtonMusic.setSize(120,70);
imageIcon2 = new ImageIcon(musicIcon); // load the image to a imageIcon
image2 = imageIcon2.getImage(); // transform it
newimg2 = image2.getScaledInstance(30, 30, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
imageIcon2 = new ImageIcon(newimg2); // transform it back
JButtonMusic.setIcon( imageIcon2);
JButtonMusic.setLocation(1050,710);
add(JButtonMusic);
Thanks a lot for your help.
MP
Ohh I understand what you’re saying now, thanks for posting the code.
The CircleButton
class overrides the paintComponent()
method, which means that it’s overriding the logic for drawing the icon as well.
You can add that logic back in yourself. You might take a look at how Java’s underlying code does it. Do a ctrl+f for paintIcon
in this file: Source for javax.swing.plaf.basic.BasicButtonUI (GNU Classpath 0.95 Documentation)
Alternatively, you can probably just draw the image yourself instead of going through the ImageIcon
class: Drawing an Image (The Java™ Tutorials > 2D Graphics > Working with Images)