Listing 2
1:
2: /**
3: * Class: Example2 <p>
4: *
5: * Print in Java series for the JavaWorld magazine.
6: *
7: * @author Jean-Pierre Dube <jpdube@videotron.ca>
8: * @version 1.0
9: * @since 1.0
10: */
11:
12: import java.awt.*;
13: import java.awt.font.*;
14: import java.awt.geom.*;
15: import java.awt.print.*;
16:
17:
18: public class Example2 {
19:
20: //--- Private instances declarations
21: private final static int POINTS_PER_INCH = 72;
22:
23:
24: /**
25: * Constructor: Example2 <p>
26: *
27: */
28: public Example2 () {
29:
30: //--- Create a new PrinterJob object
31: PrinterJob printJob = PrinterJob.getPrinterJob ();
32:
33: //--- Create a new book to add pages to
34: Book book = new Book ();
35:
36: //--- Add the cover page using the default page format for this print job
37: book.append (new IntroPage (), printJob.defaultPage ());
38:
39: //--- Add the document page using a landscape page format
40: PageFormat documentPageFormat = new PageFormat ();
41: documentPageFormat.setOrientation (PageFormat.LANDSCAPE);
42: book.append (new Document (), documentPageFormat);
43:
44: //--- Tell the printJob to use the Book as the Pageable object
45: printJob.setPageable (book);
46:
47: //--- Show the print dialog box. If the user clicks the
48: //--- print button, we then proceed to print, else we cancel
49: //--- the process.
50: if (printJob.printDialog()) {
51: try {
52: printJob.print();
53: } catch (Exception PrintException) {
54: PrintException.printStackTrace();
55: }
56: }
57: }
58:
59:
60: /**
61: * Class: IntroPage <p>
62: *
63: * This class defines the painter for the cover page by implementing the
64: * Printable interface. <p>
65: *
66: * @author Jean-Pierre Dube <jpdube@videotron.ca>
67: * @version 1.0
68: * @since 1.0
69: * @see Printable
70: */
71: private class IntroPage implements Printable {
72:
73:
74: /**
75: * Method: print <p>
76: *
77: * @param g a value of type Graphics
78: * @param pageFormat a value of type PageFormat
79: * @param page a value of type int
80: * @return a value of type int
81: */
82: public int print (Graphics g, PageFormat pageFormat, int page) {
83:
84: //--- Create the Graphics2D object
85: Graphics2D g2d = (Graphics2D) g;
86:
87: //--- Translate the origin to 0,0 for the top left corner
88: g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
89:
90: //--- Set the default drawing color to black
91: g2d.setPaint (Color.black);
92:
93: //--- Draw a border around the page
94: Rectangle2D.Double border = new Rectangle2D.Double (0,
95: 0,
96: pageFormat.getImageableWidth (),
97: pageFormat.getImageableHeight ());
98: g2d.draw (border);
99:
100: //--- Print the title
101: String titleText = "Printing in Java Part 2";
102: Font titleFont = new Font ("helvetica", Font.BOLD, 36);
103: g2d.setFont (titleFont);
104:
105: //--- Compute the horizontal center of the page
106: FontMetrics fontMetrics = g2d.getFontMetrics ();
107: double titleX = (pageFormat.getImageableWidth () / 2) - (fontMetrics.stringWidth (titleText) / 2);
108: double titleY = 3 * POINTS_PER_INCH;
109: g2d.drawString (titleText, (int) titleX, (int) titleY);
110:
111: return (PAGE_EXISTS);
112: }
113: }
114:
115:
116:
117: /**
118: * Class: Document <p>
119: *
120: * This class is the painter for the document content.<p>
121: *
122: *
123: * @author Jean-Pierre Dube <jpdube@videotron.ca>
124: * @version 1.0
125: * @since 1.0
126: * @see Printable
127: */
128: private class Document implements Printable {
129:
130:
131: /**
132: * Method: print <p>
133: *
134: * @param g a value of type Graphics
135: * @param pageFormat a value of type PageFormat
136: * @param page a value of type int
137: * @return a value of type int
138: */
139: public int print (Graphics g, PageFormat pageFormat, int page) {
140:
141:
142: //--- Create the Graphics2D object
143: Graphics2D g2d = (Graphics2D) g;
144:
145: //--- Translate the origin to 0,0 for the top left corner
146: g2d.translate (pageFormat.getImageableX (), pageFormat.getImageableY ());
147:
148: //--- Set the drawing color to black
149: g2d.setPaint (Color.black);
150:
151: //--- Draw a border around the page using a 12 point border
152: g2d.setStroke (new BasicStroke (12));
153: Rectangle2D.Double border = new Rectangle2D.Double (0,
154: 0,
155: pageFormat.getImageableWidth (),
156: pageFormat.getImageableHeight ());
157:
158: g2d.draw (border);
159:
160: //--- Print the text one inch from the top and left margins
161: g2d.drawString ("This the content page", POINTS_PER_INCH, POINTS_PER_INCH);
162:
163: //--- Validate the page
164: return (PAGE_EXISTS);
165:
166: }
167: }
168:
169: } // Example2