01: /*******************************************************************************
02: * Copyright (c) 2005 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.ui.examples.undo;
11:
12: import org.eclipse.swt.graphics.GC;
13: import org.eclipse.swt.graphics.Point;
14:
15: /**
16: * 2D Box that can move itself, hit-test, and draw.
17: */
18: public class Box {
19:
20: /*
21: * The location of the box.
22: */
23: public int x1, y1, x2, y2;
24:
25: /*
26: * Constructs a box, defined by any two diametrically
27: * opposing corners.
28: */
29: public Box(int x1, int y1, int x2, int y2) {
30: super ();
31: set(x1, y1, x2, y2);
32: }
33:
34: /*
35: * Move the box to a new origin.
36: */
37: public void move(Point origin) {
38: set(origin.x, origin.y, origin.x + getWidth(), origin.y
39: + getHeight());
40: }
41:
42: /*
43: * Draw the box with the specified gc.
44: */
45: public void draw(GC gc) {
46: gc.drawRectangle(x1, y1, x2 - x1, y2 - y1);
47: }
48:
49: /*
50: * Set the position of the box
51: */
52: private void set(int x1, int y1, int x2, int y2) {
53: this .x1 = Math.min(x1, x2);
54: this .y1 = Math.min(y1, y2);
55: this .x2 = Math.max(x1, x2);
56: this .y2 = Math.max(y1, y2);
57: }
58:
59: /*
60: * Return true if this box contains the point specified by
61: * the x and y.
62: */
63: public boolean contains(int x, int y) {
64: return x >= x1 && x <= x2 && y >= y1 && y <= y2;
65: }
66:
67: /*
68: * Answer the width of the box
69: */
70: public int getWidth() {
71: return x2 - x1;
72: }
73:
74: /*
75: * Answer the height of the box
76: */
77: public int getHeight() {
78: return y2 - y1;
79: }
80: }
|