• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

rotating a (String) object ninety degrees

 
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I extend (JPanel) and in my (paintComponent()) method I call (drawString()) on a (Graphics) object, I can pass in a (String) object as my first parameter and cause that (String) object to be printed horizontally in my (JPanel) window. Is there a way to print that (String) object vertically instead of horizontally? I need to switch each of the characters in the (String) object so that they occur one on top of the other instead of horizontally next to each other, and I also need to rotate each of the characters ninety degrees. Does anybody know how to do that?
 
Bartender
Posts: 7488
171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By chance, I have an age-old piece of code around that does exactly that. Well, it also writes an image file with the result, but to do that it needs to tweak the Graphics2D to begin with.
 
Marshal
Posts: 77162
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I would do it requires a second Graphics object. I have had difficulty with Graphics objects when I apply an affine transform to them; it doesn't seem to reverse properly. It is usually shear() that causes problems; translate() and scale() can usually be reversed.Graphics#create() method. Graphics2D#rotate() method.
 
Saloon Keeper
Posts: 9831
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kevin, can you provide a screen snapshot as to how you expect it to look. From your description I'm guessing (?) you want:
A
B
C

and not just ABC rotated 90 degrees.
 
Rancher
Posts: 3288
30
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe instead of custom painting on your panel you can use Icons.

Check out:

1) Text Icon
2) Rotated Icon

They might allow you to be creative and use any component that can display Icons instead of doing the custom painting.
 
Kevin Simonson
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Kevin, can you provide a screen snapshot as to how you expect it to look. From your description I'm guessing (?) you want:
A
B
C

and not just ABC rotated 90 degrees.


I don't know how to provide a screen snapshot; I was hoping on using the Java interpreter to display it, and I can't figure out how to do that; I don't know any other way to display it. But in response to your guess, I actually do not want
A
B
C
instead I do want ABC rotated 90 degrees.
 
Carey Brown
Saloon Keeper
Posts: 9831
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's the easier of the two problems. Campbell's suggestion should work for you.
 
Kevin Simonson
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The way I would do it requires a second Graphics object. I have had difficulty with Graphics objects when I apply an affine transform to them; it doesn't seem to reverse properly. It is usually shear() that causes problems; translate() and scale() can usually be reversed.Graphics#create() method. Graphics2D#rotate() method.


Campbell, using what you said I wrote:

Then when I ran it with

I got a large (JFrame) window with "DsgRot Cube_15_10.0" in the upper left corner, but nothing actually appeared in the window; it's all completely white. Any idea what I'm doing wrong?
 
Kevin Simonson
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:The way I would do it requires a second Graphics object. I have had difficulty with Graphics objects when I apply an affine transform to them; it doesn't seem to reverse properly. It is usually shear() that causes problems; translate() and scale() can usually be reversed.Graphics#create() method. Graphics2D#rotate() method.


Actually, let me make it easier. I tried:

And then I executed "java Ritchie", and got a window with a white strip on top with an icon and "Ritchie" on the left, and then underneath it I got an apparently 500 pixel by 500 pixel square that was completely gray; there's no trace of a green "Campbell is brilliant" anywhere near the center of the square where it should have appeared. Once again, what am I doing wrong?
 
Rob Camick
Rancher
Posts: 3288
30
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


When you do the rotate the string gets drawn down 250 and to the left 250, so effectively it is painted off the panel.

Instead try:


to see the difference.

If you want the starting point to be (250, 250) then try:




Another example to get you started:

 
Kevin Simonson
Ranch Hand
Posts: 244
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:

When you do the rotate the string gets drawn down 250 and to the left 250, so effectively it is painted off the panel.


Thanks! I got my code working the way I like it. I really appreciate the help.
 
Campbell Ritchie
Marshal
Posts: 77162
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot that rotation is around the (0, 0) point, so you would want to translate it by (width / 2, height / 2) (or similar) to move the origin to the centre of the display.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic