I’m making a GUI that includes a graph and I figured out how to create a BasicStroke that makes a dotted line and apply it to a Graphics2D object, so when I call drawLine() the line is dashed. What I can’t figure out is if there is a simple way to make a dashed line continuous across multiple line segments. That is, I have an array of values that I use as the location of points on the same line of a graph, but when I draw each line segment separately and apply a dashed line stroke the dash pattern starts again for every line segment. Some of my line segments are of varying lengths, so the dashed pattern looks all messed up (and in places where the line segments are shorter than the pattern it looks like a continuous, non-dashed line).
Is there any simple (or not so simple) fix to this and to get a continuous dashed appearance across the different line segments? Let me know if any part of my explanation is unclear.
You could use a Path2D.Float (or Path2D.Double). These have .moveTo and .lineTo functions to connect your segments. Once this is created, draw it using Graphics2D.draw(Shape s).