I'm drawing a graph with two points of each point having a line with a weight.
for example graph: point "15" to point "16" line with the weight of 1.872 and point "16" to point "15" with the weight of 1.567.
take a look at my graph for now:
I want to draw a String with always parallel (adjacent) to the line.
I calculated the slope for the straight and the angel I did calculate is the arctan of this slope:
I had use this function to rotate the string:
public static void drawRotate(Graphics2D g2d, double x, double y, double angle, String text) {
g2d.translate((float)x,(float)y);
g2d.rotate(Math.toRadians(angle));
g2d.drawString(text,0,0);
g2d.rotate(-Math.toRadians(angle));
g2d.translate(-(float)x,-(float)y);
}
With the angle of arctan((y2-y1)/(x2-x1)= the slope of the line ) and it didn't work well.
How can I rotate this String to run parallel always to the line I draw?
My goal: Draw the String like this example


Here is a quick demo to be used as a guide on how it might be done. I omitted some things like the arrowheads since that is just busy work. And I guesstimated on the label positions. I would recommend you read about the three argument version of
Graphics.rotate()andRenderingHintsandanti-aliasingto smooth the lines.You may want to write general methods to facilitate positioning the text and labels based on font size.
But I believe your primary problem was doing int division when calculating the slope.
Shows