KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > utils > Spatial


1 /*
2  * $Id: Spatial.java,v 1.1 2005/02/24 20:35:25 rbair Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7 package org.jdesktop.swing.utils;
8
9 /**
10  * <p>
11  * Represents a <b>location</b>, and a <b>size</b> (magnitude).<br>
12  * In general, the <code>Spatial</code> class is intended to track the
13  * size and position of windows in space. Hence the term.
14  * </p>
15  * <p>
16  * NOTE: This is an immutable object
17  * </p>
18  * @author Richard Bair
19  */

20 public final class Spatial {
21     /**
22      * measure of the distance from the top of space (ie: top of the screen)
23      * to the top of the object
24      */

25     private int top;
26     /**
27      * measure of the distance from the left of space (ie: left of the screen)
28      * to the left side of the object
29      */

30     private int left;
31     /**
32      * measure of the width of the object
33      */

34     private int width;
35     /**
36      * measure of the height of the object
37      */

38     private int height;
39     private int cachedHash;
40     private String JavaDoc cachedAsString;
41     
42     /**
43      * Create a new Spatial object.
44      * @param top @see top
45      * @param left @see left
46      * @param width @see width
47      * @param height @see height
48      */

49     public Spatial(int top, int left, int width, int height) {
50         this.top = top;
51         this.left = left;
52         this.width = width;
53         this.height = height;
54         
55         cachedHash = (top << 4) + (left << 3) + (width << 2) + (height << 1);
56         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
57         buffer.append("{");
58         buffer.append(top);
59         buffer.append(",");
60         buffer.append(left);
61         buffer.append(",");
62         buffer.append(width);
63         buffer.append(",");
64         buffer.append(height);
65         buffer.append("}");
66         cachedAsString = buffer.toString();
67     }
68     
69     /**
70      * Get the height of the spatial object
71      * @return
72      */

73     public int getHeight() {
74         return height;
75     }
76
77     /**
78      * Get the distance from the left of the space for the spatial object
79      * @return
80      */

81     public int getLeft() {
82         return left;
83     }
84
85     /**
86      * Get the distance from the top of the space for the spatial object
87      * @return
88      */

89     public int getTop() {
90         return top;
91     }
92
93     /**
94      * Get the width of the spatial object
95      * @return
96      */

97     public int getWidth() {
98         return width;
99     }
100
101     /* (non-Javadoc)
102      * @see java.lang.Object#equals(java.lang.Object)
103      */

104     public boolean equals(Object JavaDoc obj) {
105         if (obj instanceof Spatial) {
106             Spatial s = (Spatial)obj;
107             return height == s.height && left == s.left && top == s.top && width == s.width;
108         }
109         return false;
110     }
111
112     /* (non-Javadoc)
113      * @see java.lang.Object#hashCode()
114      */

115     public int hashCode() {
116         return cachedHash;
117     }
118
119     /* (non-Javadoc)
120      * @see java.lang.Object#toString()
121      */

122     public String JavaDoc toString() {
123         return cachedAsString;
124     }
125 }
126
Popular Tags