简体   繁体   中英

How to draw inner stroke with Java2D

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.
插图 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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM