I can think of two ways to do this.
First make an AffineTransform to rotate and translate the string to the desired position/angle.
Then you can either
1 — transform the graphics context with the transform, or
2 — create a transformed font using the Font
deriveFont method.
In the case of the graphics approach you will need to save the original transform, use your AffineTransform in the Graphics2D
transform method and finally, before exiting the
paintComponent method restore the original transform with the
setTransform method. This is important because the same graphics context can (will often) be passed along by the Container
paint method for drawing borders and child components.
|
Code:
|
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
AffineTransform orig = g2.getTransform();
AffineTransform at = ...
g2.transform(at); // concatenates with orig transform
g2.draw(String(...);
g2.setTransform(orig); // resets to original
} |