This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrinting a string vertically  RSS feed

 
Leonardo Fernandes
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I would like to write a string vertically (i.e from bottom up instead of from left to right). Is there a way to do it or to rotate a string 90 degrees?
Thanks.
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write a string in what? The System.out?
Pretty easy, actually.
Reverse the String, then loop & print, one char on each line.
 
Phil Chuang
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And now seeing as how this is the Swing/JFC/AWT forum, I assume you want to do it in a GUI
The basic idea is still the same:
Reverse the string, iterate through it adding one char at a time to a stringbuffer. But between the chars put in a newline character. Then output it however you want.
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a rotate method in Graphics2D. If you cast your graphics object to Graphics2D. You can call this method.
 
Leonardo Fernandes
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I appreciate your help, but what i mean was not printing something like:
a
v
a
J
I would like, if possible, to print every character in the same orientation as the string. Yes, I want to make it on GUI, swing preferred.
 
Leonardo Fernandes
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I cast a string to Graphics2D? How?
Thanks
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you cannot cast a String to Graphics2D.
If you overload the paint method of your component you can use the drawString() method in combination with the rotate method to draw the string you need. So you will probably have to do something like this.

I am not sure if this is exactly what you need, it a made up example. But you will have to do something like it.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!