Skip to content
Advertisement

Java awt draw elements around a circle

I’m currently writing a little game and I came over a problem. I need to draw 64 small circles at the border of a big circle. So I want something like this:

64 circles arranged around a big circle

I’ve already tried many things, but they didn’t worked. How can this be done in java, using the java.awt.Component#paint() method and the java.awt.Graphics class?

Thanks.

Advertisement

Answer

So, your basic problem comes down to “find a point on a circle based on a given angle”

A quick google will find resources like Find the coordinates of a point on a circle, now, to be frank, I’m an idiot, so I’d narrow my search to include Java, which would give us something like How to calculate the coordinates of points in a circle using Java? – sweet.

So the basic calculation might look something like

JavaScript

Now, this solves the core aspect of your problem. The rest comes down to simply painting the results. See Performing Custom Painting and Painting in AWT and Swing as a starting point and 2D Graphics for a more detailed look into the API.

Now, taking all of the above, you might end up with a solution looking something like…

enter image description here

JavaScript

So, about now, you should realise that my “squares” are, well, square, not “dimond” shaped like yours. This is where you’re going to have to start doing some work.

If I was approaching this problem I might be tempted, to create a custom shape or, apply a 45 degree transformation to the box and the translate it’s position to render it or just rotate the whole result by 45 degrees, but this brings a whole bag of other issues depending on what you want to do with it

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement