Listing 3

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