Spatial.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:3k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: Spatial.java,v 1.3 2005/10/10 18:02:58 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.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.util;
  22. /**
  23.  * <p>
  24.  * Represents a <b>location</b>, and a <b>size</b> (magnitude).<br>
  25.  * In general, the <code>Spatial</code> class is intended to track the
  26.  * size and position of windows in space.  Hence the term.
  27.  * </p>
  28.  * <p>
  29.  * NOTE: This is an immutable object
  30.  * </p>
  31.  * @author Richard Bair
  32.  */
  33. public final class Spatial {
  34. /**
  35.  * measure of the distance from the top of space (ie: top of the screen)
  36.  * to the top of the object
  37.  */
  38. private int top;
  39. /**
  40.  * measure of the distance from the left of space (ie: left of the screen)
  41.  * to the left side of the object
  42.  */
  43. private int left;
  44. /**
  45.  * measure of the width of the object
  46.  */
  47. private int width;
  48. /**
  49.  * measure of the height of the object
  50.  */
  51. private int height;
  52. private int cachedHash;
  53. private String cachedAsString;
  54. /**
  55.  * Create a new Spatial object.
  56.  * @param top @see top
  57.  * @param left @see left
  58.  * @param width @see width
  59.  * @param height @see height
  60.  */
  61. public Spatial(int top, int left, int width, int height) {
  62. this.top = top;
  63. this.left = left;
  64. this.width = width;
  65. this.height = height;
  66. cachedHash = (top << 4) + (left << 3) + (width << 2) + (height << 1);
  67. StringBuffer buffer = new StringBuffer();
  68. buffer.append("{");
  69. buffer.append(top);
  70. buffer.append(",");
  71. buffer.append(left);
  72. buffer.append(",");
  73. buffer.append(width);
  74. buffer.append(",");
  75. buffer.append(height);
  76. buffer.append("}");
  77. cachedAsString = buffer.toString();
  78. }
  79. /**
  80.  * Get the height of the spatial object
  81.  * @return the height
  82.  */
  83. public int getHeight() {
  84. return height;
  85. }
  86. /**
  87.  * Get the distance from the left of the space for the spatial object
  88.  * @return the left distance
  89.  */
  90. public int getLeft() {
  91. return left;
  92. }
  93. /**
  94.  * Get the distance from the top of the space for the spatial object
  95.  * @return the top distance
  96.  */
  97. public int getTop() {
  98. return top;
  99. }
  100. /**
  101.  * Get the width of the spatial object
  102.  * @return the width
  103.  */
  104. public int getWidth() {
  105. return width;
  106. }
  107. /**
  108.  * {@inheritDoc}
  109.  */
  110. public boolean equals(Object obj) {
  111. if (obj instanceof Spatial) {
  112. Spatial s = (Spatial)obj;
  113. return height == s.height && left == s.left && top == s.top && width == s.width;
  114. }
  115. return false;
  116. }
  117. /**
  118.  * {@inheritDoc}
  119.  */
  120. public int hashCode() {
  121. return cachedHash;
  122. }
  123. /**
  124.  * {@inheritDoc}
  125.  */
  126. public String toString() {
  127. return cachedAsString;
  128. }
  129. }