• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Text wrapping

 
Rick Crawford
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted this question over in the 'Java Intermediate' forum, but thought the nature of my question might be better suited for this forum.
Without going into too much background detail, I need to parse a bunch of words separated by spaces, like a long paragraph, into a list (probably an ArrayList.) 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 is a non-graphical application (it's a web application.) This processing will be done in preparation to print onto (annotate) an image file.
In other words, I need 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 much appreciated.) 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
}
Any suggestions to help me get started in the right direction on this? Thanks in advance for any assistance,
Rick <><

 
Rick Crawford
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I think I may have figured out the solution to my original question, only to discover another problem. The code below seems to do precisely what I want, but when the process (the call to my formatText method) finishes, the application won't 'quit' on its own. I have to add a System.exit(0) to kill it, and that may cause problems once I port this code into my complete application.
Based on process of elimination, the line of code that's hanging things is this one:
FontMetrics fm = myCanvas.getFontMetrics(new Font("Arial",Font.PLAIN, 12));
Here's the complete code:

I tried another method, but got the same results (plus a bunch of deprecation warnings):

Like I said, this all seems to be working but this one itty bitty glitch. Hopefully someone can spot what I'm doing wrong, or can suggest a better way to get the fontMetrics numbers I need to calculate a stringWidth based on a given font face and size.
Thanks,
Rick <><

[This message has been edited by Rick Crawford (edited December 19, 2001).]
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rick,
Not sure if you got an answer to this yet. But I have one. Since you want fonts you are going to have to deal with AWT. Since you also mentioned Size we can just subclass a java.awt.Component and use its fonts and size. I also can make use of the BreakIterator that handles neat stuff like hyphens and stuff.

Enjoy,
Manfred.
 
Rick Crawford
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manfred,
Thanks so much for the code! I just cut 'n pasted it into JBuilder and tried it.. and it works great! I also got a kick out of the paragraph you used to test with!
One question though. I notice that your version has a feature similar to mine: there has to be a System.exit in the code to kill it. I had to cancel/reset the application within JBuilder if I take this out. This might or might not be a problem in my real application, which is a web-based application; I'll just have to give it a try. Is the requirement for a call to System.exit to kill the process an AWT thing there's just no getting around?
Once again, I'm extremely grateful for the help!
Rick <><
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rick,
If your writing an Applet you should NEVER use System.exit method because it kills the JVM and that is disasterous for browsers. It should only be called for applications.
For applets it is left to the browser to kill the applet and the JVM.
Regards,
Manfred.
 
Rick Crawford
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manfred,
My application is going to be running within a servlet. The application server I'm using is Tomcat. Basically, I'll receive a request from a browser that will contain user entered
'remarks' that may range from zero to dozens of words. Without getting into too much detail, I then have to dynamically format these comments into a print area with a specific font face and size. I don't want to hard-code these formatting parameters in the servlet. Instead, I'd prefer to read them in from a properties or an XML configuration file.
I may have nothing to worry about. As soon as Tomcat is finished with my request, the AWT thread may just die of its own accord. I guess I'm a little leery, since I don't know for sure.
Thanks so much,
Rick <><
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever you use any AWT functionality ( i.e. the Canvas you mentioned ) the AWT thread will not die unless you explicitly tell it to ( via System.exit() ). This is because GUIs tend to sit around doing nothing for a long time ( waiting for user input... ) and you don't want your GUI to just die because of inactivity. No GUI components ever have to show up on the screen... just refer to a GUI component in your code, and... BAM!... you have to explicitly exit your program.

That being said... I'm not sure how a servlet would handle this...

-Nate
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic