Sunday, 18 December 2011

Java2D Stroke Rendering

Summary: converting all use of strokes to rectangles! Why? It seems Java2D strokes render around the co-ordinate system rather than from it.

This may seem intuitive to some, e.g. if you have a stroke thickness set to 4.0, then it renders 2.0 of that to the left of the x co-ordinate and 2.0 to the right. I searched as much documentation as I could to try and see if this was the case but couldn't see it anywhere. I hope this post helps some other newb at some point and prevents a waste of coding time.

I guess this isn't a problem if your project considers this and makes allowances if say for example the user is nudging a widget to the left of the drawing area. The code could ensures they don't go too far left by checking for the x co-ord equal to zero plus half the stroke thickness. I'm hoping my code will be more readable that that when I come back to it in a few years time - consider how to make sure the right width is returned for a widget, ensuring it doesn't go too far right in the drawing area and so on.

Maybe it's just the way I'm wired, I think it's more appropriate to know that x is the left edge of your widget not x - (strokeWidth / 2.0f).

No comments:

Post a Comment