1. /*
  2. * @(#)Insets.java 1.30 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;
  8. /**
  9. * An <code>Insets</code> object is a representation of the borders
  10. * of a container. It specifies the space that a container must leave
  11. * at each of its edges. The space can be a border, a blank space, or
  12. * a title.
  13. *
  14. * @version 1.30, 12/19/03
  15. * @author Arthur van Hoff
  16. * @author Sami Shaio
  17. * @see java.awt.LayoutManager
  18. * @see java.awt.Container
  19. * @since JDK1.0
  20. */
  21. public class Insets implements Cloneable, java.io.Serializable {
  22. /**
  23. * The inset from the top.
  24. * This value is added to the Top of the rectangle
  25. * to yield a new location for the Top.
  26. *
  27. * @serial
  28. * @see #clone()
  29. */
  30. public int top;
  31. /**
  32. * The inset from the left.
  33. * This value is added to the Left of the rectangle
  34. * to yield a new location for the Left edge.
  35. *
  36. * @serial
  37. * @see #clone()
  38. */
  39. public int left;
  40. /**
  41. * The inset from the bottom.
  42. * This value is subtracted from the Bottom of the rectangle
  43. * to yield a new location for the Bottom.
  44. *
  45. * @serial
  46. * @see #clone()
  47. */
  48. public int bottom;
  49. /**
  50. * The inset from the right.
  51. * This value is subtracted from the Right of the rectangle
  52. * to yield a new location for the Right edge.
  53. *
  54. * @serial
  55. * @see #clone()
  56. */
  57. public int right;
  58. /*
  59. * JDK 1.1 serialVersionUID
  60. */
  61. private static final long serialVersionUID = -2272572637695466749L;
  62. static {
  63. /* ensure that the necessary native libraries are loaded */
  64. Toolkit.loadLibraries();
  65. if (!GraphicsEnvironment.isHeadless()) {
  66. initIDs();
  67. }
  68. }
  69. /**
  70. * Creates and initializes a new <code>Insets</code> object with the
  71. * specified top, left, bottom, and right insets.
  72. * @param top the inset from the top.
  73. * @param left the inset from the left.
  74. * @param bottom the inset from the bottom.
  75. * @param right the inset from the right.
  76. */
  77. public Insets(int top, int left, int bottom, int right) {
  78. this.top = top;
  79. this.left = left;
  80. this.bottom = bottom;
  81. this.right = right;
  82. }
  83. /**
  84. * Set top, left, bottom, and right to the specified values
  85. *
  86. * @param top the inset from the top.
  87. * @param left the inset from the left.
  88. * @param bottom the inset from the bottom.
  89. * @param right the inset from the right.
  90. * @since 1.5
  91. */
  92. public void set(int top, int left, int bottom, int right) {
  93. this.top = top;
  94. this.left = left;
  95. this.bottom = bottom;
  96. this.right = right;
  97. }
  98. /**
  99. * Checks whether two insets objects are equal. Two instances
  100. * of <code>Insets</code> are equal if the four integer values
  101. * of the fields <code>top</code>, <code>left</code>,
  102. * <code>bottom</code>, and <code>right</code> are all equal.
  103. * @return <code>true</code> if the two insets are equal;
  104. * otherwise <code>false</code>.
  105. * @since JDK1.1
  106. */
  107. public boolean equals(Object obj) {
  108. if (obj instanceof Insets) {
  109. Insets insets = (Insets)obj;
  110. return ((top == insets.top) && (left == insets.left) &&
  111. (bottom == insets.bottom) && (right == insets.right));
  112. }
  113. return false;
  114. }
  115. /**
  116. * Returns the hash code for this Insets.
  117. *
  118. * @return a hash code for this Insets.
  119. */
  120. public int hashCode() {
  121. int sum1 = left + bottom;
  122. int sum2 = right + top;
  123. int val1 = sum1 * (sum1 + 1)/2 + left;
  124. int val2 = sum2 * (sum2 + 1)/2 + top;
  125. int sum3 = val1 + val2;
  126. return sum3 * (sum3 + 1)/2 + val2;
  127. }
  128. /**
  129. * Returns a string representation of this <code>Insets</code> object.
  130. * This method is intended to be used only for debugging purposes, and
  131. * the content and format of the returned string may vary between
  132. * implementations. The returned string may be empty but may not be
  133. * <code>null</code>.
  134. *
  135. * @return a string representation of this <code>Insets</code> object.
  136. */
  137. public String toString() {
  138. return getClass().getName() + "[top=" + top + ",left=" + left + ",bottom=" + bottom + ",right=" + right + "]";
  139. }
  140. /**
  141. * Create a copy of this object.
  142. * @return a copy of this <code>Insets</code> object.
  143. */
  144. public Object clone() {
  145. try {
  146. return super.clone();
  147. } catch (CloneNotSupportedException e) {
  148. // this shouldn't happen, since we are Cloneable
  149. throw new InternalError();
  150. }
  151. }
  152. /**
  153. * Initialize JNI field and method IDs
  154. */
  155. private static native void initIDs();
  156. }