-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.java
More file actions
128 lines (98 loc) · 3.53 KB
/
Copy pathSprite.java
File metadata and controls
128 lines (98 loc) · 3.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
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
import java.awt.*;
public class Sprite {
Polygon shape; // The shape that is to be drawn
Color color; // The color of the shape
int width; // Width of the Sprite
int height; // Height of the Sprite
int x; // Horizontal coordinate of the center of the sprite
int y; // Vertical coordinate of the center of the sprite
public Sprite() {
// Initialize all the fields
shape=new Polygon();
width=0;
height=0;
x=0;
y=0;
color=Color.BLACK;
}
public void render(Graphics g) {
// The render method is responsible for positioning the sprite at the proper location
g.setColor(color);
Polygon renderedShape=new Polygon();
for(int i=0; i<shape.npoints; i++) {
int renderedx=shape.xpoints[i] + x + width / 2;
int renderedy=shape.ypoints[i] + y + height / 2;
renderedShape.addPoint(renderedx, renderedy);
}
g.fillPolygon(renderedShape);
}
public boolean containsPoint(int x, int y) {
// This returns true only if the point (x, y) is contained within the visible shape of the sprite
return shape.contains(x - this.x - width /2, y - this.y - height /2);
}
}
class ConnectionSprite extends Sprite {
public static final int HORZ_CONN=1;
public static final int VERT_CONN=2;
boolean connectionMade; // Tracks wether the ConnectionSprite has been clicked on
public ConnectionSprite() {
// Initialize all the fields
super();
connectionMade=false;
color=Color.WHITE;
}
public static ConnectionSprite createConnection(int type, int x, int y) {
ConnectionSprite conn=new ConnectionSprite();
if(type==ConnectionSprite.HORZ_CONN) {
conn.width=Dots.DOT_GAP;
conn.height=Dots.DOT_SIZE;
} else if(type==ConnectionSprite.VERT_CONN) {
conn.width=Dots.DOT_SIZE;
conn.height=Dots.DOT_GAP;
} else {
return null;
}
conn.x=x;
conn.y=y;
conn.shape.addPoint(-conn.width/2, -conn.height/2);
conn.shape.addPoint(-conn.width/2, conn.height/2);
conn.shape.addPoint(conn.width/2, conn.height/2);
conn.shape.addPoint(conn.width/2, -conn.height/2);
return conn;
}
}
class BoxSprite extends Sprite {
ConnectionSprite[] horizontalConnections; // The ConnectionSprites that are the top and bottom borders of the box
ConnectionSprite[] verticalConnections; // The ConnectionSprites that are the left and right borders of the box
int player; // Tracks the player that closed the box
public BoxSprite() {
super();
color=Color.WHITE; // Initially the box should be the same color as the background
horizontalConnections=new ConnectionSprite[2];
verticalConnections=new ConnectionSprite[2];
width=Dots.DOT_GAP;
height=Dots.DOT_GAP;
shape.addPoint(-width/2, -height/2);
shape.addPoint(-width/2, height/2);
shape.addPoint(width/2, height/2);
shape.addPoint(width/2, -height/2);
}
public boolean isBoxed() {
boolean boxed=true;
for(int i=0; i<2; i++) {
if(!horizontalConnections[i].connectionMade || !verticalConnections[i].connectionMade) {
boxed=false;
}
}
return boxed;
}
public static BoxSprite createBox(int x, int y, ConnectionSprite[] horizontalConnections, ConnectionSprite[] verticalConnections) {
BoxSprite box=new BoxSprite();
box.player=0;
box.x=x;
box.y=y;
box.horizontalConnections=horizontalConnections;
box.verticalConnections=verticalConnections;
return box;
}
}