-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapDisplay.java
309 lines (258 loc) · 9.05 KB
/
MapDisplay.java
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
import java.util.ArrayList;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import sun.misc.BASE64Decoder;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import javax.imageio.*;
class MapCanvas extends JPanel implements MouseMotionListener
{
private ArrayList<Graph> _graphs;
private int _width, _height;
private double _scaleX, _scaleY;
private String _message;
private int _messageX, _messageY;
private BufferedImage _image;
private int _imageX, _imageY;
private double _minLat,
_maxLat,
_minLong,
_maxLong;
public MapCanvas(int width, int height)
{
_width = width - 100;
_height = height - 100;
_message = "";
_messageX = 0;
_messageY = 0;
addMouseMotionListener(this);
_graphs = new ArrayList<Graph>();
}
public void addGraph(Graph graph)
{
_graphs.add(graph);
_minLat = (_minLat < graph.getMinLat()) ? _minLat : graph.getMinLat();
_minLong = (_minLong < graph.getMinLong()) ? _minLong : graph.getMinLong();
_maxLat = (_maxLat > graph.getMaxLat()) ? _maxLat : graph.getMaxLat();
_maxLong = (_maxLong > graph.getMaxLong()) ? _maxLong : graph.getMaxLong();
_scaleX = _width/(_maxLong - _minLong);
_scaleY = _height/(_maxLat - _minLat);
}
private void drawMap(Graphics g)
{
BasicStroke bs = new BasicStroke(4, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND);
BasicStroke bs2 = new BasicStroke(1, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_ROUND);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(bs);
Dimension size = getSize();
Insets insets = getInsets();
double w = size.width - insets.left - insets.right;
double h = size.height - insets.top - insets.bottom;
for(int j = 0; j < _graphs.size(); j++)
{
Graph curGraph = _graphs.get(j);
ArrayList<Node> nodes = curGraph.getGraph();
g2d.setColor(_graphs.get(j).getColor());
for(int i = 0; i < nodes.size(); i++)
{
Node node = nodes.get(i);
int y = latToY(node.latitude());
int x = longToX(node.longitude());
g2d.drawLine(x, y, x, y);
}
if(curGraph.getShortestPath() != null)
{
g2d.setStroke(bs2);
nodes = curGraph.getShortestPath();
Node node1 = nodes.get(0), node2;
for(int i = 0; i < nodes.size() - 1; i++)
{
node2 = nodes.get(i + 1);
g2d.drawLine(longToX(node1.longitude()), latToY(node1.latitude()),
longToX(node2.longitude()), latToY(node2.latitude()));
node1 = node2;
}
g2d.setStroke(bs);
}
}
g2d.drawString(_message, _messageX, _messageY);
if(_image != null)
{
g2d.drawImage(_image, _imageX, _imageY, null);
}
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
drawMap(g);
}
public void mouseDragged(MouseEvent e)
{
removeClosestToMouseRange(e.getX(), e.getY(), 20.0);
_image = null;
_message = "";
repaint();
}
public void mouseMoved(MouseEvent e)
{
Node temp = closestToMouseRange(e.getX(), e.getY(), 20.0);
if(temp != null)
{
//System.out.printf("%.2f, %.2f\n", temp.latitude(), temp.longitude());
_message = "This node!";
_messageY = latToY(temp.latitude());
_messageX = longToX(temp.longitude());
_image = temp.picture();
if(_image != null)
{
_imageX = _messageX - (int)(_image.getWidth()/2);
_imageY = _messageY - (int)(_image.getHeight());
}
}
else
{
_message = "";
_image = null;
}
repaint();
}
private Node closestToMouseRange(int x, int y, double range)
{
Double minDistance = 9999999.0;
Node closestNode = null;
for(int i = 0; i < _graphs.size(); i++)
{
ArrayList<Node> nodes = _graphs.get(i).getGraph();
for(int j = 0; j < nodes.size(); j++)
{
Node node = nodes.get(j);
int nY = latToY(node.latitude());
int nX = longToX(node.longitude());
Double tempDistance = Math.sqrt((x - nX)*(x - nX) + (y - nY)*(y - nY));
if(tempDistance < minDistance)
{
minDistance = tempDistance;
closestNode = node;
}
}
}
return minDistance <= range ? closestNode : null;
}
private void removeClosestToMouseRange(int x, int y, double range)
{
Double minDistance = Double.POSITIVE_INFINITY;
Node closestNode = null;
Graph graph = null;
for(int i = 0; i < _graphs.size(); i++)
{
ArrayList<Node> nodes = _graphs.get(i).getGraph();
for(int j = 0; j < nodes.size(); j++)
{
Node node = nodes.get(j);
int nY = latToY(node.latitude());
int nX = longToX(node.longitude());
Double tempDistance = Math.sqrt((x - nX)*(x - nX) + (y - nY)*(y - nY));
if(tempDistance < minDistance)
{
minDistance = tempDistance;
closestNode = node;
graph = _graphs.get(i);
}
}
}
if(minDistance <= range)
{
graph.removeNode(closestNode);
}
}
private int latToY(double latitude)
{
return (int)((_height + 50.0) - _scaleY*(_maxLat - (latitude - _minLat)));
}
private int longToX(double longitude)
{
return (int)(50 + _scaleX*(longitude - _minLong));
}
}
public class MapDisplay extends JFrame
{
public MapDisplay(ArrayList<Graph> graphs)
{
setTitle("MapDisplay");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MapCanvas drawer = new MapCanvas(800, 800);
for(int i = 0; i < graphs.size(); i++)
{
drawer.addGraph(graphs.get(i));
}
add(drawer);
setSize(800, 800);
setLocationRelativeTo(null);
}
public static void main(String[] args) throws IOException
{
Color[] colorArray = {Color.red, Color.blue, Color.black,
Color.green, Color.orange, Color.cyan,
Color.yellow, Color.magenta, Color.pink,
Color.gray, Color.darkGray, Color.lightGray};
BufferedImage testImage = new BufferedImage(50, 50, BufferedImage.TYPE_INT_RGB);
ArrayList<BufferedImage> imageArr = new ArrayList<BufferedImage>();
for(int i = 1; i < 134; i++)
{
imageArr.add(ImageIO.read(new File("batch\\batch" + i + ".jpg")));
}
for(int i = 1; i < 23; i++)
{
imageArr.add(ImageIO.read(new File("fresh\\" + i + ".jpg")));
}
/*BufferedReader br = new BufferedReader(new FileReader("test1.jpg"));
String str;
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
str = sb.toString();
//System.out.println(str);
BufferedImage testImage = ImageDecode.decodeImage(str);
//ImageIcon = new ImageIcon(img);*/
Graph testGraph = new Graph();
testGraph.addNode(0, 0);
for(int i = 0; i < Integer.parseInt(args[0]); i++)
{
testGraph.addNode(Math.random()*180, Math.random()*90, imageArr.get(i%155));
}
testGraph.addNode(180,90);
testGraph.setColor(Color.BLUE);
ArrayList<Graph> testGraphList = new ArrayList<Graph>();
//testGraphList.add(testGraph);
ArrayList<Graph> clusters = Clusters.clustering(testGraph, Integer.parseInt(args[1]));
for (int k = 0; k < clusters.size(); k++) {
if(clusters.get(k).getSize() > 0)
{
clusters.get(k).setColor(colorArray[k % colorArray.length]);
testGraphList.add(clusters.get(k));
}
}
MapDisplay md = new MapDisplay(testGraphList);
md.setVisible(true);
}
}