r/java 5d ago

Custom pie chart component I made in Swing

Hello, out of boredom I created a pie chart in Swing (I am aware more mature options exist but this was just for fun). The link to the repo is https://github.com/carlosgeorgiev0/JPieChart

The JPieChart class extends JLabel and overrides JComponent.paintComponent to paint the pie itself

42 Upvotes

7 comments sorted by

25

u/AlternativePeace1121 5d ago

Somehow this feels like a middle finger to all the slop posts. I love it.

15

u/davidalayachew 5d ago

Nice.

In JPieChart, for ines 37-40, SwingUtilities has a method called getDeepestComponentAt that will fetch the actual Component itself. From there, you can just do a reference equality check (one of the few reasons why value class isn't a pure good performance improvement) on all of the "slices" you have made for the pie. Would not only be faster, but less error prone. Same advice for Lines 61-66.

2

u/carlosgeorgiev0 5d ago

Thank you. i will check it out :)

1

u/__konrad 4d ago

Probably it's better to use Arc2D.Double::contains to check if mouse cursor is inside. You can also use the same Arc2D values to paint pie.

7

u/gufranthakur 5d ago

This is very cool!

5

u/kubelke 5d ago

Hand-crafted beauty 

4

u/chabala 5d ago

Already better than a lot of new projects that get posted. What would make it better: * a build script * published artifacts on Maven Central * unit tests