Printing in Java, Part 3
Jean-Pierre Dubé introduces the print framework that works on top of the Java Print API
By Jean-pierre Dubé, JavaWorld.com, 07/26/01
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
In this third part of a five-part series on printing in Java, I will explain the design of the print framework. This framework
will work on top of the API to ease the burden of creating printed output. With it, you will be able to create pages with
running headers/footers, and insert paragraphs, images, and tables. The coding phase will begin in Part 4 and continue into
Part 5. We have a lot of ground to cover, so let's start.
Printing in Java: Read the whole series!
Goals of the print framework
In Parts 1 and 2, you had the opportunity to examine in detail the Java Print API. With this knowledge, you are now able to
evaluate its strengths and weaknesses and build a framework on top of it. That framework will compensate for the lack of high-level
features in the Java Print API.
The print framework will:
- Be easy to use.
- Provide the high-level functionality required to efficiently render pages.
- Loosely couple with the Print API. Because the framework has no direct ties to the Java Print API, it will allow greater support
for other output formats, such as PDF or HTML.
- Provide a structure that clearly defines each component involved in creating documents (document, page, paragraph, and so
on).
- Use an abstract measurement system. Developers will thus be able work with the measurement system of their choice or even
create their own. The framework will provide three default units of measurements: inches, centimeters, and points.
- Provide a print-preview facility. The Java Print API has no support for previewing the output before you print.
- Support a standard and portable page-setup dialog. As mentioned in Part 2, even though Java has been categorized as a WORA (Write Once, Run Anywhere) language, its functionality varies from platform
to platform. The print framework will provide one page-setup dialog that will be uniform across all platforms.
- Offer export features. Although the framework will support several output formats (PDF, HTML, Postscript), their implementation
is beyond the scope of this series. I leave the implementation up to you.
- Offer text handling, which is of primary importance. The framework will provide all the functionality needed to effectively
render text, including right justification, left justification, full justification, and support for the
AttributedString class.
- Support graphics primitives such as rectangles, circles, and lines.
- Support GIF and JPEG image types.
- Support headers and footers. These can be set at the document or the page level, and the first page can feature a different
header/footer.
- Support sticky position, which easily sticks a component to a specific location. As an example, let's assume that you want to center an object in
a page. Instead of doing all the math yourself, you can set the vertical and horizontal sticky value to a
CENTER value; the framework centers the object within its parent container -- in this case, the page. Since all print objects are
containers, the sticky values would be applied within the boundaries of the parent object.
- Support sticky dimensions, with which you can set a component's width and/or height to the parent container's width and/or height.
- Support tables, though their implementation will be limited.
In addition, all print objects are containers in the print framework.
- Digg
- Reddit
- SlashDot
- Stumble
- del.icio.us
- Technorati
- dzone
Resources
- 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 to
the extent that this one does. If you're writing GUIs, you must have this book next to your computer
http://www.amazon.com/exec/obidos/ASIN/0130796662/o/qid=970872039/sr=2-2/102-2403269-9106541
- 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 was the first to talk about printing in Java
http://www.amazon.com/exec/obidos/ASIN/1566046866/o/qid=969489411/sr=8-1/ref=aps_sr_b_1_3/002-2488903-6332023
- 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 also provides a
Graphics framework for advanced 2D compositions
http://www.amazon.com/exec/obidos/ASIN/0130142662/qid=969489696/sr=1-1/002-2488903-6332023
- An excellent introduction to the Java 2D API"Getting Started with Java 2D," Bill Day (JavaWorld, July 1998)
http://www.javaworld.com/javaworld/jw-07-1998/jw-07-media.html
- A reference document on the 2D APIProgrammer's guide to the Java 2D API, Sun Microsystemsftp://ftp.java.sun.com/docs/j2se1.3/j2d-book.pdf
- For more articles on printing in Java, browse JavaWorld's Topical Index
http://www.javaworld.com/javaworld/topicalindex/jw-ti-index.html
- 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