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