KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)PageFormat.java 1.21 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.AffineTransform JavaDoc;
11 import java.awt.geom.Point2D JavaDoc;
12 import java.awt.geom.Rectangle2D JavaDoc;
13
14 /**
15  * The <code>PageFormat</code> class describes the size and
16  * orientation of a page to be printed.
17  */

18 public class PageFormat implements Cloneable JavaDoc
19 {
20
21  /* Class Constants */
22
23     /**
24      * The origin is at the bottom left of the paper with
25      * x running bottom to top and y running left to right.
26      * Note that this is not the Macintosh landscape but
27      * is the Window's and PostScript landscape.
28      */

29     public static final int LANDSCAPE = 0;
30
31     /**
32      * The origin is at the top left of the paper with
33      * x running to the right and y running down the
34      * paper.
35      */

36     public static final int PORTRAIT = 1;
37
38     /**
39      * The origin is at the top right of the paper with x
40      * running top to bottom and y running right to left.
41      * Note that this is the Macintosh landscape.
42      */

43     public static final int REVERSE_LANDSCAPE = 2;
44
45  /* Instance Variables */
46
47     /**
48      * A description of the physical piece of paper.
49      */

50     private Paper JavaDoc mPaper;
51
52     /**
53      * The orientation of the current page. This will be
54      * one of the constants: PORTRIAT, LANDSCAPE, or
55      * REVERSE_LANDSCAPE,
56      */

57     private int mOrientation = PORTRAIT;
58
59  /* Constructors */
60
61     /**
62      * Creates a default, portrait-oriented
63      * <code>PageFormat</code>.
64      */

65     public PageFormat()
66     {
67     mPaper = new Paper JavaDoc();
68     }
69
70  /* Instance Methods */
71
72     /**
73      * Makes a copy of this <code>PageFormat</code> with the same
74      * contents as this <code>PageFormat</code>.
75      * @return a copy of this <code>PageFormat</code>.
76      */

77     public Object JavaDoc clone() {
78     PageFormat JavaDoc newPage;
79
80     try {
81         newPage = (PageFormat JavaDoc) super.clone();
82         newPage.mPaper = (Paper JavaDoc)mPaper.clone();
83
84     } catch (CloneNotSupportedException JavaDoc e) {
85         e.printStackTrace();
86         newPage = null; // should never happen.
87
}
88
89     return newPage;
90     }
91
92
93     /**
94      * Returns the width, in 1/72nds of an inch, of the page.
95      * This method takes into account the orientation of the
96      * page when determining the width.
97      * @return the width of the page.
98      */

99     public double getWidth() {
100     double width;
101     int orientation = getOrientation();
102
103         if (orientation == PORTRAIT) {
104             width = mPaper.getWidth();
105         } else {
106             width = mPaper.getHeight();
107         }
108
109         return width;
110     }
111
112     /**
113      * Returns the height, in 1/72nds of an inch, of the page.
114      * This method takes into account the orientation of the
115      * page when determining the height.
116      * @return the height of the page.
117      */

118     public double getHeight() {
119         double height;
120     int orientation = getOrientation();
121
122         if (orientation == PORTRAIT) {
123             height = mPaper.getHeight();
124         } else {
125             height = mPaper.getWidth();
126         }
127
128         return height;
129     }
130
131     /**
132      * Returns the x coordinate of the upper left point of the
133      * imageable area of the <code>Paper</code> object
134      * associated with this <code>PageFormat</code>.
135      * This method takes into account the
136      * orientation of the page.
137      * @return the x coordinate of the upper left point of the
138      * imageable area of the <code>Paper</code> object
139      * associated with this <code>PageFormat</code>.
140      */

141     public double getImageableX() {
142         double x;
143
144     switch (getOrientation()) {
145
146     case LANDSCAPE:
147         x = mPaper.getHeight()
148         - (mPaper.getImageableY() + mPaper.getImageableHeight());
149         break;
150
151     case PORTRAIT:
152         x = mPaper.getImageableX();
153         break;
154
155     case REVERSE_LANDSCAPE:
156         x = mPaper.getImageableY();
157         break;
158
159     default:
160         /* This should never happen since it signifies that the
161          * PageFormat is in an invalid orientation.
162          */

163         throw new InternalError JavaDoc("unrecognized orientation");
164
165     }
166
167         return x;
168     }
169
170     /**
171      * Returns the y coordinate of the upper left point of the
172      * imageable area of the <code>Paper</code> object
173      * associated with this <code>PageFormat</code>.
174      * This method takes into account the
175      * orientation of the page.
176      * @return the y coordinate of the upper left point of the
177      * imageable area of the <code>Paper</code> object
178      * associated with this <code>PageFormat</code>.
179      */

180     public double getImageableY() {
181         double y;
182
183     switch (getOrientation()) {
184
185     case LANDSCAPE:
186         y = mPaper.getImageableX();
187         break;
188
189     case PORTRAIT:
190         y = mPaper.getImageableY();
191         break;
192
193     case REVERSE_LANDSCAPE:
194         y = mPaper.getWidth()
195         - (mPaper.getImageableX() + mPaper.getImageableWidth());
196         break;
197
198     default:
199         /* This should never happen since it signifies that the
200          * PageFormat is in an invalid orientation.
201          */

202         throw new InternalError JavaDoc("unrecognized orientation");
203
204     }
205
206         return y;
207     }
208
209     /**
210      * Returns the width, in 1/72nds of an inch, of the imageable
211      * area of the page. This method takes into account the orientation
212      * of the page.
213      * @return the width of the page.
214      */

215     public double getImageableWidth() {
216         double width;
217
218         if (getOrientation() == PORTRAIT) {
219             width = mPaper.getImageableWidth();
220         } else {
221             width = mPaper.getImageableHeight();
222         }
223
224         return width;
225     }
226
227     /**
228      * Return the height, in 1/72nds of an inch, of the imageable
229      * area of the page. This method takes into account the orientation
230      * of the page.
231      * @return the height of the page.
232      */

233     public double getImageableHeight() {
234         double height;
235
236         if (getOrientation() == PORTRAIT) {
237             height = mPaper.getImageableHeight();
238         } else {
239             height = mPaper.getImageableWidth();
240         }
241
242         return height;
243     }
244
245
246     /**
247      * Returns a copy of the {@link Paper} object associated
248      * with this <code>PageFormat</code>. Changes made to the
249      * <code>Paper</code> object returned from this method do not
250      * affect the <code>Paper</code> object of this
251      * <code>PageFormat</code>. To update the <code>Paper</code>
252      * object of this <code>PageFormat</code>, create a new
253      * <code>Paper</code> object and set it into this
254      * <code>PageFormat</code> by using the {@link #setPaper(Paper)}
255      * method.
256      * @return a copy of the <code>Paper</code> object associated
257      * with this <code>PageFormat</code>.
258      * @see #setPaper
259      */

260     public Paper JavaDoc getPaper() {
261     return (Paper JavaDoc)mPaper.clone();
262     }
263
264     /**
265      * Sets the <code>Paper</code> object for this
266      * <code>PageFormat</code>.
267      * @param paper the <code>Paper</code> object to which to set
268      * the <code>Paper</code> object for this <code>PageFormat</code>.
269      * @exception <code>NullPointerException</code>
270      * a null paper instance was passed as a parameter.
271      * @see #getPaper
272      */

273      public void setPaper(Paper JavaDoc paper) {
274          mPaper = (Paper JavaDoc)paper.clone();
275      }
276
277     /**
278      * Sets the page orientation. <code>orientation</code> must be
279      * one of the constants: PORTRAIT, LANDSCAPE,
280      * or REVERSE_LANDSCAPE.
281      * @param orientation the new orientation for the page
282      * @throws IllegalArgumentException if
283      * an unknown orientation was requested
284      * @see #getOrientation
285      */

286     public void setOrientation(int orientation) throws IllegalArgumentException JavaDoc
287     {
288     if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
289         mOrientation = orientation;
290     } else {
291         throw new IllegalArgumentException JavaDoc();
292     }
293     }
294
295     /**
296      * Returns the orientation of this <code>PageFormat</code>.
297      * @return this <code>PageFormat</code> object's orientation.
298      * @see #setOrientation
299      */

