1. /*
  2. * @(#)Insets.java 1.28 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. /**
  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.28, 01/23/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. * Checks whether two insets objects are equal. Two instances
  85. * of <code>Insets</code> are equal if the four integer values
  86. * of the fields <code>top</code>, <code>left</code>,
  87. * <code>bottom</code>, and <code>right</code> are all equal.
  88. * @return <code>true</code> if the two insets are equal;
  89. * otherwise <code>false</code>.
  90. * @since JDK1.1
  91. */
  92. public boolean equals(Object obj) {
  93. if (obj instanceof Insets) {
  94. Insets insets = (Insets)obj;
  95. return ((top == insets.top) && (left == insets.left) &&
  96. (bottom == insets.bottom) && (right == insets.right));
  97. }
  98. return false;
  99. }
  100. /**
  101. * Returns the hash code for this Insets.
  102. *
  103. * @return a hash code for this Insets.
  104. */
  105. public int hashCode() {
  106. int sum1 = left + bottom;
  107. int sum2 = right + top;
  108. int val1 = sum1 * (sum1 + 1)/2 + left;
  109. int val2 = sum2 * (sum2 + 1)/2 + top;
  110. int sum3 = val1 + val2;
  111. return sum3 * (sum3 + 1)/2 + val2;
  112. }
  113. /**
  114. * Returns a string representation of this <code>Insets</code> object.
  115. * This method is intended to be used only for debugging purposes, and
  116. * the content and format of the returned string may vary between
  117. * implementations. The returned string may be empty but may not be
  118. * <code>null</code>.
  119. *
  120. * @return a string representation of this <code>Insets</code> object.
  121. */
  122. public String toString() {
  123. return getClass().getName() + "[top=" + top + ",left=" + left + ",bottom=" + bottom + ",right=" + right + "]";
  124. }
  125. /**
  126. * Create a copy of this object.
  127. * @return a copy of this <code>Insets</code> object.
  128. */
  129. public Object clone() {
  130. try {
  131. return super.clone();
  132. } catch (CloneNotSupportedException e) {
  133. // this shouldn't happen, since we are Cloneable
  134. throw new InternalError();
  135. }
  136. }
  137. /**
  138. * Initialize JNI field and method IDs
  139. */
  140. private static native void initIDs();
  141. }