-1

I want to rotate the text present on my JComponent to vertical, JComponent also contains border painted in paintComponent method, I don't want to rotate that border, only text.

I already used graphics2D rotate function, but it rotate component border as well, which fails when my component is rectangle.

Please suggest me any approch to rotate only text. This is currently my JComponent :

enter image description here

And what I want :

enter image description here

Actually it's not duplicate. I already used below code:

  Graphics2D g2 = (Graphics2D) g;
                g2.rotate(Math.PI / 4, bi.getWidth() / 2, bi.getHeight() / 2);

but problem is that, it also rotate border, I don't want that.

All the solutions given are not working, it rotate the border as well. I don't want to rotate the border, only text. This is what I get after rotate with some angle: enter image description here

Sandeep Kokate
  • 825
  • 4
  • 16
  • instead of coding , you can easily rotate your photo then use it in your code – Mohsen_Fatemi Oct 03 '16 at 07:18
  • @Mohsen_Fatemi, actually it's text, not photo, that is my JComponent with white border, in which I am drawing different text – Sandeep Kokate Oct 03 '16 at 07:25
  • Hi @kevto, actually I already referred that example, but it's not what I need. Is there some approch, where I can rotate only certain part of the JComponent? – Sandeep Kokate Oct 03 '16 at 07:27
  • 1
    i think here is what you want : http://stackoverflow.com/questions/14324460/rotating-a-jtextfield-vertically – Mohsen_Fatemi Oct 03 '16 at 07:39
  • @Mohsen_Fatemi : Hi thanks for the comment, but link you provided is also not working for me. approch given in that also rotate border of my jcomponent. – Sandeep Kokate Oct 03 '16 at 08:20
  • In your code you talk about rotating text. But in the code you post you are drawing an image. That doesn't make sense. Post a proper [SSCCE](http://sscce.org/) that demonstrates what your are doing. Only once a proper `SSCCE` is posted will I vote to reopen the question. – camickr Oct 03 '16 at 14:34
  • Did you tried to give a JPanel your static border and adding your JLabel / image on this Panel ? You could rotate the text the same way without moving the border I suppose ? – Kapcash Oct 03 '16 at 15:46

1 Answers1

-1

Remember that the Graphics object has a lot of state in it. This includes the current transformation. Your current code modifies the state and does not restore it to the original situation.

After drawing your image, you should "undo" the rotation. This can be done either by rotating in the other the direction or by creating a new graphics object (g2.create()) specifically to draw the rotated content. If you do the latter, make sure to dispose() the temporary graphics object you created.

jackrabbit
  • 5,525
  • 1
  • 27
  • 38