001 /*
002 * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.awt.print;
027
028 import java.awt.geom.Rectangle2D;
029
030 /**
031 * The <code>Paper</code> class describes the physical characteristics of
032 * a piece of paper.
033 * <p>
034 * When creating a <code>Paper</code> object, it is the application's
035 * responsibility to ensure that the paper size and the imageable area
036 * are compatible. For example, if the paper size is changed from
037 * 11 x 17 to 8.5 x 11, the application might need to reduce the
038 * imageable area so that whatever is printed fits on the page.
039 * <p>
040 * @see #setSize(double, double)
041 * @see #setImageableArea(double, double, double, double)
042 */
043 public class Paper implements Cloneable {
044
045 /* Private Class Variables */
046
047 private static final int INCH = 72;
048 private static final double LETTER_WIDTH = 8.5 * INCH;
049 private static final double LETTER_HEIGHT = 11 * INCH;
050
051 /* Instance Variables */
052
053 /**
054 * The height of the physical page in 1/72nds
055 * of an inch. The number is stored as a floating
056 * point value rather than as an integer
057 * to facilitate the conversion from metric
058 * units to 1/72nds of an inch and then back.
059 * (This may or may not be a good enough reason
060 * for a float).
061 */
062 private double mHeight;
063
064 /**
065 * The width of the physical page in 1/72nds
066 * of an inch.
067 */
068 private double mWidth;
069
070 /**
071 * The area of the page on which drawing will
072 * be visable. The area outside of this
073 * rectangle but on the Page generally
074 * reflects the printer's hardware margins.
075 * The origin of the physical page is
076 * at (0, 0) with this rectangle provided
077 * in that coordinate system.
078 */
079 private Rectangle2D mImageableArea;
080
081 /* Constructors */
082
083 /**
084 * Creates a letter sized piece of paper
085 * with one inch margins.
086 */
087 public Paper() {
088 mHeight = LETTER_HEIGHT;
089 mWidth = LETTER_WIDTH;
090 mImageableArea = new Rectangle2D.Double(INCH, INCH, mWidth - 2
091 * INCH, mHeight - 2 * INCH);
092 }
093
094 /* Instance Methods */
095
096 /**
097 * Creates a copy of this <code>Paper</code> with the same contents
098 * as this <code>Paper</code>.
099 * @return a copy of this <code>Paper</code>.
100 */
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</code>.
124 */
125 public double getHeight() {
126 return mHeight;
127 }
128
129 /**
130 * Sets the width and height of this <code>Paper</code>
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</code>
136 * object's width
137 * @param height the value to which to set this <code>Paper</code>
138 * object's height
139 */
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</code>.
150 */
151 public double getWidth() {
152 return mWidth;
153 }
154
155 /**
156 * Sets the imageable area of this <code>Paper</code>. 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</code>
161 * @param y the Y coordinate to which to set the
162 * upper-left corner of the imageable area of this <code>Paper</code>
163 * @param width the value to which to set the width of the
164 * imageable area of this <code>Paper</code>
165 * @param height the value to which to set the height of the
166 * imageable area of this <code>Paper</code>
167 */
168 public void setImageableArea(double x, double y, double width,
169 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</code> object's imageable area.
176 * @return the x coordinate of the imageable area.
177 */
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</code> object's imageable area.
185 * @return the y coordinate of the imageable area.
186 */
187 public double getImageableY() {
188 return mImageableArea.getY();
189 }
190
191 /**
192 * Returns the width of this <code>Paper</code> object's imageable
193 * area.
194 * @return the width of the imageable area.
195 */
196 public double getImageableWidth() {
197 return mImageableArea.getWidth();
198 }
199
200 /**
201 * Returns the height of this <code>Paper</code> object's imageable
202 * area.
203 * @return the height of the imageable area.
204 */
205 public double getImageableHeight() {
206 return mImageableArea.getHeight();
207 }
208 }
|