Link to home
Start Free TrialLog in
Avatar of asukai
asukai

asked on

String problem

I asked the following question before but stuck on implementation of the answer. i wouod appriciate if anyone could specifically tell me how to do it or provide me some fragment of codes.

Thanx
=====================================================
When I draw a string with the drawString method of the Graphics class, it ignores escape characters such as \n.

For example if I execute the following code:

str="TESTING TEXT: newline:\n tab:\t END";
g.drawString(str, pvalx, pvaly+ascent);
System.out.println(str);

System.out.println cmmand prints out the string in a correct format on the command prompt screen, like

TESTING TEXT: newline:
tab:    END


but drawString ignores the escape characters and prints out text in a single line like

TESTING TEXT:newline:tab:END

Can anyone please tell me how to overcome this problem so that the string can be drawn  in the correct format?

Thanx

====================================================
Yes, you should break your text into separate strings and
put them in place one by one.
It's easy to write code that puts every next line so-many pixels lower.

;JOOP!
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> you should break your text into separate strings and
put them in place one by one.

Thats exactly what you have to do.
drawString() does no special interpretation of control characters.
Avatar of asukai
asukai

ASKER

->drawString() does no special interpretation of control characters

Yes. I relized that.  But I don't know exactly how I can  break text into separate strings and
put them in place one by one. Do I have to use StringTokenizer and StringBuffer to do this?
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if your drawing to an image, you have to respect the bounderies of the image. Something like this:

Graphics2D g2 = image.createGraphics();
Font font = new Font( face, style, size );
g2.setFont( font );
Rectangle2D rect = font.getStringBounds( text, g2.getFontRenderContext() );

Now you can see how the text you are writing relates to the image to which you are writing. Assuming all characters are going to be written in the same Font, do some math determine positioning, then split your text, looking for logical breaks on whitespace and your newline '\n', split it up and go to town. You could even implement this so that if your image is too small, you decrement the font size and start over until it fits or you reach some exception point.

Hope this helps you along. Good luck.
You'll have to print the strings separately, specifying the co-ordinates. drawString () ignores escape sequences or control characters.

Mayank.
Use String Tokenizer
> Use String Tokenizer

Please read discussion so far, and try not to repeat previous comments :)