View Javadoc
1   /*
2    * Copyright (c) 1997, 2006, 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</code> class describes the physical characteristics of
32   * a piece of paper.
33   * <p>
34   * When creating a <code>Paper</code> 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   * <p>
40   * @see #setSize(double, double)
41   * @see #setImageableArea(double, double, double, double)
42   */
43  public class Paper implements Cloneable {
44  
45   /* Private Class Variables */
46  
47      private static final int INCH = 72;
48      private static final double LETTER_WIDTH = 8.5 * INCH;
49      private static final double LETTER_HEIGHT = 11 * INCH;
50  
51   /* Instance Variables */
52  
53      /**
54       * The height of the physical page in 1/72nds
55       * of an inch. The number is stored as a floating
56       * point value rather than as an integer
57       * to facilitate the conversion from metric
58       * units to 1/72nds of an inch and then back.
59       * (This may or may not be a good enough reason
60       * for a float).
61       */
62      private double mHeight;
63  
64      /**
65       * The width of the physical page in 1/72nds
66       * of an inch.
67       */
68      private double mWidth;
69  
70      /**
71       * The area of the page on which drawing will
72       * be visable. The area outside of this
73       * rectangle but on the Page generally
74       * reflects the printer's hardware margins.
75       * The origin of the physical page is
76       * at (0, 0) with this rectangle provided
77       * in that coordinate system.
78       */
79      private Rectangle2D mImageableArea;
80  
81   /* Constructors */
82  
83      /**
84       * Creates a letter sized piece of paper
85       * with one inch margins.
86       */
87      public Paper() {
88          mHeight = LETTER_HEIGHT;
89          mWidth = LETTER_WIDTH;
90          mImageableArea = new Rectangle2D.Double(INCH, INCH,
91                                                  mWidth - 2 * INCH,
92                                                  mHeight - 2 * INCH);
93      }
94  
95   /* Instance Methods */
96  
97      /**
98       * Creates a copy of this <code>Paper</code> with the same contents
99       * as this <code>Paper</code>.
100      * @return a copy of this <code>Paper</code>.
101      */
102     public Object clone() {
103 
104         Paper newPaper;
105 
106         try {
107             /* It's okay to copy the reference to the imageable
108              * area into the clone since we always return a copy
109              * of the imageable area when asked for it.
110              */
111             newPaper = (Paper) super.clone();
112 
113         } catch (CloneNotSupportedException e) {
114             e.printStackTrace();
115             newPaper = null;    // should never happen.
116         }
117 
118         return newPaper;
119     }
120 
121     /**
122      * Returns the height of the page in 1/72nds of an inch.
123      * @return the height of the page described by this
124      *          <code>Paper</code>.
125      */
126     public double getHeight() {
127         return mHeight;
128     }
129 
130     /**
131      * Sets the width and height of this <code>Paper</code>
132      * object, which represents the properties of the page onto
133      * which printing occurs.
134      * The dimensions are supplied in 1/72nds of
135      * an inch.
136      * @param width the value to which to set this <code>Paper</code>
137      * object's width
138      * @param height the value to which to set this <code>Paper</code>
139      * object's height
140      */
141     public void setSize(double width, double height) {
142         mWidth = width;
143         mHeight = height;
144     }
145 
146     /**
147      * Returns the width of the page in 1/72nds
148      * of an inch.
149      * @return the width of the page described by this
150      * <code>Paper</code>.
151      */
152     public double getWidth() {
153         return mWidth;
154     }
155 
156     /**
157      * Sets the imageable area of this <code>Paper</code>.  The
158      * imageable area is the area on the page in which printing
159      * occurs.
160      * @param x the X coordinate to which to set the
161      * upper-left corner of the imageable area of this <code>Paper</code>
162      * @param y the Y coordinate to which to set the
163      * upper-left corner of the imageable area of this <code>Paper</code>
164      * @param width the value to which to set the width of the
165      * imageable area of this <code>Paper</code>
166      * @param height the value to which to set the height of the
167      * imageable area of this <code>Paper</code>
168      */
169     public void setImageableArea(double x, double y,
170                                  double width, double height) {
171         mImageableArea = new Rectangle2D.Double(x, y, width,height);
172     }
173 
174     /**
175      * Returns the x coordinate of the upper-left corner of this
176      * <code>Paper</code> object's imageable area.
177      * @return the x coordinate of the imageable area.
178      */
179     public double getImageableX() {
180         return mImageableArea.getX();
181     }
182 
183     /**
184      * Returns the y coordinate of the upper-left corner of this
185      * <code>Paper</code> object's imageable area.
186      * @return the y coordinate of the imageable area.
187      */
188     public double getImageableY() {
189         return mImageableArea.getY();
190     }
191 
192     /**
193      * Returns the width of this <code>Paper</code> object's imageable
194      * area.
195      * @return the width of the imageable area.
196      */
197     public double getImageableWidth() {
198         return mImageableArea.getWidth();
199     }
200 
201     /**
202      * Returns the height of this <code>Paper</code> object's imageable
203      * area.
204      * @return the height of the imageable area.
205      */
206     public double getImageableHeight() {
207         return mImageableArea.getHeight();
208     }
209 }