Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Printing in Java, Part 5

Discover the print framework's support classes

  • Print
  • Feedback
Welcome to the fifth and final installment of "Printing in Java." In Part 1 you learned about the different models that the Java API uses to produce printed output, and Part 2 presented code examples of those models. Part 3 introduced you to the print framework. Most recently, in Part 4, you learned about the base classes that form the print framework, as I explained the PFDocument, PFPage, PFPrintObject, and all the measurement classes. In Part 5 I will focus my explanations on what I call the support classes of the print framework. I will start with the PFParagraph class.



Handling text

The PFParagraph class is essential to the print framework. It provides all the basic functions needed to render text. By using PFParagraph, you will be able to render text with the following alignments: left, right, center, and fully justified. In addition, the class enables you to set the text color and the font size. PFParagraph also supports two text input methods. You can use either the standard String class, or if you need a more elaborate way of inputting text, you can use an AttributedString.

PFParagraph also provides all the previously explained functions of the PFPrintObject. Although PFParagraph does not provide direct support for margins, you can use the implementation provided by the PFPrintObject. You will find PFParagraph's code in Listing 1.

Listing 1: PFParagraph As most of PFParagraph is pretty straightforward, I will focus only on its rendering methods. The rendering process starts in the print method located at line 179 in Listing 1. First, the method sets the text color and the text font. Then, depending on the horizontal alignment, the method calls the proper text renderer. When finished with the rendering process, the print method calls the printChilds() method to render any child object that it may contain.

There are four private methods available to render text: renderLeftJustfied(), renderRightJustified(), renderCenterJustified(), and renderFullyJustified(). The renderLeftJustified() method (line 387) uses nearly the same code as that presented in Part 2, Listing 4.

Line 325 shows how renderCenterJustified() differs from renderLeftJustified(). To calculate the object's center, divide the width of the object by two. Then divide the width of the string -- which the layout.getAdvance() method obtains -- by two. Next, subtract the object's center from the string's center to obtain the position where the string will be rendered.

The renderRightJustified() method resembles renderLeftJustified() except that you subtract the string's width from the object's width (i.e., you compute the same math without the divisions).

  • Print
  • Feedback

Resources