Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Printing in Java, Part 5 - JavaWorld March 2001

Printing in Java, Part 5 - JavaWorld March 2001

Tutorial Details:

Printing in Java, Part 5
Printing in Java, Part 5
By: By Jean-Pierre Dubé
Discover the print framework's support classes
elcome 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!
Part 1: Acquaint yourself with the Java printing model
Part 2: Print your first page and render complex documents
Part 3: Jean-Pierre Dubé introduces the print framework that works on top of the Java Print API
Part 4: Code the print framework
Part 5: Discover the print framework's support classes
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).
The renderFullyJustified() method differs from the other private rendering methods. With renderLeftJustfied() , renderRightJustified() , and renderCenterJustified() , all paragraph lines are justified, whereas in a fully justified paragraph, the last line should not be justified. Since the LineBreakMeasurer class cannot determine when it has reached the last line in a paragraph, you must implement that function yourself. Fortunately, the solution to the problem is simple: just scan and store each line in the paragraph in a Vector object (lines 423-449). Then, the method enters into a for loop to render each line stored in the previously created Vector . Use an if statement (line 459) to trap the last line and render it as left justified. At line 460, note the use of the getJustifiedLayout() method; it fully justifies a line of text, a useful feature of the TextLayout class.
To ease the layout of paragraphs on a page, I implemented the following two methods in PFParagraph : getTextHeight() , which obtains the height of a paragraph, and getNextParagraphPosition() , which obtains the next paragraph location.
Use the getNextParagraphPosition() method when you want to lay out several paragraphs one after the other, as shown in Figure 1:
Figure 1. Paragraph layout using the getNextParagraphPosition method
To achieve the result in Figure 1, I used the following code excerpt:
1|PFParagraph paragraph = new PFParagraph ();
2|PFParagraph paragraph1 = new PFParagraph ();
3|
4|page.add (paragraph);
5|paragraph.setText (text);
6|paragraph.setHorizontalAlignment (PFParagraph.FULL_JUSTIFIED);
7|paragraph.setSize (new PFSize (page.getPrintableAreaSize ().getWidth (), new PFInchUnit (6)));
8|
9|page.add (paragraph1);
10|paragraph1.setText (text1);
11|paragraph1.setHorizontalAlignment (PFParagraph.FULL_JUSTIFIED);
12|paragraph1.setSize (new PFSize (page.getPrintableAreaSize ().getWidth (), new PFInchUnit (6)));
13|paragraph1.setPosition (paragraph.getNextParagraphPosition ());
For the sake of clarity, assume that the text was assigned to paragraph and paragraph1 . The trick resides at line 13, where paragraph 's getNextParagraphPosition() method calls setPosition() to obtain the next paragraph's position. Suppose you want to print a file using the framework. To do that, create a PFParagraph instance for each line and use the getNextParagraph() method to lay out each line. To know when it's time to create another page, use the getTextHeight() method. Let's review what you have learned so far about rendering text using the print framework. The PFParagraph class enables you to render text with the following justifications: left, center, right, and full. The class will accept text as either a String or an AttributedString object. The PFParagraph will also provide metrics to help you lay out text objects. Next, I will introduce the drawing primitives available in the framework.
Drawing primitives
The print framework offers three drawing primitives
The PFFrame to draw rectangles
The PFLine to render lines
The PFCircle to render circles
Those three primitives enable you to enhance your printed output and can form the base for drawing more complicated figures.
Rectangles
In the print framework, the PFFrame class represents a rectangle drawing. Don't confuse it with the PFRectangle , which the measurement system uses. You can render rectangles with several attributes: line thickness, fill color, and line color. Since all the attribute methods are basically setters/getters, I will focus on the print() method located between lines 130 and 170 in Listing 2.
Listing 2: PFFrame
First, you compute the size and position of the PFFrame object by calling the computePositionAndSize() method (line 145). Next, a Rectangles2D.Double object is created (line 148). Use that rectangle object as your rendering object. Notice the use of the getDrawingOrigin() and getDrawingSize() methods, which set the rectangle's position and size. To set the line thickness, use a BasicStroke object (lines 160-161). Finally, the rendering process takes place at line 163. But just before you print the child objects, restore the Graphics2d object to its previous color.
As the PFCircle and PFLine classes work in a similar way as PFFrame , I will not go into their details.
The next rendering class that you will explore is the PFImage class, which will enable you to render GIF and JPEG images.
Images
At first glance, rendering images might look like a tedious process, but thanks to the Java Print API, rendering images has never been easier. The PFImage class implementation is also extremely simple. To render an image, you must set the image URL using the setURL() method. The method will save the URL and load the image in memory. The print() method, located between lines 76 and 98 in Listing 3, uses the same structure as the previously explained print methods. The computePositionAndSize() is called first, and then the Image object is rendered. Finally, the child objects are rendered.
Listing 3: PFImage
Of course, you could extend this method to support more image formats. You could use the JAI (Java Advanced Imaging) library from Sun to extend this class or use JavaWorld 's Java Tip 43 (Jeff West, with John D. Mitchell) to add support for 8- and 24-bit bitmap images.
Print Preview window
I started to present the structure of the print-preview window in Part 3 . Recall that the print-preview window is divided into two major classes. The first one, the PFPrintPreview , renders the page on screen; the second class, the PFPrintPreviewToolbar , presents the toolbar and calls the appropriate method when the user selects a button. The PFPrintPreviewToolbar is an inner class of the PFPrintPreview . Let's take a look at the PFPrintPreviewToolbar class first, located between lines 369 and 488 of Listing 4.
Listing 4: PFPrintPreview
Aside from creating the toolbar, the only other thing worth mentioning about PFPrintPreviewToolbar is that you must pass a PFPrintPreview object to the constructor (line 403). The toolbar sends user requests to the PFPrintPreview . The ActionPerformed() method completes the routing (lines 462-486).
The PagePanel class performs a page's rendering process (lines 270-366). I will focus my explanations on the paint() method (lines 338-364). The PagePanel class implements a JPanel object. You override the paint() method to render the page. Next, create a scaleFactor variable (line 348) that keeps the page's proportion relative to a printed page. At this time, the print preview supports only letter-sized pages. For an 8.5-by-11-inch page, the scale factor is 0.77. You obtain this value by dividing the page's width by its height.
At this point, a BufferedImage object is created (line 350). That object will be used to render the p


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Printing in Java, Part 5 - JavaWorld March 2001

