View Javadoc
1   /*
2    * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package java.awt;
27  
28  /**
29   * An <code>Insets</code> object is a representation of the borders
30   * of a container. It specifies the space that a container must leave
31   * at each of its edges. The space can be a border, a blank space, or
32   * a title.
33   *
34   * @author      Arthur van Hoff
35   * @author      Sami Shaio
36   * @see         java.awt.LayoutManager
37   * @see         java.awt.Container
38   * @since       JDK1.0
39   */
40  public class Insets implements Cloneable, java.io.Serializable {
41  
42      /**
43       * The inset from the top.
44       * This value is added to the Top of the rectangle
45       * to yield a new location for the Top.
46       *
47       * @serial
48       * @see #clone()
49       */
50      public int top;
51  
52      /**
53       * The inset from the left.
54       * This value is added to the Left of the rectangle
55       * to yield a new location for the Left edge.
56       *
57       * @serial
58       * @see #clone()
59       */
60      public int left;
61  
62      /**
63       * The inset from the bottom.
64       * This value is subtracted from the Bottom of the rectangle
65       * to yield a new location for the Bottom.
66       *
67       * @serial
68       * @see #clone()
69       */
70      public int bottom;
71  
72      /**
73       * The inset from the right.
74       * This value is subtracted from the Right of the rectangle
75       * to yield a new location for the Right edge.
76       *
77       * @serial
78       * @see #clone()
79       */
80      public int right;
81  
82      /*
83       * JDK 1.1 serialVersionUID
84       */
85      private static final long serialVersionUID = -2272572637695466749L;
86  
87      static {
88          /* ensure that the necessary native libraries are loaded */
89          Toolkit.loadLibraries();
90          if (!GraphicsEnvironment.isHeadless()) {
91              initIDs();
92          }
93      }
94  
95      /**
96       * Creates and initializes a new <code>Insets</code> object with the
97       * specified top, left, bottom, and right insets.
98       * @param       top   the inset from the top.
99       * @param       left   the inset from the left.
100      * @param       bottom   the inset from the bottom.
101      * @param       right   the inset from the right.
102      */
103     public Insets(int top, int left, int bottom, int right) {
104         this.top = top;
105         this.left = left;
106         this.bottom = bottom;
107         this.right = right;
108     }
109 
110     /**
111      * Set top, left, bottom, and right to the specified values
112      *
113      * @param       top   the inset from the top.
114      * @param       left   the inset from the left.
115      * @param       bottom   the inset from the bottom.
116      * @param       right   the inset from the right.
117      * @since 1.5
118      */
119     public void set(int top, int left, int bottom, int right) {
120         this.top = top;
121         this.left = left;
122         this.bottom = bottom;
123         this.right = right;
124     }
125 
126     /**
127      * Checks whether two insets objects are equal. Two instances
128      * of <code>Insets</code> are equal if the four integer values
129      * of the fields <code>top</code>, <code>left</code>,
130      * <code>bottom</code>, and <code>right</code> are all equal.
131      * @return      <code>true</code> if the two insets are equal;
132      *                          otherwise <code>false</code>.
133      * @since       JDK1.1
134      */
135     public boolean equals(Object obj) {
136         if (obj instanceof Insets) {
137             Insets insets = (Insets)obj;
138             return ((top == insets.top) && (left == insets.left) &&
139                     (bottom == insets.bottom) && (right == insets.right));
140         }
141         return false;
142     }
143 
144     /**
145      * Returns the hash code for this Insets.
146      *
147      * @return    a hash code for this Insets.
148      */
149     public int hashCode() {
150         int sum1 = left + bottom;
151         int sum2 = right + top;
152         int val1 = sum1 * (sum1 + 1)/2 + left;
153         int val2 = sum2 * (sum2 + 1)/2 + top;
154         int sum3 = val1 + val2;
155         return sum3 * (sum3 + 1)/2 + val2;
156     }
157 
158     /**
159      * Returns a string representation of this <code>Insets</code> object.
160      * This method is intended to be used only for debugging purposes, and
161      * the content and format of the returned string may vary between
162      * implementations. The returned string may be empty but may not be
163      * <code>null</code>.
164      *
165      * @return  a string representation of this <code>Insets</code> object.
166      */
167     public String toString() {
168         return getClass().getName() + "[top="  + top + ",left=" + left + ",bottom=" + bottom + ",right=" + right + "]";
169     }
170 
171     /**
172      * Create a copy of this object.
173      * @return     a copy of this <code>Insets</code> object.
174      */
175     public Object clone() {
176         try {
177             return super.clone();
178         } catch (CloneNotSupportedException e) {
179             // this shouldn't happen, since we are Cloneable
180             throw new InternalError(e);
181         }
182     }
183     /**
184      * Initialize JNI field and method IDs
185      */
186     private static native void initIDs();
187 
188 }