1. /*
  2. * @(#)PageFormat.java 1.16 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.awt.print;
  11. import java.awt.geom.AffineTransform;
  12. import java.awt.geom.Point2D;
  13. import java.awt.geom.Rectangle2D;
  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
  19. {
  20. /* Class Constants */
  21. /**
  22. * The origin is at the bottom left of the paper with
  23. * x running bottom to top and y running left to right.
  24. * Note that this is not the Macintosh landscape but
  25. * is the Window's and PostScript landscape.
  26. */
  27. public static final int LANDSCAPE = 0;
  28. /**
  29. * The origin is at the top left of the paper with
  30. * x running to the right and y running down the
  31. * paper.
  32. */
  33. public static final int PORTRAIT = 1;
  34. /**
  35. * The origin is at the top right of the paper with x
  36. * running top to bottom and y running right to left.
  37. * Note that this is the Macintosh landscape.
  38. */
  39. public static final int REVERSE_LANDSCAPE = 2;
  40. /* Instance Variables */
  41. /**
  42. * A description of the physical piece of paper.
  43. */
  44. private Paper mPaper;
  45. /**
  46. * The orientation of the current page. This will be
  47. * one of the constants: PORTRIAT, LANDSCAPE, or
  48. * REVERSE_LANDSCAPE,
  49. */
  50. private int mOrientation = PORTRAIT;
  51. /* Constructors */
  52. /**
  53. * Creates a default, portrait-oriented
  54. * <code>PageFormat</code>.
  55. */
  56. public PageFormat()
  57. {
  58. mPaper = new Paper();
  59. }
  60. /* Instance Methods */
  61. /**
  62. * Makes a copy of this <code>PageFormat</code> with the same
  63. * contents as this <code>PageFormat</code>.
  64. * @return a copy of this <code>PageFormat</code>.
  65. */
  66. public Object clone() {
  67. PageFormat newPage;
  68. try {
  69. newPage = (PageFormat) super.clone();
  70. newPage.mPaper = (Paper)mPaper.clone();
  71. } catch (CloneNotSupportedException e) {
  72. e.printStackTrace();
  73. newPage = null; // should never happen.
  74. }
  75. return newPage;
  76. }
  77. /**
  78. * Returns the width, in 1/72nds of an inch, of the page.
  79. * This method takes into account the orientation of the
  80. * page when determining the width.
  81. * @return the width of the page.
  82. */
  83. public double getWidth() {
  84. double width;
  85. int orientation = getOrientation();
  86. if (orientation == PORTRAIT) {
  87. width = mPaper.getWidth();
  88. } else {
  89. width = mPaper.getHeight();
  90. }
  91. return width;
  92. }
  93. /**
  94. * Returns the height, in 1/72nds of an inch, of the page.
  95. * This method takes into account the orientation of the
  96. * page when determining the height.
  97. * @return the height of the page.
  98. */
  99. public double getHeight() {
  100. double height;
  101. int orientation = getOrientation();
  102. if (orientation == PORTRAIT) {
  103. height = mPaper.getHeight();
  104. } else {
  105. height = mPaper.getWidth();
  106. }
  107. return height;
  108. }
  109. /**
  110. * Returns the x coordinate of the upper left point of the
  111. * imageable area of the <code>Paper</code> object
  112. * associated with this <code>PageFormat</code>.
  113. * This method takes into account the
  114. * orientation of the page.
  115. * @return the x coordinate of the upper left point of the
  116. * imageable area of the <code>Paper</code> object
  117. * associated with this <code>PageFormat</code>.
  118. */
  119. public double getImageableX() {
  120. double x;
  121. switch (getOrientation()) {
  122. case LANDSCAPE:
  123. x = mPaper.getHeight()
  124. - (mPaper.getImageableY() + mPaper.getImageableHeight());
  125. break;
  126. case PORTRAIT:
  127. x = mPaper.getImageableX();
  128. break;
  129. case REVERSE_LANDSCAPE:
  130. x = mPaper.getImageableY();
  131. break;
  132. default:
  133. /* This should never happen since it signifies that the
  134. * PageFormat is in an invalid orientation.
  135. */
  136. throw new InternalError("unrecognized orientation");
  137. }
  138. return x;
  139. }
  140. /**
  141. * Returns the y coordinate of the upper left point of the
  142. * imageable area of the <code>Paper</code> object
  143. * associated with this <code>PageFormat</code>.
  144. * This method takes into account the
  145. * orientation of the page.
  146. * @return the y coordinate of the upper left point of the
  147. * imageable area of the <code>Paper</code> object
  148. * associated with this <code>PageFormat</code>.
  149. */
  150. public double getImageableY() {
  151. double y;
  152. switch (getOrientation()) {
  153. case LANDSCAPE:
  154. y = mPaper.getImageableX();
  155. break;
  156. case PORTRAIT:
  157. y = mPaper.getImageableY();
  158. break;
  159. case REVERSE_LANDSCAPE:
  160. y = mPaper.getWidth()
  161. - (mPaper.getImageableX() + mPaper.getImageableWidth());
  162. break;
  163. default:
  164. /* This should never happen since it signifies that the
  165. * PageFormat is in an invalid orientation.
  166. */
  167. throw new InternalError("unrecognized orientation");
  168. }
  169. return y;
  170. }
  171. /**
  172. * Returns the width, in 1/72nds of an inch, of the imageable
  173. * area of the page. This method takes into account the orientation
  174. * of the page.
  175. * @return the width of the page.
  176. */
  177. public double getImageableWidth() {
  178. double width;
  179. if (getOrientation() == PORTRAIT) {
  180. width = mPaper.getImageableWidth();
  181. } else {
  182. width = mPaper.getImageableHeight();
  183. }
  184. return width;
  185. }
  186. /**
  187. * Return the height, in 1/72nds of an inch, of the imageable
  188. * area of the page. This method takes into account the orientation
  189. * of the page.
  190. * @return the height of the page.
  191. */
  192. public double getImageableHeight() {
  193. double height;
  194. if (getOrientation() == PORTRAIT) {
  195. height = mPaper.getImageableHeight();
  196. } else {
  197. height = mPaper.getImageableWidth();
  198. }
  199. return height;
  200. }
  201. /**
  202. * Returns a copy of the {@link Paper} object associated
  203. * with this <code>PageFormat</code>. Changes made to the
  204. * <code>Paper</code> object returned from this method do not
  205. * affect the <code>Paper</code> object of this
  206. * <code>PageFormat</code>. To update the <code>Paper</code>
  207. * object of this <code>PageFormat</code>, create a new
  208. * <code>Paper</code> object and set it into this
  209. * <code>PageFormat</code> by using the {@link #setPaper(Paper)}
  210. * method.
  211. * @return a copy of the <code>Paper</code> object associated
  212. * with this <code>PageFormat</code>.
  213. */
  214. public Paper getPaper() {
  215. return (Paper)mPaper.clone();
  216. }
  217. /**
  218. * Sets the <code>Paper</code> object for this
  219. * <code>PageFormat</code>.
  220. * @param paper the <code>Paper</code> object to which to set
  221. * the <code>Paper</code> object for this <code>PageFormat</code>.
  222. * @exception <code>NullPointerException</code>
  223. * a null paper instance was passed as a parameter.
  224. */
  225. public void setPaper(Paper paper) {
  226. mPaper = (Paper)paper.clone();
  227. }
  228. /**
  229. * Sets the page orientation. <code>orientation</code> must be
  230. * one of the constants: PORTRAIT, LANDSCAPE,
  231. * or REVERSE_LANDSCAPE.
  232. * @param orientation the new orientation for the page
  233. * @exception <code>IllegalArgumentException</code>
  234. * an unknown orientation was requested
  235. */
  236. public void setOrientation(int orientation) throws IllegalArgumentException
  237. {
  238. if (0 <= orientation && orientation <= REVERSE_LANDSCAPE) {
  239. mOrientation = orientation;
  240. } else {
  241. throw new IllegalArgumentException();
  242. }
  243. }
  244. /**
  245. * Returns the orientation of this <code>PageFormat</code>.
  246. * @return this <code>PageFormat</code> object's orientation.
  247. */
  248. public int getOrientation() {
  249. return mOrientation;
  250. }
  251. /**
  252. * Returns a transformation matrix that translates user
  253. * space rendering to the requested orientation
  254. * of the page. The values are placed into the
  255. * array as
  256. * { m00, m10, m01, m11, m02, m12} in
  257. * the form required by the {@link AffineTransform}
  258. * constructor.
  259. * @return the matrix used to translate user space rendering
  260. * to the orientation of the page.
  261. * @see java.awt.geom.AffineTransform
  262. */
  263. public double[] getMatrix() {
  264. double[] matrix = new double[6];
  265. switch (mOrientation) {
  266. case LANDSCAPE:
  267. matrix[0] = 0; matrix[1] = -1;
  268. matrix[2] = 1; matrix[3] = 0;
  269. matrix[4] = 0; matrix[5] = mPaper.getHeight();
  270. break;
  271. case PORTRAIT:
  272. matrix[0] = 1; matrix[1] = 0;
  273. matrix[2] = 0; matrix[3] = 1;
  274. matrix[4] = 0; matrix[5] = 0;
  275. break;
  276. case REVERSE_LANDSCAPE:
  277. matrix[0] = 0; matrix[1] = 1;
  278. matrix[2] = -1; matrix[3] = 0;
  279. matrix[4] = mPaper.getWidth(); matrix[5] = 0;
  280. break;
  281. default:
  282. throw new IllegalArgumentException();
  283. }
  284. return matrix;
  285. }
  286. }