Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Drawing a GeneralPath using a dashed stroke

 
Peter Kolb
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have recently started to support dashed strokes in my application and found that dashed strokes are not well suited for drawing GeneralPaths. The unstroked segments are too small, and at one point they disappear, making the dashed GeneralPath look like a solid GeneralPath.
Here is some sample code to play:

The line and the rectangle are drawn correctly with a dashed stroke, but the Path2D.Double is a continuous line.
Can anybody reproduce it, and can this be solved?
This code was tested on WinMe and WinXP, java build 1.6.0_22-b04.
Thanks for help,
Peter
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aren't you supposed to set the Stroke before you draw the GeneralPath?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that isn't the problem. I shall try something different.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are doing is drawing a line down to the right, then when you close the path, you are coming back. So all your dashes are overwriting the gaps. Try changing the first lineTo to read lineTo(401, 301), then you aren't drawing a double line, and it should work for you.
 
Peter Kolb
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, that did it!

However, some real-world cases are still misbehaved. I probably need to look more careful at the construction of the corresponding GeneralPaths.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50235
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic