• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

rotate text

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use Graphics2D.setComposite(AlphaComposite.getRotateInstance(theta)) to rotate text or anything else you want to draw.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic