-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFSmap.java
More file actions
334 lines (294 loc) · 9.31 KB
/
BFSmap.java
File metadata and controls
334 lines (294 loc) · 9.31 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.awt.Point;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class BFSmap extends JPanel{
private final int width = 1000, height = 700;
int x, y;
Image img, campus;
Boolean point = false;
HashMap<String, Point> map = new HashMap<String, Point>();
HashMap<String, ArrayList<String>> edges = new HashMap<String, ArrayList<String>>();
HashMap<String, Point> PointMap = new HashMap<String, Point>();
ArrayList<Edge> edgepath = new ArrayList<Edge>();
ArrayList<Vertex> vertexpath = new ArrayList<Vertex>();
Graph gr = new Graph();
HashMap<Vertex, ArrayList<Edge>> compiled = new HashMap<Vertex, ArrayList<Edge>>();
Vertex vselected = null, vhover = null, vtarget = null;
public BFSmap() throws IOException{
BoxLayout boxlayout = new BoxLayout(this, BoxLayout.Y_AXIS);
setLayout(boxlayout);
setBorder(BorderFactory.createTitledBorder("King's Academy Map"));
JTextArea displayarea = new JTextArea();
displayarea.setEditable(true);
JButton addpoint = new JButton("add point");
addpoint.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
WriteData();
}
});
JPanel innerPanel = new JPanel();
innerPanel.add(addpoint);
add(innerPanel);
JFrame frame = new JFrame();
frame.setSize(width, height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
this.setFocusable(true);
img = Toolkit.getDefaultToolkit().createImage("old.png");
campus = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
run();
}
public void CollectData() {
x = y = 0;
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e) {
String input = JOptionPane.showInputDialog(this, "Name of the Vertex");
Point v1 = new Point(x,y);
map.put(input, v1);
}
});
}
public void WriteData() {
try {
FileWriter writer = new FileWriter("MapData.txt", true);
for(String keys: map.keySet()) {
writer.write(keys +", "+ map.get(keys).x +", " +map.get(keys).y);
writer.write("\n");
System.out.println(keys +", "+ map.get(keys).x +", " +map.get(keys).y);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
//CollectData();
readFileVertex();
hover();
userinput();
}
private void readFileVertex() {
try {
BufferedReader in = new BufferedReader(new FileReader("MapData.txt"));
for (String line = in.readLine(); line != null; line = in.readLine()) {
String[] string = line.split(", ");
int xcor = Integer.parseInt(string[1]);
int ycor = Integer.parseInt(string[2]);
gr.addVertex(string[0], xcor, ycor);
}
repaint();
in.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found :( make sure file is in the project (not source code) and "
+ "has the correct name");
}
catch (IOException e) {}
ReadFileEdges();
}
private void ReadFileEdges() {
try {
BufferedReader in = new BufferedReader(new FileReader("MapData.txt"));
ArrayList<Integer> distances = new ArrayList<Integer>();
for (String line = in.readLine(); line != null; line = in.readLine()) {
String[] string = line.split(", ");
int p1x = gr.vertices.get(string[0]).x;
int p1y = gr.vertices.get(string[0]).y;
for(int i = 3; i<string.length; i++) {
int p2x = gr.vertices.get(string[i]).x;
int p2y = gr.vertices.get(string[i]).y;
int distance = (int) Math.sqrt(Math.pow(p2x-p1x, 2) + Math.pow(p2y-p1y, 2));
if(!distances.contains(distance)) {
distances.add(distance);
gr.addEdge(string[0], string[i], distance);
}
}
}
}
catch (FileNotFoundException e) {
System.out.println("File not found :( make sure file is in the project (not source code) and "
+ "has the correct name");
}
catch (IOException e) {}
}
public void paint(Graphics g) {
Point p2 = null;
Point p3 = null;
super.paint(g);
g.drawImage(campus, 0, 60, this);
Graphics2D g2 = (Graphics2D) g;
for(Vertex v: gr.vertices.values()) {
p2 = new Point(v.x, v.y);
for(Edge e : v.edges) {
p3 = new Point(e.get(v).x, e.get(v).y);
if(edgepath.contains(e)) {
g2.setColor(Color.BLUE);
}
else {
g2.setColor(Color.BLACK);
}
g2.setStroke(new BasicStroke(3));
g2.drawLine(p2.x, p2.y, p3.x, p3.y);
Point midpoint = new Point(((p2.x+ p3.x)/2), ((p2.y+p3.y)/2));
g.drawString(Integer.toString(e.distance), midpoint.x, midpoint.y);
}
}
for(String key : gr.vertices.keySet()) {
if(vselected!=null && (vselected.x == gr.vertices.get(key).x) && (vselected.y == gr.vertices.get(key).y)) {
g.setColor(Color.RED);
}
else if(vhover!=null && (vhover.x == gr.vertices.get(key).x) && (vhover.y == gr.vertices.get(key).y)) {
g.setColor(Color.RED);
}
else if(vtarget!=null && (vtarget.x == gr.vertices.get(key).x) && (vtarget.y == gr.vertices.get(key).y)) {
g.setColor(Color.RED);
}
else if(vertexpath.contains(gr.vertices.get(key))) {
g.setColor(Color.BLUE);
}
else {
g.setColor(Color.BLACK);
}
g.fillOval(gr.vertices.get(key).x, gr.vertices.get(key).y, 15, 15);
}
}
public boolean isinside(int cx, int cy, int rad, int x, int y) {
if ((x - cx) * (x - cx) + (y - cy) * (y - cy) <= rad * rad)
return true;
else
return false;
}
public void userinput() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
for(String find: gr.vertices.keySet()) {
if(isinside((gr.vertices.get(find).x), (gr.vertices.get(find).y), 15, e.getX(), e.getY())) {
vselected = gr.vertices.get(find);
repaint();
}
}
}
});
}
public void hover() {
addMouseMotionListener(new MouseAdapter() {
public void mouseMoved(MouseEvent e) {
if(vselected!=null) {
for(Vertex key : gr.vertices.values()) {
if(isinside(key.x, key.y, 15, e.getX(), e.getY())) {
vhover = key;
if(!vhover.equals(vselected) && !vhover.equals(vtarget)) {
vtarget = key;
dijkstra();
repaint();
}
}
}
}
}
});
}
public void dijkstra() {
HashMap<Vertex, Integer> distances = new HashMap<Vertex, Integer>();
PriorityQueue1 toVisit = new PriorityQueue1();
HashMap<Vertex, Edge> leadsto = new HashMap<Vertex, Edge>();
ArrayList<Vertex> visited = new ArrayList<Vertex>();
for(Vertex v: gr.vertices.values()) {
distances.put(v, Integer.MAX_VALUE);
}
toVisit.add(vselected, 0);
distances.put(vselected, 0);
//System.out.println("selected: " +vselected.place);
//System.out.println("target: "+vtarget.place);
int d = 0;
while(!toVisit.isEmpty()) {
Vertex trialselected = toVisit.pop();
//System.out.println(trialselected.place);
//System.out.println(trialselected.edges.size());
visited.add(trialselected);
if(trialselected.equals(vtarget)) {
//System.out.println("completed");
backtrace(leadsto);
return;
}
for(Edge e: trialselected.edges) {
Vertex neighbour = e.get(trialselected);
d = distances.get(trialselected) + e.distance;
if(!visited.contains(neighbour)) {
if(d < distances.get(neighbour)) {
//System.out.println(neighbour.place);
//System.out.println("d: "+d+ " and the distances.getneighbour is: "+distances.get(neighbour));
distances.put(neighbour, d);
leadsto.put(neighbour, e);
//System.out.println("is empty "+ toVisit.isEmpty());
if(toVisit.contains(neighbour)) {
//System.out.println("original:" +toVisit.getP(neighbour)+ " and the thing to update to is: "+d);
toVisit.update(neighbour, d);
}
else {
//System.out.println("inner else statement");
toVisit.add(neighbour, d);
}
}
}
}
}
}
public void backtrace(HashMap<Vertex, Edge> leadsto) {
if(!edgepath.isEmpty())
edgepath.clear();
if(!vertexpath.isEmpty())
vertexpath.clear();
System.out.println(" Starting backtrace..");
System.out.println("Target is: "+vtarget.place);
Vertex currentVertex = vtarget;
while(!currentVertex.equals(vselected)) {
Edge e = leadsto.get(currentVertex);
currentVertex = e.get(currentVertex);
edgepath.add(e);
vertexpath.add(currentVertex);
System.out.println(currentVertex.place);
}
System.out.println("Starts at: " +vselected.place);
repaint();
}
public static void main(String[] args) {
try {
BFSmap test = new BFSmap();
} catch (IOException e) {
e.printStackTrace();
}
}
}