-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangle.java
More file actions
88 lines (77 loc) · 1.83 KB
/
Copy pathRectangle.java
File metadata and controls
88 lines (77 loc) · 1.83 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
76
77
78
79
80
81
82
83
84
85
86
87
import java.awt.Color;
import java.awt.Graphics;
/**
* A rectangle-shaped Shape
* Defined by an upper-left corner (x1,y1) and a lower-right corner (x2,y2)
* with x1<=x2 and y1<=y2
*
* @author Chris Bailey-Kellogg, Dartmouth CS 10, Fall 2012
* @author CBK, updated Fall 2016
* @author Tim Pierson Dartmouth CS 10, provided for Winter 2024
* @author godwin kangor
* * @author Ahmed Elmi
*/
public class Rectangle implements Shape {
// TODO: YOUR CODE HERE
private int x1, y1, x2, y2;
private Color color;
/**
* Creates a rectangle with only one corner
* @param x1 x coordinate of top left corner
* @param y1 y coordinate of top left corner
* @param color color of shape
*/
public Rectangle(int x1, int y1, Color color) {
this.x1 = x1;
this.x2 = x1;
this.y1 = y1;
this.y2 = y1;
this.color = color;
}
/**
* A rectangle defined by two corners
*/
public Rectangle(int x1, int y1, int x2, int y2, Color color) {
setCorners(x1, y1, x2, y2);
this.color = color;
}
/**
* Redefines the rectangle based on new corners
*/
public void setCorners(int x1, int y1, int x2, int y2) {
// Ensure correct upper left and lower right
this.x1 = Math.min(x1, x2);
this.y1 = Math.min(y1, y2);
this.x2 = Math.max(x1, x2);
this.y2 = Math.max(y1, y2);
}
@Override
public void moveBy(int dx, int dy) {
x1 += dx;
y1 += dy;
x2 += dx;
y2 += dy;
}
@Override
public Color getColor() {
return color;
}
@Override
public void setColor(Color color) {
this.color = color;
}
@Override
public boolean contains(int x, int y) {
return x >= x1 && x <= x2 && y >= y1 && y <= y2;
}
@Override
public void draw(Graphics g) {
g.setColor(color);
g.fillRect(x1, y1, (x2 - x1), (y2 - y1));
}
public String toString() {
return "rectangle" + " " + x1 + " " +
y1 + " " + x2 + " " + y2 + " " +
color.getRGB();
}
}