-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathMovableRectangle.java
More file actions
59 lines (50 loc) · 1.53 KB
/
MovableRectangle.java
File metadata and controls
59 lines (50 loc) · 1.53 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
package src.ex5;
public class MovableRectangle implements Movable {
private MovablePoint topLeft;
private MovablePoint bottomRight;
public MovableRectangle(int x1, int y1, int x2, int y2, int xSpeed, int ySpeed) {
topLeft = new MovablePoint(x1, y1, xSpeed, ySpeed);
bottomRight = new MovablePoint(x2, y2, xSpeed, ySpeed);
}
@Override
public void moveUp() {
if ( ! hasPointsSameSpeed()) {
return;
}
topLeft.y -= topLeft.ySpeed;
bottomRight.y -= bottomRight.ySpeed;
}
@Override
public void moveDown() {
if ( ! hasPointsSameSpeed()) {
return;
}
topLeft.y += topLeft.ySpeed;
bottomRight.y += bottomRight.ySpeed;
}
@Override
public void moveLeft() {
if ( ! hasPointsSameSpeed()) {
return;
}
topLeft.x -= topLeft.xSpeed;
bottomRight.x -= bottomRight.xSpeed;
}
@Override
public void moveRight() {
if ( ! hasPointsSameSpeed()) {
return;
}
topLeft.x += topLeft.xSpeed;
bottomRight.x += bottomRight.xSpeed;
}
private boolean hasPointsSameSpeed() {
return (topLeft.xSpeed == bottomRight.xSpeed)
&& (topLeft.ySpeed == bottomRight.ySpeed);
}
@Override
public String toString() {
return String.format("MovableRectangle with topLeft: %1$s and bottomRight: %2$s"
, topLeft.toString(), bottomRight.toString());
}
}