How to draw an inner stroke using Java2D

I just want to draw a circle exactly 15x15 pixels in size and have a fill and outline. I am using Java2D. The problem is that as a result, Graphics2D.fill(circle) and Graphics2D.draw(circle) are called 16x16 pixels in a circle. This is due to the internal outlinig Java2D mechanism, which provides a 16x16 contour for a given 15x15 size. In addition, if I ask Java2D to draw a circle outline of 14x14 pixels, it draws exactly 14x14 px. I tried to play with antialiasing and intuitive renderer hints without any luck.
illustration Here:

  • RenderingHints.VALUE_STROKE_NORMALIZE and Ellipse2D.Double(0, 0, 15, 15) outline
  • RenderingHints.VALUE_STROKE_PURE and the same outline - note the 1px distortion
  • previous stroke and Ellipse2D.Double(0, 0, 14, 14)
  • RenderingHints.VALUE_STROKE_NORMALIZE , Ellipse2D.Double(0, 0, 15, 15) tooltip and smoothing

So, I cannot draw a 15px outline (also 13 px, 29 px and any odd size) with Java2D. Is there a way to make some kind of internal stroke that fills the pixels on the inner border of the form?

+6
source share
1 answer

It sounds like "1" is the option you want, but you have a "one by one" error. From 0.0 to 15.15 there should actually be 16 pixels. If you want 15 pixels, you need between 0.0 and 14.14.

If it is specifically 15x15 with a predefined color, you can pre-render in some image editing program and just use drawImage.

You may also consider translating to .5, .5. See what # 2 looks like when you do Ellipse.Double (.5, .5,15.5,15.5). You can be happier with this result.

0
source

Source: https://habr.com/ru/post/974813/


All Articles