All Packages  Class Hierarchy  This Package  Previous  Next  Index

Class com.roguewave.format.TextPicture

java.lang.Object
   |
   +----com.roguewave.format.TextPicture

public class TextPicture
extends Object

The TextPicture class allows numbers to be inserted into a pictorial template. This separates the specification of the position and width of a field from the formatting of text within the field. TextPicture objects allow the programmer to place fields in text in a visually intuitive manner.

In its simplest form the TextPicture class can be used in a manner similar to C's printf functions. 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 (Note that Java automatically converts number into a String):
 public void trivial(String string, int number)
 {
   System.out.println("A " + string + " is worth " + number + "words.");
 }
 
but for more complicated formatting String concatenation can become difficult to read. In complex cases TextPicture's separation of the constant from the variable improves readability:
   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);
 
vs.
   for(int i = 0; i < rows; i++)
     System.out.println("<tr><td> " + col1 + " </td><td> " +
       col2 + " </td><td> " + col3 + " </td></tr>");
 
Not convinced? There's more! 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);  
 

The TextPictureExample2.java example is a small program that produces text tables like this:
 ----------------------------------
 | Credits  |  Debits  | Balance  |
 ----------------------------------
 |     50.0 |          |     50.0 |
 |          |     4.75 |    45.25 |
 |          |    29.75 |     15.5 |
 |          |      8.5 |      7.0 |
 |     50.0 |          |     57.0 |
 |          |     37.5 |     19.5 |
 ----------------------------------
 

See also, the TextAlignment class, which you can use to center, left justify, or right justify values in a table like the one above; and the NumericFormat class, which you can use, for example, to get the values to print out with two digits after the decimal place.

Notice that the precision of the numbers output by TextPicture is determined by the toString() methods of the corresponding wrapper classes in java.lang. This can lead to unexpected results for numbers that cannot be exactly represented. For example, if the entry 29.75 in the "Debits" column above were changed to 29.99, the Balance would show up as 15.260000000000002 instead of the expected 15.26. The QuickFormat class allows the user tighter control over number formatting (including precision) while maintaining the simple interface of TextPicture.

See Also:
TextAlignment, NumericFormat, QuickFormat

Constructor Index

 o TextPicture(String)
Create a TextPicture based on the argument string

Method Index

 o arg(double)
Replace the next blank with the floating point argument.
 o arg(Double)
Replace the next blank with the floating point argument.
 o arg(Float)
Replace the next blank with the floating point argument.
 o arg(float)
Replace the next blank with the floating point argument.
 o arg(int)
Replace the next blank with the integer argument.
 o arg(Integer)
Replace the next blank with the integer argument.
 o arg(long)
Replace the next blank with the argument integer.
 o arg(Long)
Replace the next blank with the argument integer.
 o arg(String)
Replace the next blank with the argument string arg.
 o arg(TextAlignment)
Replace the next blank with the argument TextAlignment.
 o print(PrintStream)
Print self on the argument PrintStream (no implicit newline).
 o println(PrintStream)
Print self followed by a newline on the argument PrintStream.
 o setPictureChar(char)
Set the "blank" char which indicates where substitutions occur.
 o toString()
Return a string representation of self.

Constructors

 o TextPicture
 public TextPicture(String string)
Create a TextPicture based on the argument string

Methods

 o setPictureChar
 public void setPictureChar(char pchar)
Set the "blank" char which indicates where substitutions occur.

 o arg
 public TextPicture arg(String arg)
Replace the next blank with the argument string arg.

 o arg
 public TextPicture arg(int value)
Replace the next blank with the integer argument.

 o arg
 public TextPicture arg(Integer value)
Replace the next blank with the integer argument.

 o arg
 public TextPicture arg(long integer)
Replace the next blank with the argument integer.

 o arg
 public TextPicture arg(Long integer)
Replace the next blank with the argument integer.

 o arg
 public TextPicture arg(float value)
Replace the next blank with the floating point argument.

 o arg
 public TextPicture arg(Float value)
Replace the next blank with the floating point argument.

 o arg
 public TextPicture arg(double value)
Replace the next blank with the floating point argument.

 o arg
 public TextPicture arg(Double value)
Replace the next blank with the floating point argument.

 o arg
 public TextPicture arg(TextAlignment tl)
Replace the next blank with the argument TextAlignment.

 o toString
 public String toString()
Return a string representation of self.

Overrides:
toString in class Object
 o print
 public void print(PrintStream ps)
Print self on the argument PrintStream (no implicit newline).

 o println
 public void println(PrintStream ps)
Print self followed by a newline on the argument PrintStream.


All Packages  Class Hierarchy  This Package  Previous  Next  Index