File: Rectangle.java

package info (click to toggle)
orp-classpath 1%3A0.02.1-3
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 15,212 kB
  • ctags: 16,077
  • sloc: java: 82,255; ansic: 12,779; sh: 6,321; makefile: 1,478; perl: 962; exp: 122; lisp: 115
file content (719 lines) | stat: -rw-r--r-- 17,194 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/* Rectangle.java -- Class representing a rectangle.
   Copyright (C) 1999 Free Software Foundation, Inc.

This file is part of the non-peer AWT libraries of GNU Classpath.

This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Library General Public License as published 
by the Free Software Foundation, either version 2 of the License, or
(at your option) any later verion.

This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Library General Public License for more details.

You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation
Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA. */


package java.awt;

/**
  * This class represents a rectangle and all the interesting things you
  * might want to do with it.  Note that the coordinate system uses
  * the origin (0,0) as the top left of the screen, with the x and y
  * values increasing as they move to the right and down respectively.
  *
  * @author Aaron M. Renn (arenn@urbanophile.com)
  */
public class Rectangle implements Shape, java.io.Serializable
{

/*
 * Instance Variables
 */

/**
  * The X coordinate of the top-left corner of the rectangle.
  */
public int x;

/**
  * The Y coordinate of the top-left corner of the rectangle;
  */
public int y;

/**
  * The width of the rectangle
  */
public int width;

/**
  * The height of the rectangle
  */
public int height;

/*************************************************************************/

/*
 * Constructors
 */

/**
  * Initializes a new instance of <code>Rectangle</code> with a top
  * left corner at (0,0) and a width and heigth of 0.
  */
public
Rectangle()
{
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>Rectangle</code> from the
  * coordinates of the specified rectangle.
  *
  * @param rect The rectangle to copy from.
  */
public
Rectangle(Rectangle rect)
{
  this.x = rect.x;
  this.y = rect.y;
  this.width = rect.width;
  this.height = rect.height;
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>Rectangle</code> from the specified
  * inputs.
  *
  * @param x The X coordinate of the top left corner of the rectangle.
  * @param y The Y coordinate of the top left corner of the rectangle.
  * @param width The width of the rectangle.
  * @param height The height of the rectangle.
  */
public
Rectangle(int x, int y, int width, int height)
{
  this.x = x;
  this.y = y;
  this.width = width;
  this.height = height;
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>Rectangle</code> with the specified
  * width and height.  The upper left corner of the rectangle will be at
  * the origin (0,0).
  *
  * @param width The width of the rectangle.
  * @param height the height of the rectange.
  */
public
Rectangle(int width, int height)
{
  this(0, 0, width, height);
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>Rectangle</code> with a top-left
  * corner represented by the specified point and the width and height
  * represented by the specified dimension.
  *
  * @param point The upper left corner of the rectangle.
  * @param dim The width and height of the rectangle.
  */
public 
Rectangle(Point point, Dimension dim)
{
  this(point.x, point.y, dim.width, dim.height);
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>Rectangle</code> with a top left
  * corner at the specified point and a width and height of zero.
  *
  * @param poin The upper left corner of the rectangle.
  */
public
Rectangle(Point point)
{
  this(point.x, point.y, 0, 0);
}

/*************************************************************************/

/**
  * Initializes a new instance of <code>Rectangle</code> with an
  * upper left corner at the origin (0,0) and a width and height represented
  * by the specified dimension.
  *
  * @param dim The width and height of the rectangle.
  */
public
Rectangle(Dimension dim)
{
  this(0, 0, dim.width, dim.height);
}

/*************************************************************************/

/*
 * Instance Methods
 */

/**
  * Returns the bounding rectangle for this rectangle, which is simply
  * this rectange itself.
  *
  * @return This rectangle.
  */
public Rectangle
getBounds()
{
  return(this);
}

/*************************************************************************/

/**
  * Updates this rectangle to match the dimensions of the specified 
  * rectangle.
  *
  * @param rect The rectangle to update from.
  */
public void
setBounds(Rectangle rect)
{
  synchronized(this)
    {
      this.x = rect.x;
      this.y = rect.y;
      this.width = rect.width;
      this.height = rect.height; 
    }
}

/*************************************************************************/

/**
  * Updates this rectangle to have the specified dimensions.
  *
  * @param x The new X coordinate of the upper left hand corner.
  * @param y The new Y coordinate of the upper left hand corner.
  * @param width The new width of this rectangle.
  * @param height The new height of this rectangle.
  */
public void
setBounds(int x, int y, int width, int height)
{
  synchronized(this)
    {
      this.x = x;
      this.y = y;
      this.width = width;
      this.height = height;
    }
}

/*************************************************************************/

/**
  * Updates this rectangle to have the specified dimensions.
  *
  * @param x The new X coordinate of the upper left hand corner.
  * @param y The new Y coordinate of the upper left hand corner.
  * @param width The new width of this rectangle.
  * @param height The new height of this rectangle.
  *
  * @deprecated This method is deprecated in favor of 
  * <code>setBounds(int, int, int, int)</code>.
  */
public void
reshape(int x, int y, int width, int height)
{
  setBounds(x, y, width, height);
}

/*************************************************************************/

/**
  * Returns the location of this rectangle, which is the coordinates of
  * its upper left corner.
  * // FIXME: Is this true?
  *
  * @return The point where this rectangle is located.
  */
public Point
getLocation()
{
  return(new Point(x, y));
}

/*************************************************************************/

/**
  * Moves the location of this rectangle by setting its upper left
  * corner to the specified point.
  * // FIXME: Is this true?
  *
  * @param point The point to move the rectange to.
  */
public void
setLocation(Point point)
{
  synchronized(this)
    {
      this.x = point.x;
      this.y = point.y;
    }
}

/*************************************************************************/

/**
  * Moves the location of this rectangle by setting its upper left
  * corner to the specified coordinates.
  * // FIXME: Is this true?
  *
  * @param x The new X coordinate for this rectangle.
  * @param y The new Y coordinate for this rectangle.
  */
public void
setLocation(int x, int y)
{
  synchronized(this)
    {
      this.x = x;
      this.y = y;
    }
}

/*************************************************************************/

/**
  * Moves the location of this rectangle by setting its upper left
  * corner to the specified coordinates.
  * // FIXME: Is this true?
  *
  * @param x The new X coordinate for this rectangle.
  * @param y The new Y coordinate for this rectangle.
  *
  * @deprecated This method is deprecated in favor of
  * <code>setLocation(int, int)</code>.
  */
public void
move(int x, int y)
{
  setLocation(x, y);
}

/*************************************************************************/

/**
  * Returns the size of this rectangle.
  *
  * @return The size of this rectangle.
  */
public Dimension
getSize()
{
  return(new Dimension(width, height));
}

/*************************************************************************/

/**
  * Sets the size of this rectangle based on the specified dimensions.
  *
  * @param dim The new dimensions of the rectangle.
  */
public void
setSize(Dimension dim)
{
  synchronized(this)
    {
      this.width = dim.width;
      this.height = dim.height;
    }
}

/*************************************************************************/

/**
  * Sets the size of this rectangle based on the specified dimensions.
  *
  * @param width The new width of the rectangle.
  * @param height The new height of the rectangle.
  */
public void
setSize(int width, int height)
{
  synchronized(this)
    {
      this.width = width;
      this.height = height;
    }
}

public void
translate (int dx, int dy)
{
  synchronized (this)
    {
      this.x += dx;
      this.y += dy;
    }
}

/*************************************************************************/

/**
  * Sets the size of this rectangle based on the specified dimensions.
  *
  * @param width The new width of the rectangle.
  * @param height The new height of the rectangle.
  *
  * @deprecated This method is deprecated in favor of
  * <code>setSize(int, int)</code>.
  */
public void
resize(int width, int height)
{
  setSize(width, height);
}

/*************************************************************************/

/**
  * Tests whether or not the specified point is inside this rectangle.
  *
  * @param point The point to test.
  *
  * @return <code>true</code> if the point is inside the rectangle,
  * <code>false</code> otherwise.
  */
public boolean
contains(Point point)
{
  return(contains(point.x, point.y));
}

/*************************************************************************/

/**
  * Tests whether or not the specified point is inside this rectangle.
  *
  * @param x The X coordinate of the point to test.
  * @param y The Y coordinate of the point to test.
  *
  * @return <code>true</code> if the point is inside the rectangle,
  * <code>false</code> otherwise.
  */
public boolean
contains(int x, int y)
{
  // FIXME: Do we count points on the rectangle?
  if ((x < this.x) || (x > (this.x + width)))
    return(false);
  if ((y < this.y) || (y > (this.y + height)))
    return(false);

  return(true);
}

/*************************************************************************/

/**
  * Tests whether or not the specified point is inside this rectangle.
  *
  * @param x The X coordinate of the point to test.
  * @param y The Y coordinate of the point to test.
  *
  * @return <code>true</code> if the point is inside the rectangle,
  * <code>false</code> otherwise.
  *
  * @deprecated This method is deprecated in favor of
  * <code>contains(int, int)</code>.
  */
public boolean
inside(int x, int y)
{
  return(contains(x, y));
}

/*************************************************************************/

/**
  * Tests whether or not the specified rectangle intersects this rectangle.
  *
  * @param rect The rectangle to test against.
  *
  * @return <code>true</code> if the specified rectangle intersects this
  * one, <code>false</code> otherwise.
  */
public boolean
intersects(Rectangle rect)
{
  if (contains(rect.x, rect.y))
    return(true);
  if (contains(rect.x + width, rect.y))
    return(true);
  if (contains(rect.x, rect.y + height))
    return(true);
  if (contains(rect.x + width, rect.y + height))
    return(true);

  return(false);
}

/*************************************************************************/

/**
  * Determines the rectange which is formed by the intersection of this
  * rectangle with the specified rectangle.
  *
  * @param rect The rectange to calculate the intersection with.
  *
  * @return The rectangle bounding the intersection.
  */
public Rectangle
intersection(Rectangle rect)
{
  // FIXME: What do we do if there is no intersection?  I'll return an
  // empty rectangle.  
  if (!intersects(rect))
    return(new Rectangle());

  int nx, ny, nw, nh;

  if (x > rect.x)
    nx = x;
  else
    nx = rect.x;

  if (y > rect.y)
    ny = y;
  else
    ny = rect.y;

  if ((x + width) < (rect.x + rect.width))
    nw = (x + width) - nx;
  else
    nw = (rect.x + rect.width) - nx;

  if ((y + height) < (rect.y + rect.height))
    nh = (y + height) - ny;
  else
    nh = (rect.y + rect.height) - ny;

  return(new Rectangle(nx, ny, nw, nh));
}

/*************************************************************************/

/**
  * Returns the smallest rectangle that contains both this rectangle
  * and the specified rectangle.
  *
  * @param rect The rectangle to compute the union with.
  *
  * @return The smallest rectangle containing both rectangles.
  */
public Rectangle
union(Rectangle rect)
{
  int nx, ny, nw, nh;

  if (x < rect.x)
    nx = x;
  else
    nx = rect.x;

  if (y < rect.y)
    ny = y;
  else
    ny = rect.y;

  if ((x + width) > (rect.x + rect.width))
    nw = (x + width) - nx;
  else
    nw = (rect.x + rect.width) - nx;

  if ((y - height) > (rect.y - rect.height))
    nh = (y + height) - ny;
  else
    nh = (rect.y + rect.height) - ny;

  return(new Rectangle(nx, ny, nw, nh));
}

/*************************************************************************/

/**
  * Modifies this rectangle so that it represents the smallest rectangle 
  * that contains both the existing rectangle and the specified point.
  *
  * @param point The point to add to this rectangle.
  */
public void
add(Point point)
{
  add(new Rectangle(point));
}

/*************************************************************************/

/**
  * Modifies this rectangle so that it represents the smallest rectangle 
  * that contains both the existing rectangle and the specified point.
  *
  * @param x The X coordinate of the point to add to this rectangle.
  * @param y The Y coordinate of the point to add to this rectangle.
  */
public void
add(int x, int y)
{
  add(new Rectangle(new Point(x, y)));
}

/*************************************************************************/

/**
  * Modifies this rectangle so that it represents the smallest rectangle 
  * that contains both the existing rectangle and the specified rectangle.
  *
  * @param rect The rectangle to add to this rectangle.
  */
public void
add(Rectangle rect)
{
  Rectangle newrect = union(rect);

  synchronized(this)
    {
      this.x = newrect.x;
      this.y = newrect.y;
      this.width = newrect.width;
      this.height = newrect.height;
    }
}

/*************************************************************************/

/**
  * Expands the rectangle by the specified amount.  The horizontal
  * and vertical expansion values are applied both to the X,Y coordinate
  * of this rectangle, and its width and height.  Thus the width and
  * height will increase by 2h and 2v accordingly.
  *
  * @param h The horizontal expansion value.
  * @param v The vertical expansion value.
  */
public void
grow(int h, int v)
{
  synchronized(this)
    {
      x -= h;
      y -= v;
      width += (2*h);
      height += (2*h);
    }
}

/*************************************************************************/

/**
  * Tests whether or not this rectangle is empty.  An empty rectangle
  * has a width or height of zero.
  *
  * @return <code>true</code> if the rectangle is empty, <code>false</code>
  * otherwise.
  */
public boolean
isEmpty()
{
  if ((width <= 0) || (height <= 0))
    return(true);
  else
    return(false);
}

/*************************************************************************/

/**
  * Tests this rectangle for equality against the specified object.  This
  * will be true if an only if the specified object:
  * <p>
  * <ul>
  * <li>Is not <code>null</code>.
  * <li>Is an instance of <code>Rectangle</code>.
  * <li>Has X and Y coordinates identical to this rectangle.
  * <li>Has a width and height identical to this rectangle.
  * </ul>
  *
  * @param obj The object to test against for equality.
  *
  * @return <code>true</code> if the specified object is equal to this one,
  * <code>false</code> otherwise.
  */
public boolean
equals(Object obj)
{
  if (obj == null)
    return(false);

  if (!(obj instanceof Rectangle))
    return(false);

  Rectangle rect = (Rectangle)obj;

  if ((rect.x != x) || (rect.y != y) || (rect.width != width) ||
      (rect.height != height))
    return(false);

  return(true);
}
 
/*************************************************************************/

/**
  * Returns a hash value for this object.
  *
  * @return A hash value for this object.
  */
public int
hashCode()
{
  return(x*y*width*height*37);
}

/*************************************************************************/

/**
  * Returns a string representation of this rectangle.
  *
  * @return A string representation of this rectangle.
  */
public String
toString()
{
  return(getClass().getName() + "(x=" + x + ", y=" + y + ", width=" +
         width + ", height=" + height + ")");
}

} // class Rectangle