Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

line return in drawString()  RSS feed

 
James Kennedy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code:

output = letters[x]+ "\n" + digits[x];
g.drawString(output,x,y);
instead of getting a letter with a digit underneath it, I'm getting a letter, a symbol and a digit all on the same line. What am I doing wrong?
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,
I suggest trying this, instead:

Hope it helps.
Good Luck,
Avi.
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello James,
Considering that you want a letter & a digit on two separate lines - please remember that the graphics context doesn't work on a line basis. Instead it works on a pixel basis. What this means is that you have to explicitly place your String's where you want them to appear.
The modified code below will get you your desired outcome -

The symbol you get is probably because the graphics context can't render the newline character sequence.
On thought - if you want some formatted output you can also use HTML tags on some components.
HTH
Ashish Hareet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!