?
Solved

String problem

Posted on 2003-03-05
7
Medium Priority
?
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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