-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazeComponent.java
More file actions
151 lines (121 loc) · 3.35 KB
/
MazeComponent.java
File metadata and controls
151 lines (121 loc) · 3.35 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//This class was provided by the instructor
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
@SuppressWarnings("serial")
public class MazeComponent extends JComponent
{
private int mazeWidth;
private int mazeHeight;
private int width;
private int height;
private int leftMargin = 10;
private int rightMargin = 10;
private int topMargin = 10;
private int bottomMargin = 10;
private Maze maze;
private double cellSize;
private double halfCellSize;
private ArrayList<Pair<Integer,Integer>> solution;
private BasicStroke thin = new BasicStroke(2);
private BasicStroke thick = new BasicStroke(5);
public MazeComponent(Maze aMaze, ArrayList<Pair<Integer,Integer>> aSolution)
{
maze = aMaze;
mazeWidth = maze.getWidth();
mazeHeight = maze.getHeight();
solution = aSolution;
}
public void paintComponent(Graphics g)
{
Graphics2D g2 = (Graphics2D) g;
width = this.getWidth();
height = this.getHeight();
g2.setColor(Color.WHITE);
Rectangle2D.Double rect = new Rectangle2D.Double(0,0,width,height);
g2.fill(rect);
g2.setColor(Color.BLACK);
g2.setStroke(thin);
Line2D.Double line = new Line2D.Double(0,0,0,0);
double hCellWidth = (double) (width - (leftMargin + rightMargin)) / mazeWidth;
double vCellHeight = (double) (height - (topMargin + bottomMargin)) / mazeHeight;
if(hCellWidth > vCellHeight)
{
cellSize = vCellHeight;
}
else
{
cellSize = hCellWidth;
}
halfCellSize = cellSize / 2;
for(int i = 0; i < mazeWidth; i++)
{
for(int j = 0; j < mazeHeight; j++)
{
double sx, ex, sy, ey;
// draw north wall
if(maze.isNorthWall(j, i))
{
sx = leftMargin + (i * cellSize);
ex = leftMargin + ((i + 1) * cellSize);
sy = topMargin + (j * cellSize);
ey = sy;
line.setLine(sx,sy,ex,ey);
g2.draw(line);
}
// draw east wall
if(maze.isEastWall(j, i))
{
sx = leftMargin + ((i + 1) * cellSize);
ex = sx;
sy = topMargin + (j * cellSize);
ey = topMargin + ((j + 1) * cellSize);
line.setLine(sx,sy,ex,ey);
g2.draw(line);
}
// draw south wall
if(maze.isSouthWall(j, i))
{
sx = leftMargin + (i * cellSize);
ex = leftMargin + ((i + 1) * cellSize);
sy = topMargin + ((j + 1) * cellSize);
ey = sy;
line.setLine(sx,sy,ex,ey);
g2.draw(line);
}
// draw west wall
if(maze.isWestWall(j, i))
{
sx = leftMargin + (i * cellSize);
ex = sx;
sy = topMargin + (j * cellSize);
ey = topMargin + ((j + 1) * cellSize);
line.setLine(sx,sy,ex,ey);
g2.draw(line);
}
}
}
// Draw a solution
g2.setColor(Color.GREEN);
g2.setStroke(thin);
int size = solution.size();
double sx, sy, ex, ey;
Pair<Integer,Integer> start, end;
for(int i = 0; i < size - 1; i++)
{
start = solution.get(i);
end = solution.get(i + 1);
sx = leftMargin + (start.snd() * cellSize) + halfCellSize;
ex = leftMargin + (end.snd() * cellSize) + halfCellSize;
sy = topMargin + (start.fst() * cellSize) + halfCellSize;
ey = topMargin + (end.fst() * cellSize) + halfCellSize;
line.setLine(sx,sy,ex,ey);
g2.draw(line);
}
}
}