How to draw inner stroke with Java2D
Asked Answered
B

1

7

I just want to draw the circle which is exactly 15x15 pixels sized, and have fill and outline. I'm using Java2D. The problem is, as a result of sequentally called Graphics2D.fill(circle) and Graphics2D.draw(circle) the circle of 16x16 pixels drawn. It is because of internal Java2D outlinig mechanism, which for given 15x15 size provides 16x16 circle outline. Furthermore, if I ask Java2D to draw 14x14 pixels circle outline - it draws exactly 14x14 px. I have tried to play with antialiasing and stroke hints of renderer with no luck.
illustration Here are:

  1. RenderingHints.VALUE_STROKE_NORMALIZE and Ellipse2D.Double(0, 0, 15, 15) outline
  2. RenderingHints.VALUE_STROKE_PURE and same outline - notice 1px distortion
  3. previous stroke hint and Ellipse2D.Double(0, 0, 14, 14)
  4. RenderingHints.VALUE_STROKE_NORMALIZE, Ellipse2D.Double(0, 0, 15, 15) outline and antialiasing hint

So, I can't draw 15px circle outline (also 13 px, 29 px and any odd size) with Java2D. Is there a way to draw some sort of inner stroke, which fills pixels at inner border of shape?

Baram answered 4/9, 2014 at 8:56 Comment(1)
IMHO there is no way to do it in java Grapics/Graphics2D library because I was facing the same problem in the past and couldn't find any reasonable solution. Anyway, option 1 and 4 looks good in case you doesn't want to change your graphics library.Disaster
W
0

Looks like "1" is the option you want, but you've got an off-by-one error. 0,0 to 15,15 is actually supposed to be 16 pixels. If you want 15 pixels, you'll need 0,0 to 14,14.

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

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

Winterkill answered 20/7, 2015 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.