300     public int getOrientation() {
301     return mOrientation;
302     }
303
304     /**
305      * Returns a transformation matrix that translates user
306      * space rendering to the requested orientation
307      * of the page. The values are placed into the
308      * array as
309      * {&nbsp;m00,&nbsp;m10,&nbsp;m01,&nbsp;m11,&nbsp;m02,&nbsp;m12} in
310      * the form required by the {@link AffineTransform}
311      * constructor.
312      * @return the matrix used to translate user space rendering
313      * to the orientation of the page.
314      * @see java.awt.geom.AffineTransform
315      */

316     public double[] getMatrix() {
317         double[] matrix = new double[6];
318
319     switch (mOrientation) {
320
321     case LANDSCAPE:
322             matrix[0] = 0; matrix[1] = -1;
323             matrix[2] = 1; matrix[3] = 0;
324             matrix[4] = 0; matrix[5] = mPaper.getHeight();
325         break;
326
327     case PORTRAIT:
328             matrix[0] = 1; matrix[1] = 0;
329             matrix[2] = 0; matrix[3] = 1;
330             matrix[4] = 0; matrix[5] = 0;
331         break;
332
333     case REVERSE_LANDSCAPE:
334             matrix[0] = 0; matrix[1] = 1;
335             matrix[2] = -1; matrix[3] = 0;
336             matrix[4] = mPaper.getWidth(); matrix[5] = 0;
337         break;
338
339     default:
340         throw new IllegalArgumentException JavaDoc();
341     }
342
343     return matrix;
344     }
345 }
346
Popular Tags