1 11 package org.eclipse.swt.graphics; 12 13 14 import org.eclipse.swt.internal.SerializableCompatibility; 15 import org.eclipse.swt.*; 16 17 42 43 public final class Rectangle implements SerializableCompatibility { 44 45 48 public int x; 49 50 53 public int y; 54 55 58 public int width; 59 60 63 public int height; 64 65 static final long serialVersionUID = 3256439218279428914L; 66 67 76 public Rectangle (int x, int y, int width, int height) { 77 this.x = x; 78 this.y = y; 79 this.width = width; 80 this.height = height; 81 } 82 83 99 public void add (Rectangle rect) { 100 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 101 int left = x < rect.x ? x : rect.x; 102 int top = y < rect.y ? y : rect.y; 103 int lhs = x + width; 104 int rhs = rect.x + rect.width; 105 int right = lhs > rhs ? lhs : rhs; 106 lhs = y + height; 107 rhs = rect.y + rect.height; 108 int bottom = lhs > rhs ? lhs : rhs; 109 x = left; y = top; width = right - left; height = bottom - top; 110 } 111 112 121 public boolean contains (int x, int y) { 122 return (x >= this.x) && (y >= this.y) && ((x - this.x) < width) && ((y - this.y) < height); 123 } 124 125 137 public boolean contains (Point pt) { 138 if (pt == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 139 return contains(pt.x, pt.y); 140 } 141 142 152 public boolean equals (Object object) { 153 if (object == this) return true; 154 if (!(object instanceof Rectangle)) return false; 155 Rectangle r = (Rectangle)object; 156 return (r.x == this.x) && (r.y == this.y) && (r.width == this.width) && (r.height == this.height); 157 } 158 159 169 public int hashCode () { 170 return x ^ y ^ width ^ height; 171 } 172 173 186 public void intersect (Rectangle rect) { 187 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 188 if (this == rect) return; 189 int left = x > rect.x ? x : rect.x; 190 int top = y > rect.y ? y : rect.y; 191 int lhs = x + width; 192 int rhs = rect.x + rect.width; 193 int right = lhs < rhs ? lhs : rhs; 194 lhs = y + height; 195 rhs = rect.y + rect.height; 196 int bottom = lhs < rhs ? lhs : rhs; 197 x = right < left ? 0 : left; 198 y = bottom < top ? 0 : top; 199 width = right < left ? 0 : right - left; 200 height = bottom < top ? 0 : bottom - top; 201 } 202 203 218 public Rectangle intersection (Rectangle rect) { 219 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 220 if (this == rect) return new Rectangle (x, y, width, height); 221 int left = x > rect.x ? x : rect.x; 222 int top = y > rect.y ? y : rect.y; 223 int lhs = x + width; 224 int rhs = rect.x + rect.width; 225 int right = lhs < rhs ? lhs : rhs; 226 lhs = y + height; 227 rhs = rect.y + rect.height; 228 int bottom = lhs < rhs ? lhs : rhs; 229 return new Rectangle ( 230 right < left ? 0 : left, 231 bottom < top ? 0 : top, 232 right < left ? 0 : right - left, 233 bottom < top ? 0 : bottom - top); 234 } 235 236 260 public boolean intersects (int x, int y, int width, int height) { 261 return (x < this.x + this.width) && (y < this.y + this.height) && 262 (x + width > this.x) && (y + height > this.y); 263 } 264 265 283 public boolean intersects (Rectangle rect) { 284 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 285 return rect == this || intersects (rect.x, rect.y, rect.width, rect.height); 286 } 287 288 300 public boolean isEmpty () { 301 return (width <= 0) || (height <= 0); 302 } 303 304 310 public String toString () { 311 return "Rectangle {" + x + ", " + y + ", " + width + ", " + height + "}"; } 313 314 332 public Rectangle union (Rectangle rect) { 333 if (rect == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 334 int left = x < rect.x ? x : rect.x; 335 int top = y < rect.y ? y : rect.y; 336 int lhs = x + width; 337 int rhs = rect.x + rect.width; 338 int right = lhs > rhs ? lhs : rhs; 339 lhs = y + height; 340 rhs = rect.y + rect.height; 341 int bottom = lhs > rhs ? lhs : rhs; 342 return new Rectangle (left, top, right - left, bottom - top); 343 } 344 345 } 346 | Popular Tags |