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