javagraphicsgraphics2d

Java Graphics fillArc() is not precise enough


(Specifically, I'm using Graphics2D, but it inherits the fillArc() method from Graphics, so I listed the question under that class.)

The fillArc method accepts its parameters in the following order:

fillArc(int x, int y, int width, int height, int startAngle, int arcAngle)

My problem stems from the fact that it requires startAngle and arcAngle to be given as integers. I use Math.toDegrees() to convert my radians (calculated to a precise position) into degrees, but it then needs to be cast to int for the method. Unfortunately, this creates a small region where the specified arc is not placed in the proper area.

All of my calculations produce radians for positions in the circle, stored in doubles. These allow the level of precision I need, but fillArc is producing a background that is misaligned from the rest of the data. Changing the data to fit the regions covered by fillArc may be possible, but is needlessly complicated and (this is my main gripe) removes the accuracy of the render.

Is there any way to draw a filled arc with the level of precision that I want? I'd like to be able to dump the radian (or converted degree) into the method to get as accurate a render as possible. Changing from Graphics2D is acceptable; I'd much rather have to rewrite the easier rendering section of code than lose accuracy.

Edit: See image below. Sunrise/sunset bars are the accurate times, while the colored segments are drawn by fillArc.

Edit 2: Solution provided by Computable in comments. (Arc2D.Double) Clock


Solution

  • The draw and fill can take a Shape one of which is a Arc2D.Double.