I’m using Java AWT to draw lines on a panel (Line2D and Graphics2D.drawLine()) and I’m wondering how I can draw a line with tick marks, similar to:
|—-|—-|—-|—-|—-|
I know the positions I’d like to draw the ticks at in advance.
The lines could be in any position, so the ticks must be drawn at an angle releative to the line itself.
My basic geometry & ability to apply it in Java is failing me. 🙂
I suggest you
Math.atan2.AffineTransformwith translation and rotation before invoking the ruler-drawing-method.Here is a complete test-program. (The
Graphics.createmethod is used to create a copy of the original graphics object, so we don’t mess up the original transform.)Note, that you could just as easily draw numbers above the ticks. The drawString-calls would go through the same transformation and get nicely “tilted” along the line.