KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > print > Paper


1 /*
2  * @(#)Paper.java 1.17 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.print;
9
10 import java.awt.geom.Rectangle2D JavaDoc;
11
12 /**
13  * The <code>Paper</code> class describes the physical characteristics of
14  * a piece of paper.
15  * <p>
16  * When creating a <code>Paper</code> object, it is the application's
17  * responsibility to ensure that the paper size and the imageable area
18  * are compatible. For example, if the paper size is changed from
19  * 11 x 17 to 8.5 x 11, the application might need to reduce the
20  * imageable area so that whatever is printed fits on the page.
21  * <p>
22  * @see #setSize(double, double)
23  * @see #setImageableArea(double, double, double, double)
24  */

25 public class Paper implements Cloneable JavaDoc {
26
27  /* Private Class Variables */
28
29     private static final int INCH = 72;
30     private static final double LETTER_WIDTH = 8.5 * INCH;
31     private static final double LETTER_HEIGHT = 11 * INCH;
32
33  /* Instance Variables */
34
35     /**
36      * The height of the physical page in 1/72nds
37      * of an inch. The number is stored as a floating
38      * point value rather than as an integer
39      * to facilitate the conversion from metric
40      * units to 1/72nds of an inch and then back.
41      * (This may or may not be a good enough reason
42      * for a float).
43      */

44     private double mHeight;
45
46     /**
47      * The width of the physical page in 1/72nds
48      * of an inch.
49      */

50     private double mWidth;
51
52     /**
53      * The area of the page on which drawing will
54      * be visable. The area outside of this
55      * rectangle but on the Page generally
56      * reflects the printer's hardware margins.
57      * The origin of the physical page is
58      * at (0, 0) with this rectangle provided
59      * in that coordinate system.
60      */

61     private Rectangle2D JavaDoc mImageableArea;
62
63  /* Constructors */
64
65     /**
66      * Creates a letter sized piece of paper
67      * with one inch margins.
68      */

69     public Paper() {
70     mHeight = LETTER_HEIGHT;
71     mWidth = LETTER_WIDTH;
72     mImageableArea = new Rectangle2D.Double JavaDoc(INCH, INCH,
73                         mWidth - 2 * INCH,
74                         mHeight - 2 * INCH);
75     }
76
77  /* Instance Methods */
78
79     /**
80      * Creates a copy of this <code>Paper</code> with the same contents
81      * as this <code>Paper</code>.
82      * @return a copy of this <code>Paper</code>.
83      */

84     public Object JavaDoc clone() {
85
86     Paper JavaDoc newPaper;
87
88     try {
89         /* It's okay to copy the reference to the imageable
90          * area into the clone since we always return a copy
91          * of the imageable area when asked for it.
92          */

93         newPaper = (Paper JavaDoc) super.clone();
94
95     } catch (CloneNotSupportedException JavaDoc e) {
96         e.printStackTrace();
97         newPaper = null; // should never happen.
98
}
99
100     return newPaper;
101     }
102
103     /**
104      * Returns the height of the page in 1/72nds of an inch.
105      * @return the height of the page described by this
106      * <code>Paper</code>.
107      */

108     public double getHeight() {
109     return mHeight;
110     }
111
112     /**
113      * Sets the width and height of this <code>Paper</code>
114      * object, which represents the properties of the page onto
115      * which printing occurs.
116      * The dimensions are supplied in 1/72nds of
117      * an inch.
118      * @param width the value to which to set this <code>Paper</code>
119      * object's width
120      * @param height the value to which to set this <code>Paper</code>
121      * object's height
122      */

123     public void setSize(double width, double height) {
124     mWidth = width;
125     mHeight = height;
126     }
127
128     /**
129      * Returns the width of the page in 1/72nds
130      * of an inch.
131      * @return the width of the page described by this
132      * <code>Paper</code>.
133      */

134     public double getWidth() {
135     return mWidth;
136     }
137
138     /**
139      * Sets the imageable area of this <code>Paper</code>. The
140      * imageable area is the area on the page in which printing
141      * occurs.
142      * @param x,&nbsp;y the coordinates to which to set the
143      * upper-left corner of the imageable area of this <code>Paper</code>
144      * @param width the value to which to set the width of the
145      * imageable area of this <code>Paper</code>
146      * @param height the value to which to set the height of the
147      * imageable area of this <code>Paper</code>
148      */

149     public void setImageableArea(double x, double y,
150                  double width, double height) {
151     mImageableArea = new Rectangle2D.Double JavaDoc(x, y, width,height);
152     }
153
154     /**
155      * Returns the x coordinate of the upper-left corner of this
156      * <code>Paper</code> object's imageable area.
157      * @return the x coordinate of the imageable area.
158      */

159     public double getImageableX() {
160     return mImageableArea.getX();
161     }
162
163     /**
164      * Returns the y coordinate of the upper-left corner of this
165      * <code>Paper</code> object's imageable area.
166      * @return the y coordinate of the imageable area.
167      */

168     public double getImageableY() {
169     return mImageableArea.getY();
170     }
171
172     /**
173      * Returns the width of this <code>Paper</code> object's imageable
174      * area.
175      * @return the width of the imageable area.
176      */

177     public double getImageableWidth() {
178     return mImageableArea.getWidth();
179     }
180
181     /**
182      * Returns the height of this <code>Paper</code> object's imageable
183      * area.
184      * @return the height of the imageable area.
185      */

186     public double getImageableHeight() {
187     return mImageableArea.getHeight();
188     }
189 }
190
Popular Tags