• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

rotate text

 
Raschin Ghanad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that in your second line that you need
g2.drawString("200",10,10);

Cheers,
Rachel
 
Raschin Ghanad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use Graphics2D.setComposite(AlphaComposite.getRotateInstance(theta)) to rotate text or anything else you want to draw.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic