Source code: jcurses/util/Rectangle.java
1 package jcurses.util;
2
3 /**
4 * This is a class to represent an screen rectangle.
5 * To implement this class was needed, because <code>java.awt.rectangle</code>
6 * works with double's, this is by a text based terminal senseless.
7 */
8 public class Rectangle {
9
10 int _x=0;
11 int _y=0;
12 int _width=0;
13 int _height=0;
14
15 /**
16 * The constructor
17 *
18 * @param x the x coordinate of the top left corner
19 * @param y the y coordinate of the top left corner
20 * @param width the width of the rectangle
21 * @param height the height of the rectangle
22 */
23
24 public Rectangle(int x, int y, int width, int height) {
25 _x=x;
26 _y=y;
27 _width=width;
28 _height=height;
29
30 }
31
32
33
34 /**
35 * The constructor, that defines only the size but no location
36 *
37 * @param width the width of the rectangle
38 * @param height the height of the rectangle
39 */
40 public Rectangle(int width, int height) {
41 _width = width;
42 _height = height;
43 }
44
45
46 /**
47 * @return the x coordinate of the top left corner
48 */
49 public int getX() {
50 return _x;
51 }
52
53 /**
54 * @return the y coordinate of the top left corner
55 */
56 public int getY() {
57 return _y;
58 }
59
60 /**
61 * @return the width of the rectangle
62 */
63 public int getWidth() {
64 return _width;
65 }
66
67 /**
68 * @return the height of the rectangle
69 */
70 public int getHeight() {
71 return _height;
72 }
73
74 /**
75 * Sets the x coordinate of the top left corner
76 *
77 * @param x the x coordinate of the top left corner to set
78 */
79 public void setX(int x) {
80 _x = x;
81 }
82
83 /**
84 * Sets the y coordinate of the top left corner
85 *
86 * @param y the x coordinate of the top left corner to set
87 */
88 public void setY(int y) {
89 _y = y;
90 }
91
92 /**
93 * Sets the width of the rectangle
94 *
95 * @param width the width of the rectangle to set
96 */
97 public void setWidth(int width) {
98 _width = width;
99 }
100
101 /**
102 * Sets the height of the rectangle
103 *
104 * @param height the height of the rectangle to set
105 */
106 public void setHeight(int height) {
107 _height= height;
108 }
109
110 /**
111 * @return <code>true</code> if the rectangle is empty in other case <code>false</code>
112 */
113 public boolean isEmpty() {
114 return (_width <= 0) || (_height <= 0);
115 }
116
117 /**
118 * The method veriifies, whether a rectangle lies within this rectangle
119 *
120 * @param X x coordinate of the rectangle, whose containment is to verify
121 * @param Y y coordinate of the rectangle, whose containment is to verify
122 * @param W width of the rectangle, whose containment is to verify
123 * @param H x height of the rectangle, whose containment is to verify
124 *
125 * @return <code>true</code> if the parameter rectangle is withhin this rectangle in other case <code>false</code>
126 */
127 public boolean contains(int X, int Y, int W, int H) {
128 int width = _width;
129 int height = _height;
130 if (width <= 0 || height <= 0 || W <= 0 || H <= 0) {
131 return false;
132 }
133 int x = _x;
134 int y = _y;
135 return (X >= x &&
136 Y >= y &&
137 X + W <= x + width &&
138 Y + H <= y + height);
139 }
140
141
142 /**
143 * The method veriifies, whether a rectangle lies within this rectangle
144 *
145 * @param rect the rectangle, whose containment is to verify
146 *
147 *
148 * @return <code>true</code> if the parameter rectangle is withhin this rectangle in other case <code>false</code>
149 */
150 public boolean contains(Rectangle rect) {
151 return contains(rect.getX(),rect.getY(),rect.getWidth(),rect.getHeight());
152 }
153
154
155 /**
156 * The method returns an intersection of the rectangle with an other rectangle,
157 * that is, the greatest rectangle, that is contained in both.
158 *
159 * @param r rectangle to build intersection with this rectangle
160 *
161 * @return the intersection rectangle
162 */
163 public Rectangle intersection(Rectangle r) {
164 if (isEmpty()) {
165 return (Rectangle)this.clone();
166 } else if (r.isEmpty()) {
167 return (Rectangle)r.clone();
168 } else {
169 int x1 = Math.max(_x, r.getX());
170 int x2 = Math.min(_x + _width, r.getX() + r.getWidth());
171 int y1 = Math.max(_y, r.getY());
172 int y2 = Math.min(_y + _height, r.getY() + r.getHeight());
173 if (((x2 - x1) < 0) || ((y2 - y1) < 0))
174 // Width or height is negative. No intersection.
175 return new Rectangle(0,0,0,0);
176 else
177 return new Rectangle(x1, y1, x2 - x1, y2 - y1);
178 }
179 }
180
181
182 /**
183 * The method returns an union of the rectangle with an other rectangle,
184 * that is, the smallest rectangle, that contains both.
185 *
186 * @param r rectangle to build union with this rectangle
187 *
188 * @return the union rectangle
189 */
190 public Rectangle union(Rectangle r) {
191 if (isEmpty()) {
192 return (Rectangle)r.clone();
193 } else if (r.isEmpty()) {
194 return (Rectangle)this.clone();
195 } else {
196 int x1 = Math.min(_x, r.getX());
197 int x2 = Math.max(_x + _width, r.getX() + r.getWidth());
198 int y1 = Math.min(_y, r.getY());
199 int y2 = Math.max(_y + _height, r.getY() + r.getHeight());
200 return new Rectangle(x1, y1, x2 - x1, y2 - y1);
201 }
202 }
203
204
205 /**
206 * The method veriifies, whether a point lies within this rectangle
207 *
208 * @param X x coordinate of the point, whose containment is to verify
209 * @param Y y coordinate of the point, whose containment is to verify
210 * @return <code>true</code> if the point is withhin this rectangle in other case <code>false</code>
211 */
212 public boolean inside(int x, int y) {
213 return (x >= _x) && ((x - _x) < _width) && (y >= _y) && ((y-_y) < _height);
214 }
215
216
217 /**
218 * Sets the location of the rectangle
219 *
220 * @param x new x coordinate
221 * @param y new y coordinate
222 */
223 public void setLocation(int x, int y) {
224 setX(x);
225 setY(y);
226 }
227
228
229 /**
230 * Changes the size of the rectangle
231 *
232 * @param width new width
233 * @param height new height
234 */
235 public void resize(int width, int height) {
236 setWidth(width);
237 setHeight(height);
238 }
239
240
241 public Object clone() {
242 return new Rectangle(_x, _y, _width, _height);
243 }
244
245
246 public String toString() {
247 return "[x="+_x+",y="+_y+",width="+_width+",height="+_height+",isEmpty="+isEmpty()+"]";
248 }
249
250 }