1. /*
  2. * @(#)Dimension.java 1.31 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt;
  8. import java.awt.geom.Dimension2D;
  9. /**
  10. * The <code>Dimension</code> class encapsulates the width and
  11. * height of a component (in integer precision) in a single object.
  12. * The class is
  13. * associated with certain properties of components. Several methods
  14. * defined by the <code>Component</code> class and the
  15. * <code>LayoutManager</code> interface return a
  16. * <code>Dimension</code> object.
  17. * <p>
  18. * Normally the values of <code>width</code>
  19. * and <code>height</code> are non-negative integers.
  20. * The constructors that allow you to create a dimension do
  21. * not prevent you from setting a negative value for these properties.
  22. * If the value of <code>width</code> or <code>height</code> is
  23. * negative, the behavior of some methods defined by other objects is
  24. * undefined.
  25. *
  26. * @version 1.31, 01/23/03
  27. * @author Sami Shaio
  28. * @author Arthur van Hoff
  29. * @see java.awt.Component
  30. * @see java.awt.LayoutManager
  31. * @since JDK1.0
  32. */
  33. public class Dimension extends Dimension2D implements java.io.Serializable {
  34. /**
  35. * The width dimension; negative values can be used.
  36. *
  37. * @serial
  38. * @see #getSize
  39. * @see #setSize
  40. */
  41. public int width;
  42. /**
  43. * The height dimension; negative values can be used.
  44. *
  45. * @serial
  46. * @see #getSize
  47. * @see #setSize
  48. */
  49. public int height;
  50. /*
  51. * JDK 1.1 serialVersionUID
  52. */
  53. private static final long serialVersionUID = 4723952579491349524L;
  54. /**
  55. * Initialize JNI field and method IDs
  56. */
  57. private static native void initIDs();
  58. static {
  59. /* ensure that the necessary native libraries are loaded */
  60. Toolkit.loadLibraries();
  61. if (!GraphicsEnvironment.isHeadless()) {
  62. initIDs();
  63. }
  64. }
  65. /**
  66. * Creates an instance of <code>Dimension</code> with a width
  67. * of zero and a height of zero.
  68. */
  69. public Dimension() {
  70. this(0, 0);
  71. }
  72. /**
  73. * Creates an instance of <code>Dimension</code> whose width
  74. * and height are the same as for the specified dimension.
  75. *
  76. * @param d the specified dimension for the
  77. * <code>width</code> and
  78. * <code>height</code> values
  79. */
  80. public Dimension(Dimension d) {
  81. this(d.width, d.height);
  82. }
  83. /**
  84. * Constructs a <code>Dimension</code> and initializes
  85. * it to the specified width and specified height.
  86. *
  87. * @param width the specified width
  88. * @param height the specified height
  89. */
  90. public Dimension(int width, int height) {
  91. this.width = width;
  92. this.height = height;
  93. }
  94. /**
  95. * Returns the width of this dimension in double precision.
  96. * @return the width of this dimension in double precision
  97. */
  98. public double getWidth() {
  99. return width;
  100. }
  101. /**
  102. * Returns the height of this dimension in double precision.
  103. * @return the height of this dimension in double precision
  104. */
  105. public double getHeight() {
  106. return height;
  107. }
  108. /**
  109. * Sets the size of this <code>Dimension</code> object to
  110. * the specified width and height in double precision.
  111. * Note that if <code>width</code> or <code>height</code>
  112. * are larger than <code>Integer.MAX_VALUE</code>, they will
  113. * be reset to <code>Integer.MAX_VALUE</code>.
  114. *
  115. * @param width the new width for the <code>Dimension</code> object
  116. * @param height the new height for the <code>Dimension</code> object
  117. */
  118. public void setSize(double width, double height) {
  119. this.width = (int) Math.ceil(width);
  120. this.height = (int) Math.ceil(height);
  121. }
  122. /**
  123. * Gets the size of this <code>Dimension</code> object.
  124. * This method is included for completeness, to parallel the
  125. * <code>getSize</code> method defined by <code>Component</code>.
  126. *
  127. * @return the size of this dimension, a new instance of
  128. * <code>Dimension</code> with the same width and height
  129. * @see java.awt.Dimension#setSize
  130. * @see java.awt.Component#getSize
  131. * @since JDK1.1
  132. */
  133. public Dimension getSize() {
  134. return new Dimension(width, height);
  135. }
  136. /**
  137. * Sets the size of this <code>Dimension</code> object to the specified size.
  138. * This method is included for completeness, to parallel the
  139. * <code>setSize</code> method defined by <code>Component</code>.
  140. * @param d the new size for this <code>Dimension</code> object
  141. * @see java.awt.Dimension#getSize
  142. * @see java.awt.Component#setSize
  143. * @since JDK1.1
  144. */
  145. public void setSize(Dimension d) {
  146. setSize(d.width, d.height);
  147. }
  148. /**
  149. * Sets the size of this <code>Dimension</code> object
  150. * to the specified width and height.
  151. * This method is included for completeness, to parallel the
  152. * <code>setSize</code> method defined by <code>Component</code>.
  153. *
  154. * @param width the new width for this <code>Dimension</code> object
  155. * @param height the new height for this <code>Dimension</code> object
  156. * @see java.awt.Dimension#getSize
  157. * @see java.awt.Component#setSize
  158. * @since JDK1.1
  159. */
  160. public void setSize(int width, int height) {
  161. this.width = width;
  162. this.height = height;
  163. }
  164. /**
  165. * Checks whether two dimension objects have equal values.
  166. */
  167. public boolean equals(Object obj) {
  168. if (obj instanceof Dimension) {
  169. Dimension d = (Dimension)obj;
  170. return (width == d.width) && (height == d.height);
  171. }
  172. return false;
  173. }
  174. /**
  175. * Returns the hash code for this <code>Dimension</code>.
  176. *
  177. * @return a hash code for this <code>Dimension</code>
  178. */
  179. public int hashCode() {
  180. int sum = width + height;
  181. return sum * (sum + 1)/2 + width;
  182. }
  183. /**
  184. * Returns a string representation of the values of this
  185. * <code>Dimension</code> object's <code>height</code> and
  186. * <code>width</code> fields. This method is intended to be used only
  187. * for debugging purposes, and the content and format of the returned
  188. * string may vary between implementations. The returned string may be
  189. * empty but may not be <code>null</code>.
  190. *
  191. * @return a string representation of this <code>Dimension</code>
  192. * object
  193. */
  194. public String toString() {
  195. return getClass().getName() + "[width=" + width + ",height=" + height + "]";
  196. }
  197. }