1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
/* * @(#)PageFormat.java 1.22 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.awt.print; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; /** * The <code>PageFormat</code> class describes the size and * orientation of a page to be printed. */ public class PageFormat implements Cloneable { /* Class Constants */ /** * The origin is at the bottom left of the paper with * x running bottom to top and y running left to right. * Note that this is not the Macintosh landscape but * is the Window's and PostScript landscape. */ public static final int LANDSCAPE = 0; /** * The origin is at the top left of the paper with * x running to the right and y running down the * paper. */ public static final int PORTRAIT = 1; /** * The origin is at the top right of the paper with x * running top to bottom and y running right to left. * Note that this is the Macintosh landscape. */ public static final int REVERSE_LANDSCAPE = 2; /* Instance Variables */ /** * A description of the physical piece of paper. */ private Paper mPaper; /** * The orientation of the current page. This will be * one of the constants: PORTRIAT, LANDSCAPE, or * REVERSE_LANDSCAPE, */ private int mOrientation = PORTRAIT; /* Constructors */ /** * Creates a default, portrait-oriented * <code>PageFormat</code>. */ public PageFormat() { mPaper = new Paper(); } /* Instance Methods */ /** * Makes a copy of this <code>PageFormat</code> with the same * contents as this <code>PageFormat</code>. * @return a copy of this <code>PageFormat</code>. */ public Object clone() { PageFormat newPage; try { newPage = (PageFormat) super.clone(); newPage.mPaper = (Paper)mPaper.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); newPage = null; // should never happen. } return newPage; } /** * Returns the width, in 1/72nds of an inch, of the page. * This method takes into account the orientation of the * page when determining the width. * @return the width of the page. */ public double getWidth() { double width; int orientation = getOrientation(); if (orientation == PORTRAIT) { width = mPaper.getWidth(); } else { width = mPaper.getHeight(); } return width; } /** * Returns the height, in 1/72nds of an inch, of the page. * This method takes into account the orientation of the * page when determining the height. * @return the height of the page. */ public double getHeight() { double height; int orientation = getOrientation(); if (orientation == PORTRAIT) { height = mPaper.getHeight(); } else { height = mPaper.getWidth(); } return height; } /** * Returns the x coordinate of the upper left point of the * imageable area of the <code>Paper</code> object * associated with this <code>PageFormat</code>. * This method takes into account the * orientation of the page. * @return the x coordinate of the upper left point of the * imageable area of the <code>Paper</code> object * associated with this <code>PageFormat</code>. */ public double getImageableX() { double x; switch (getOrientation()) { case LANDSCAPE: x = mPaper.getHeight() - (mPaper.getImageableY() + mPaper.getImageableHeight()); break; case PORTRAIT: x = mPaper.getImageableX(); break; case REVERSE_LANDSCAPE: x = mPaper.getImageableY(); break; default: /* This should never happen since it signifies that the * PageFormat is in an invalid orientation. */ throw new InternalError("unrecognized orientation"); } return x; } /** * Returns the y coordinate of the upper left point of the * imageable area of the <code>Paper</code> object * associated with this <code>PageFormat</code>. * This method takes into account the * orientation of the page. * @return the y coordinate of the upper left point of the * imageable area of the <code>Paper</code> object * associated with this <code>PageFormat</code>. */ public double getImageableY() { double y; switch (getOrientation()) { case LANDSCAPE: y = mPaper.getImageableX(); break; case PORTRAIT: y = mPaper.getImageableY(); break; case REVERSE_LANDSCAPE: y = mPaper.getWidth() - (mPaper.getImageableX() + mPaper.getImageableWidth()); break; default: /* This should never happen since it signifies that the * PageFormat is in an invalid orientation. */ throw new InternalError("unrecognized orientation"); } return y; } /** * Returns the width, in 1/72nds of an inch, of the imageable * area of the page. This method takes into account the orientation * of the page. * @return the width of the page. */ public double getImageableWidth() { double width; if (getOrientation() == PORTRAIT) { width = mPaper.getImageableWidth(); } else { width = mPaper.getImageableHeight(); } return width; } /** * Return the height, in 1/72nds of an inch, of the imageable * area of the page. This method takes into account the orientation * of the page. * @return the height of the page. */ public double getImageableHeight() { double height; if (getOrientation() == PORTRAIT) { height = mPaper.getImageableHeight(); } else { height = mPaper.getImageableWidth(); } return height; } /** * Returns a copy of the {@link Paper} object associated * with this <code>PageFormat</code>. Changes made to the * <code>Paper</code> object returned from this method do not * affect the <code>Paper</code> object of this * <code>PageFormat</code>. To update the <code>Paper</code> * object of this <code>PageFormat</code>, create a new * <code>Paper</code> object and set it into this * <code>PageFormat</code> by using the {@link #setPaper(Paper)} * method. * @return a copy of the <code>Paper</code> object associated * with this <code>PageFormat</code>. * @see #setPaper */ public Paper getPaper() { return (Paper)mPaper.clone(); } /** * Sets the <code>Paper</code> object for this * <code>PageFormat</code>. * @param paper the <code>Paper</code> object to which to set * the <code>Paper</code> object for this <code>PageFormat</code>. * @exception <code>NullPointerException</code> * a null paper instance was passed as a parameter. * @see #getPaper */ public void setPaper(Paper paper) { mPaper = (Paper)paper.clone(); } /** * Sets the page orientation. <code>orientation</code> must be * one of the constants: PORTRAIT, LANDSCAPE, * or REVERSE_LANDSCAPE. * @param orientation the new orientation for the page * @throws IllegalArgumentException if * an unknown orientation was requested * @see #getOrientation */ public void setOrientation(int orientation) throws IllegalArgumentException { if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) { mOrientation = orientation; } else { throw new IllegalArgumentException(); } } /** * Returns the orientation of this <code>PageFormat</code>. * @return this <code>PageFormat</code> object's orientation. * @see #setOrientation */ public int getOrientation() { return mOrientation; } /** * Returns a transformation matrix that translates user * space rendering to the requested orientation * of the page. The values are placed into the * array as * { m00, m10, m01, m11, m02, m12} in * the form required by the {@link AffineTransform} * constructor. * @return the matrix used to translate user space rendering * to the orientation of the page. * @see java.awt.geom.AffineTransform */ public double[] getMatrix() { double[] matrix = new double[6]; switch (mOrientation) { case LANDSCAPE: matrix[0] = 0; matrix[1] = -1; matrix[2] = 1; matrix[3] = 0; matrix[4] = 0; matrix[5] = mPaper.getHeight(); break; case PORTRAIT: matrix[0] = 1; matrix[1] = 0; matrix[2] = 0; matrix[3] = 1; matrix[4] = 0; matrix[5] = 0; break; case REVERSE_LANDSCAPE: matrix[0] = 0; matrix[1] = 1; matrix[2] = -1; matrix[3] = 0; matrix[4] = mPaper.getWidth(); matrix[5] = 0; break; default: throw new IllegalArgumentException(); } return matrix; } }