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