forked from ChrisMayfield/ThinkJavaCode2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovingPolygon.java
More file actions
56 lines (48 loc) · 1.12 KB
/
MovingPolygon.java
File metadata and controls
56 lines (48 loc) · 1.12 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
/**
* A polygon that moves around the screen.
*/
public class MovingPolygon extends RegularPolygon {
private int dx;
private int dy;
/**
* Constructs a moving polygon.
*
* @param nsides the number of sides
* @param length length of each side
*/
public MovingPolygon(int nsides, int length) {
super(nsides, length);
this.dx = 10;
this.dy = 5;
}
/**
* @param dx how many pixels to move left/right
*/
public void setDx(int dx) {
this.dx = dx;
}
/**
* @param dy how many pixels to move up/down
*/
public void setDy(int dy) {
this.dy = dy;
}
@Override
public void act() {
// edge detection
for (int i = 0; i < npoints; i++) {
if (xpoints[i] < 0 || xpoints[i] > 800) {
dx *= -1;
break;
}
}
for (int i = 0; i < npoints; i++) {
if (ypoints[i] < 0 || ypoints[i] > 600) {
dy *= -1;
break;
}
}
// move one step
translate(dx, dy);
}
}