Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate Swing text?

Tags:

java

swing

jlabel

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?

like image 888
jacknad Avatar asked Dec 19 '11 14:12

jacknad


People also ask

How to rotate text in SolidWorks?

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.

How do I rotate text 90 degrees?

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.

How to rotate a JLabel text using JLabel?

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 ().

How do I rotate a block in illustrator?

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.


3 Answers

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)

like image 71
user949300 Avatar answered Oct 19 '22 14:10

user949300


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.

like image 36
S.L. Barth Avatar answered Oct 19 '22 16:10

S.L. Barth


Not JLabel but JEditorPane content http://java-sl.com/vertical.html

like image 37
StanislavL Avatar answered Oct 19 '22 15:10

StanislavL