1. /*
  2. * @(#)Dimension.java 1.22 01/11/29
  3. *
  4. * Copyright 2002 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.22, 11/29/01
  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. initIDs();
  62. }
  63. /**
  64. * Creates an instance of <code>Dimension</code> with a width
  65. * of zero and a height of zero.
  66. */
  67. public Dimension() {
  68. this(0, 0);
  69. }
  70. /**
  71. * Creates an instance of <code>Dimension</code> whose width
  72. * and height are the same as for the specified dimension.
  73. * @param d the specified dimension for the
  74. * <code>width</code> and
  75. * <code>height</code> values.
  76. */
  77. public Dimension(Dimension d) {
  78. this(d.width, d.height);
  79. }
  80. /**
  81. * Constructs a Dimension and initializes it to the specified width and
  82. * specified height.
  83. * @param width the specified width dimension
  84. * @param height the specified height dimension
  85. */
  86. public Dimension(int width, int height) {
  87. this.width = width;
  88. this.height = height;
  89. }
  90. /**
  91. * Returns the width of this dimension in double precision.
  92. */
  93. public double getWidth() {
  94. return width;
  95. }
  96. /**
  97. * Returns the height of this dimension in double precision.
  98. */
  99. public double getHeight() {
  100. return height;
  101. }
  102. /**
  103. * Set the size of this Dimension object to the specified width
  104. * and height in double precision.
  105. * @param width the new width for the Dimension object
  106. * @param height the new height for the Dimension object
  107. */
  108. public void setSize(double width, double height) {
  109. width = (int) Math.ceil(width);
  110. height = (int) Math.ceil(height);
  111. }
  112. /**
  113. * Gets the size of this <code>Dimension</code> object.
  114. * This method is included for completeness, to parallel the
  115. * <code>getSize</code> method defined by <code>Component</code>.
  116. * @return the size of this dimension, a new instance of
  117. * <code>Dimension</code> with the same width and height.
  118. * @see java.awt.Dimension#setSize
  119. * @see java.awt.Component#getSize
  120. * @since JDK1.1
  121. */
  122. public Dimension getSize() {
  123. return new Dimension(width, height);
  124. }
  125. /**
  126. * Set the size of this <code>Dimension</code> object to the specified size.
  127. * This method is included for completeness, to parallel the
  128. * <code>setSize</code> method defined by <code>Component</code>.
  129. * @param d the new size for this <code>Dimension</code> object.
  130. * @see java.awt.Dimension#getSize
  131. * @see java.awt.Component#setSize
  132. * @since JDK1.1
  133. */
  134. public void setSize(Dimension d) {
  135. setSize(d.width, d.height);
  136. }
  137. /**
  138. * Set the size of this <code>Dimension</code> object
  139. * to the specified width and height.
  140. * This method is included for completeness, to parallel the
  141. * <code>setSize</code> method defined by <code>Component</code>.
  142. * @param width the new width for this <code>Dimension</code> object.
  143. * @param height the new height for this <code>Dimension</code> object.
  144. * @see java.awt.Dimension#getSize
  145. * @see java.awt.Component#setSize
  146. * @since JDK1.1
  147. */
  148. public void setSize(int width, int height) {
  149. this.width = width;
  150. this.height = height;
  151. }
  152. /**
  153. * Checks whether two dimension objects have equal values.
  154. */
  155. public boolean equals(Object obj) {
  156. if (obj instanceof Dimension) {
  157. Dimension d = (Dimension)obj;
  158. return (width == d.width) && (height == d.height);
  159. }
  160. return false;
  161. }
  162. /**
  163. * Returns a string representation of the values of this
  164. * <code>Dimension</code> object's <code>height</code> and
  165. * <code>width</code> fields. This method is intended to be used only
  166. * for debugging purposes, and the content and format of the returned
  167. * string may vary between implementations. The returned string may be
  168. * empty but may not be <code>null</code>.
  169. *
  170. * @return a string representation of this <code>Dimension</code>
  171. * object.
  172. */
  173. public String toString() {
  174. return getClass().getName() + "[width=" + width + ",height=" + height + "]";
  175. }
  176. }