• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Non-graphical text wrapping

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 <><
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that java.awt.Font may help you, but I haven't actually checked if a solution is there.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try java.awt.FontMetrics (in combination with Font).
 
Jason Kilgrow
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks David and Jim!
I'm going to try them out!
 
I've been selected to go to the moon! All thanks to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic