The TextPicture class allows strings to be inserted into a pictorial template. This class separates the specification of the position and width of a field from the formatting of text within the field. TextPicture objects allow you to place fields in text in a visually intuitive manner.
In its simplest form, the TextPicture class can be used like the C language printf() function. In keeping with Java's String-centered I/O, TextPicture allows you to substitute values into a line of text:
public void trivial(String string, int number) { TextPicture picture = new TextPicture("A _ is worth _ words."); System.out.println(picture.arg(string).arg(number)); }
An alternative way to write the same thing is:
public void trivial(String string, int number) { TextPicture picture = new TextPicture ("A _ is worth _words.").arg(string).arg(number); picture.println(System.out); }
For this simple example there is no clear advantage over Java's String concatenation:
public void trivial(String string, int number) { System.out.println("A " + string + " is worth " + number + "words."); }
For more complicated formatting, however, String concatenation can become difficult to read. In complex cases, a separation of the constant from the variable improves readability. Compare this code:
TextPicture fmt = new TextPicture("<tr><td> _ </td><td> _ </td><td> _ </td></tr>"); for(int i = 0; i < rows; i++) fmt.arg(col1[i]).arg(col2[i]).arg(col3[i]).println(System.out);
With this code:
for(int i = 0; i < rows; i++) System.out.println("<tr><td> " + col1 + " </td><td> " + col2 + " </td><td> " + col3 + " </td></tr>");
In addition, TextPicture allows you to specify field widths pictorially.
String hline = "----------------------------------"; String headers = "| Credits | Debits | Balance |"; String row = "| ________ | ________ | ________ |" TextPicture fmt = new TextPicture(row); System.out.println(hline); System.out.println(headers); for(int i = 0; i < rows; i++) fmt.arg(credits[i]).arg(debits[i]).arg(balances[i]).println(System.out); System.out.println(hline);
This would produce a text table like this:
---------------------------------- | Credits | Debits | Balance | ---------------------------------- | 50 | | 50 | | | 4.95 | 45.05 | | | 29.99 | 15.06 | | | 8.5 | 6.56 | | 50 | | 56.56 | | | 37.5 | 19.06 | ----------------------------------
©Copyright 2000, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.