wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Drawing a GeneralPath using a dashed stroke Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Drawing a GeneralPath using a dashed stroke" Watch "Drawing a GeneralPath using a dashed stroke" New topic
Author

Drawing a GeneralPath using a dashed stroke

Peter Kolb
Greenhorn

Joined: Nov 19, 2008
Posts: 8
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

Joined: Oct 13, 2005
Posts: 37923
    
  22
Aren't you supposed to set the Stroke before you draw the GeneralPath?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
No, that isn't the problem. I shall try something different.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37923
    
  22
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

Joined: Nov 19, 2008
Posts: 8
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

Joined: Oct 13, 2005
Posts: 37923
    
  22
You're welcome
 
jQuery in Action, 2nd edition
 
subject: Drawing a GeneralPath using a dashed stroke
 
Similar Threads
percentage based background scheme for a JPanel
AffineTransform "transform" method
A flatland puzzle
Making a diagram interactive???
Turning images around the clock