Is there a way to rotate Swing text such as in a JLabel between 0 and 360 (or between -180 and 180) degrees in 1 degree steps?
The text can be rotated by adjusting the positions of the start and end points of the arc. See also How to Uninstall SolidWorks? Instead of an arc, a line or an edge can be used. If a line is used, adjust its angle as desired. In the image below, the text is centered on the sketch line. Without the line, the options for alignment are greyed out.
With the Transform tool active, simply right-click on the text and select Rotate 90° Clockwise or Counterclockwise. You can also rotate a full 180° if you prefer.
By default, JLabel can display a text in the horizontal position and we can rotate a JLabel text by implementing the rotate () method of Graphics2D class inside the paintComponent ().
If blocks are used, they can be rotated. Select the block to be rotated, untick “Lock angle” and input a value. Note: This will rotate the entire block and not just the text, if the block only contains text then there is no issue.
Yes. Look at Graphics2D.rotate(). For a JLabel, I think you could override the paintComponent() method to call rotate(x), then call the existing paintComponent(), then call rotate(-x). e.g.
protected void paintComponent(Graphics g) {
Graphics2D g2 = ( Graphics2D )g;
g2.rotate(theta);
super.paintComponent(g2);
g2.rotate(-theta);
}
I haven't tried this. You might need to add an offset, see Graphics2D.rotate(double theta, double x, double y)
I do not believe that Swing offers explicit support for this.
However, you can turn your text into an image, and rotate that, using the AffineTransform class.
Here is some example code, apparently taken from the book "Swing Hacks", for writing text backwards. You can easily modify it for rotating text, although you will have to add some code for the animation effect.
Not JLabel but JEditorPane content http://java-sl.com/vertical.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With