1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.awt.print; 27 28 import java.awt.geom.Rectangle2D; 29 30 /** 31 * The {@code Paper} class describes the physical characteristics of 32 * a piece of paper. 33 * <p> 34 * When creating a {@code Paper} object, it is the application's 35 * responsibility to ensure that the paper size and the imageable area 36 * are compatible. For example, if the paper size is changed from 37 * 11 x 17 to 8.5 x 11, the application might need to reduce the 38 * imageable area so that whatever is printed fits on the page. 39 * @see #setSize(double, double) 40 * @see #setImageableArea(double, double, double, double) 41 */ 42 public class Paper implements Cloneable { 43 44 /* Private Class Variables */ 45 46 private static final int INCH = 72; 47 private static final double LETTER_WIDTH = 8.5 * INCH; 48 private static final double LETTER_HEIGHT = 11 * INCH; 49 50 /* Instance Variables */ 51 52 /** 53 * The height of the physical page in 1/72nds 54 * of an inch. The number is stored as a floating 55 * point value rather than as an integer 56 * to facilitate the conversion from metric 57 * units to 1/72nds of an inch and then back. 58 * (This may or may not be a good enough reason 59 * for a float). 60 */ 61 private double mHeight; 62 63 /** 64 * The width of the physical page in 1/72nds 65 * of an inch. 66 */ 67 private double mWidth; 68 69 /** 70 * The area of the page on which drawing will 71 * be visible. The area outside of this 72 * rectangle but on the Page generally 73 * reflects the printer's hardware margins. 74 * The origin of the physical page is 75 * at (0, 0) with this rectangle provided 76 * in that coordinate system. 77 */ 78 private Rectangle2D mImageableArea; 79 80 /* Constructors */ 81 82 /** 83 * Creates a letter sized piece of paper 84 * with one inch margins. 85 */ Paper()86 public Paper() { 87 mHeight = LETTER_HEIGHT; 88 mWidth = LETTER_WIDTH; 89 mImageableArea = new Rectangle2D.Double(INCH, INCH, 90 mWidth - 2 * INCH, 91 mHeight - 2 * INCH); 92 } 93 94 /* Instance Methods */ 95 96 /** 97 * Creates a copy of this {@code Paper} with the same contents 98 * as this {@code Paper}. 99 * @return a copy of this {@code Paper}. 100 */ clone()101 public Object clone() { 102 103 Paper newPaper; 104 105 try { 106 /* It's okay to copy the reference to the imageable 107 * area into the clone since we always return a copy 108 * of the imageable area when asked for it. 109 */ 110 newPaper = (Paper) super.clone(); 111 112 } catch (CloneNotSupportedException e) { 113 e.printStackTrace(); 114 newPaper = null; // should never happen. 115 } 116 117 return newPaper; 118 } 119 120 /** 121 * Returns the height of the page in 1/72nds of an inch. 122 * @return the height of the page described by this 123 * {@code Paper}. 124 */ getHeight()125 public double getHeight() { 126 return mHeight; 127 } 128 129 /** 130 * Sets the width and height of this {@code Paper} 131 * object, which represents the properties of the page onto 132 * which printing occurs. 133 * The dimensions are supplied in 1/72nds of 134 * an inch. 135 * @param width the value to which to set this {@code Paper} 136 * object's width 137 * @param height the value to which to set this {@code Paper} 138 * object's height 139 */ setSize(double width, double height)140 public void setSize(double width, double height) { 141 mWidth = width; 142 mHeight = height; 143 } 144 145 /** 146 * Returns the width of the page in 1/72nds 147 * of an inch. 148 * @return the width of the page described by this 149 * {@code Paper}. 150 */ getWidth()151 public double getWidth() { 152 return mWidth; 153 } 154 155 /** 156 * Sets the imageable area of this {@code Paper}. The 157 * imageable area is the area on the page in which printing 158 * occurs. 159 * @param x the X coordinate to which to set the 160 * upper-left corner of the imageable area of this {@code Paper} 161 * @param y the Y coordinate to which to set the 162 * upper-left corner of the imageable area of this {@code Paper} 163 * @param width the value to which to set the width of the 164 * imageable area of this {@code Paper} 165 * @param height the value to which to set the height of the 166 * imageable area of this {@code Paper} 167 */ setImageableArea(double x, double y, double width, double height)168 public void setImageableArea(double x, double y, 169 double width, double height) { 170 mImageableArea = new Rectangle2D.Double(x, y, width,height); 171 } 172 173 /** 174 * Returns the x coordinate of the upper-left corner of this 175 * {@code Paper} object's imageable area. 176 * @return the x coordinate of the imageable area. 177 */ getImageableX()178 public double getImageableX() { 179 return mImageableArea.getX(); 180 } 181 182 /** 183 * Returns the y coordinate of the upper-left corner of this 184 * {@code Paper} object's imageable area. 185 * @return the y coordinate of the imageable area. 186 */ getImageableY()187 public double getImageableY() { 188 return mImageableArea.getY(); 189 } 190 191 /** 192 * Returns the width of this {@code Paper} object's imageable 193 * area. 194 * @return the width of the imageable area. 195 */ getImageableWidth()196 public double getImageableWidth() { 197 return mImageableArea.getWidth(); 198 } 199 200 /** 201 * Returns the height of this {@code Paper} object's imageable 202 * area. 203 * @return the height of the imageable area. 204 */ getImageableHeight()205 public double getImageableHeight() { 206 return mImageableArea.getHeight(); 207 } 208 } 209