Printing in Java, Part 5
Discover the print framework's support classes
By Jean-pierre Dubé, JavaWorld.com, 07/26/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
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.
Printing in Java: Read the whole series!
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).
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- Download this article's source code and resource files
http://www.javaworld.com/javaworld/jw-03-2001/print/jw-0302-print.zip
- Graphic Java 2, Mastering the JFCAWT, Volume 1, David M. Geary (Prentice Hall, 1998) -- you will find tons of books covering Java AWT, but none will cover the subject as
thoroughly. If you're writing GUIs, you must have this book next to your computer
http://www.amazon.com/exec/obidos/ASIN/0130796662/javaworld
- Migrating from Java 1.0 to Java 1.1, Daniel I. Joshi and Pavel A. Vorobiev (Ventana Communications Group, 1997) -- this book was helpful when Java 1.1 came out,
and it was the first to talk about printing in Java
http://www.amazon.com/exec/obidos/ASIN/1566046866/javaworld
- Java 2D API Graphics, Vincent J. Hardy (Prentice Hall, 1999) -- probably the best book on Java 2D, this book covers all aspects of the 2D API and
provides a
Graphics framework for advanced 2D compositions
http://www.amazon.com/exec/obidos/ASIN/0130142662/javaworld
- "Getting Started with Java 2D," Bill Day, (JavaWorld, July 1998) -- an excellent introduction to the Java 2D API
http://www.javaworld.com/javaworld/jw-07-1998/jw-07-media.html
- "Programmer's Guide to the Java 2D API" (Sun Microsystems) -- a reference document on the 2D APIftp://ftp.java.sun.com/docs/j2se1.3/j2d-book.pdf
- "Test Infect Your Enterprise JavaBeans," Michael T. Nygard and Tracie Karsjens (JavaWorld, May 26, 2000) -- how to use JUnit to test JavaBeans applications
http://www.javaworld.com/javaworld/jw-05-2000/jw-0526-testinfect.html
- "Automate Your Build Process Using Java and Ant," Michael Cymerman, (JavaWorld, October 20, 2000) -- learn to use Ant
http://www.javaworld.com/javaworld/jw-10-2000/jw-1020-ant.html
- "Benefit from Platform-Independent Builds," Sanjay Mahapatra (JavaWorld, August 4, 2000) -- for more on build processes and Ant
http://www.javaworld.com/javaworld/jw-08-2000/jw-0804-builds.html
- "Incremental Development with Ant and JUnit," Malcolm Davis, (developerWorks, November 2000) -- learn to integrate Ant and JUnit
http://www-4.ibm.com/software/developer/library/j-ant/?dwzone=java
- "Java Tip 43How to Read 8- and 24-bit Microsoft Windows Bitmaps in Java Applications," Jeff West, with John D. Mitchell (JavaWorld)
http://www.javaworld.com/javatips/jw-javatip43.html
- The Ant homepage
http://jakarta.apache.org/ant/
- The JUnit homepage
http://junit.org
- The form layout manager
http://www.oz.net/~bhawkins/software.html
- Discuss your technical issues in the Java Forum
http://www.itworld.com/jump/jw-0302-print/forums.itworld.com/webx?14@@.ee6b802
- Sign up for the JavaWorld This Week free weekly email newsletter and keep up with what's new at JavaWorld
http://www.idg.net/jw-subscribe
- Browse JavaWorld's Topical Index
http://www.javaworld.com/javaworld/topicalindex/jw-ti-index.html