File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes rotate text Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "rotate text " Watch "rotate text " New topic
Author

rotate text

Raschin Ghanad
Greenhorn

Joined: Jul 28, 2004
Posts: 10
can anyone help me, I am trying to rotate the text for the scale of my x-axis, but when I use the Graphics2D rotate-method, everything disappears...

I did it the following way:

g2.rotate(45.0d)
drawString("200",10,10);
g2.rotate(-45.0d)

Why don't I see anything?
Maybe I have to use other coordinates?
I tried many variants, but non worked...

Maybe theres another way to rotate a text?
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I think that in your second line that you need
g2.drawString("200",10,10);

Cheers,
Rachel
Raschin Ghanad
Greenhorn

Joined: Jul 28, 2004
Posts: 10
sorry, I forgot to write it correctly...
I did it with g2.drawString(...)

the real code was:



I tried to write it in an easy, understanding way...
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Rotating the graphics context happens at the origin, ie, at (0,0) of your JPanel so the rendered text is somewhere off the screen in the direction of 45 radians which is Math.toDegrees(45) % 360 = 58.31 degrees clockwise. 45 degrees is Math.PI/4 or Math.toRadians(45).

The java tutorial has some resources that you might enjoy:

The Performing Custom Painting lesson in the Creating a GUI with JFC/Swing trail covers the basics and contains links to more in–depth material.

The specialized trail 2D Graphics takes you a little farther. The Transforming Shapes, Text, and Images page in the Displaying Graphics with Graphics2D lesson has a discussion of rotating the graphics context.

Here's a demo:

[ August 04, 2004: Message edited by: Craig Wood ]
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
You can also use Graphics2D.setComposite(AlphaComposite.getRotateInstance(theta)) to rotate text or anything else you want to draw.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: rotate text
 
Similar Threads
Rotating a JWindow with multiple JPanels
Collision detection with rotating shapes
regarding JTableHeader
Change text orientation on canvas
How to turn the text vertical in graphics?