-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRect.java
More file actions
75 lines (54 loc) · 1.18 KB
/
Rect.java
File metadata and controls
75 lines (54 loc) · 1.18 KB
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
import java.awt.*;
public class Rect extends Shape
{
int w;
int h;
public Rect(int x, int y, int w, int h)
{
super(x, y);
this.w = w;
this.h = h;
}
public void draw(Graphics win)
{
win.drawRect(x, y, w, h);
}
public void fill(Graphics win)
{
win.fillRect(x, y, w, h);
}
public boolean contains(int mx, int my)
{
return (mx >= x) && (mx <= x+w) && (my >= y) && (my <= y+h);
}
public boolean overlaps(Rect r)
{
return (r.x + r.w > x) && (r.y + r.h > y) && (x + w > r.x) && (y + h > r.y);
}
public void resizeBy(int dw, int dh)
{
w += dw;
h += dh;
}
public void setX(int dx){
x = dx;
}
public void setY(int dy){
y = dy;
}
public void moveTo(int x, int y)
{
this.x = x;
this.y = y;
}
public void setSize(int w, int h)
{
this.w = w;
this.h = h;
}
public int distanceTo(Rect r)
{
// using Manhattan distance, calculate the distance between this and another object
return Math.abs(x- r.x) + Math.abs(y- r.y);
}
}