*
The moose likes Java in General and the fly likes Non-graphical text wrapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Non-graphical text wrapping" Watch "Non-graphical text wrapping" New topic
Author

Non-graphical text wrapping

Rick Crawford
Ranch Hand

Joined: Aug 17, 2001
Posts: 34
Hello all. As usual, I'm trying to do something a little weird here. And as usual, I'm stuck. That's why I'm turning to the Moose for help.
Without going into too much background detail, I need to parse a bunch of words separated by spaces, like a long paragraph, into an list. Each element in the list will be a String containing a section of the paragraph not to exceed a given length. I don't want to split any words. This processing will be done in preparation to print onto (annotate) an image file.
In other words, I need to create a non-graphical word wrapper.
I could write a class that does pretty much I've described to this point (although any sample code someone else already has working would be great.) The real kicker is that I want to be able to specify a font size and face for the method to take under consideration in calculating the contents of each String. I would need to somehow specify a maximum number of pixels(?)
as the length of each line.
This class would also need to calculate the offset between the 'sentence' Strings to print them down the page (the image.)
Here's a high-level use case of how I'd envision this to work:
TextWrapper tw = new TextWrapper();
tw.setTextToWrap("Here's the stuff we want to wrap all over the place, blah, blah, blah");
tw.setFont(new Font("Arial",Font.PLAIN,10);
tw.setMaxPixels(1000);
tw.wrapIt();
for (int i = 0; i < tw.getNumLines(); i++) {
String nextLine = (String)tw.getLine(i);
// write out each line
}
There may be a way to accomplish the end results using awt components, but I haven't been able to figure this out so far.
Thanks in advance for any assistance,
Rick <><
Jason Kilgrow
Ranch Hand

Joined: May 21, 2001
Posts: 47
Originally posted by Rick Crawford:
Hello all. As usual, I'm trying to do something a little weird here. And as usual, I'm stuck. That's why I'm turning to the Moose for help.
...
The real kicker is that I want to be able to specify a font size and face for the method to take under consideration in calculating the contents of each String. I would need to somehow specify a maximum number of pixels(?)
as the length of each line.
...
Thanks in advance for any assistance,
Rick <><

I've actually tried something like this for a jsp application I just wrote. The one really irritating thing about font sizes is that they don't have anything to do with character width. Font sizes are all about character height. How stupid is that?
Anyway, it wasn't exactly how I wanted it but it was pretty close. What I did was calculate an average pixel width and use that number to calculate how many characters I could have on one line. It was sort of kludgy but it worked. I suppose I could have figured out how many pixels there were for each character I had to print, test what character I was printing, and count pixels as I went. Yuk.
If anybody has a better idea, I'm interested...
[This message has been edited by Jason Kilgrow (edited December 18, 2001).]
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
I think that java.awt.Font may help you, but I haven't actually checked if a solution is there.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Try java.awt.FontMetrics (in combination with Font).


"I'm not back." - Bill Harding, Twister
Jason Kilgrow
Ranch Hand

Joined: May 21, 2001
Posts: 47
Thanks David and Jim!
I'm going to try them out!
 
Consider Paul's rocket mass heater.
 
subject: Non-graphical text wrapping