View Tutorial:
Printing in Java, Part 5 - JavaWorld March 2001

Related Tutorials:

3D graphics programming in Java, Part 3: OpenGL
3D graphics programming in Java, Part 3: OpenGL
 
How to write a Java Card applet: A developer's guide
How to write a Java Card applet: A developer's guide
 
Web services hits the Java scene, Part 1
Web services hits the Java scene, Part 1
 
Java security evolution and concepts, Part 5
Java security evolution and concepts, Part 5
 
I want my AOP!, Part 2
I want my AOP!, Part 2
 
Achieve strong performance with threads, Part 1
Achieve strong performance with threads, Part 1
 
Achieve strong performance with threads, Part 2
Achieve strong performance with threads, Part 2
 
Java's character and assorted string classes support text-processing
Java's character and assorted string classes support text-processing
 
Test email components in your software
Test email components in your software
 
Good, but obsolete
Good, but obsolete
 
Very interesting
Very interesting
 
Lisp and Java
Lisp and Java In this article, we\'re going to steal an idea from one of the most theft-worthy languages out there: Lisp. We\'re going to pick out one of its most useful features -- the ability to treat functions as data -- and talk about how to apply th
 
Clustering and Load Balancing in Tomcat 5, Part 2
Clustering and Load Balancing in Tomcat 5, Part 2
 
Taming Tiger, Part 3
J2SE 5—code named "Tiger"—is the most significant revision to the Java language since its original inception. Tarak Modi's primary goal with his three-part series on Tiger is to familiarize readers with J2SE 5's most important additions and show how t
 
Creating Varargs in Java 1.5 Tiger
Creating Varargs in Java 1.5 Tiger In this excerpt from Chapter 5 of the book, Brett and David cover how to create and iterate over variable-length argument lists (better known as varargs), which will have you writing better, cleaner, more flexible code
 
Annotations in Tiger, Part 2: Custom annotations
Write your own annotations in Java 5 Part 1 of this series introduced annotations, the new metadata facility in J2SE 5.0, and focused on Tiger's basic built-in annotations. A more powerful related feature is support for writing your own annotations. In t
 
What's New in Swing?
A new skinnable look and feel (Synth), printing support for |JTable| components, the ability to add components directly to a frame, these are a few of the new features in Swing for J2SE 5.0.
 
Write custom appenders for log4j
The Apache Software Foundation's log4j logging library is one of the better logging systems around. It's both easier to use and more flexible than Java's built-in logging system.
 
Just Forms PDF library
The JustFormsPDF library is a Java class library for filling or editing interactive PDF forms on-the-fly. Empower your applications with the industry-standard PDF forms technology using JustFormsPDF.
 
Solaris 10 OS Certification Beta Exams
If you are an expert in system and network administration, you can get involved in the creation of three new Solaris 10 certification exams. These Beta exams count toward official Solaris Certification and allow you to provide comments and technical feedb
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.