Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

String problem

Posted on 2003-03-05
7
Medium Priority
?
309 Views
Last Modified: 2010-08-05
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!
0
Comment
Question by:asukai
7 Comments
 
LVL 92

Expert Comment

by:objects
ID: 8075066
> 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.
0
 

Author Comment

by:asukai
ID: 8076111
->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?
0
 
LVL 92

Accepted Solution

by:
objects earned 320 total points
ID: 8076181
Yes StringTokenizer should be able to handle it for you.

StringTokenizer st = new StringTokenizer(text, "\n\t", true);

StreamTokenizer is another option.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:functionpointer
ID: 8077378
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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8087571
You'll have to print the strings separately, specifying the co-ordinates. drawString () ignores escape sequences or control characters.

Mayank.
0
 

Expert Comment

by:vinay_krishna
ID: 8093490
Use String Tokenizer
0
 
LVL 92

Expert Comment

by:objects
ID: 8095465
> Use String Tokenizer

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question