Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
Latest commit
166 lines (141 loc) · 5.01 KB
/
Copy pathMap.java
File metadata and controls
166 lines (141 loc) · 5.01 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
importjavax.swing.JFrame;
importjava.awt.Graphics;
importjava.awt.Graphics2D;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjava.awt.Dimension;
importjavax.swing.JComponent;
importjava.awt.geom.Line2D;
/**
* Displays the map and draws the current tour and status on top of it.
* Also maps from lat/lng to coordinates in the context of this component.
*
* @author @gcschmit
* @version 02jun2020
*/
publicclassMapextendsJComponent
{
privatestaticfinalintWIDTH = 800;
privatestaticfinalintHEIGHT = 600;
privatestaticfinaldoubleMIN_LAT = 50.19395025319712;
privatestaticfinaldoubleMAX_LAT = 30.320009196592867;
privatestaticfinaldoubleMIN_LNG = -103.57812500000001;
privatestaticfinaldoubleMAX_LNG = -68.42187500000001;
privateBufferedImagemapImage;
privateTourtour;
privateintgen;
/**
* Constructs a new Map.
*/
publicMap()
{
try
{
this.mapImage = ImageIO.read(newFile("map.png"));
}
catch (IOExceptione)
{
}
}
/**
* Overrides JComponent's getPreferredSize method to return a size large enough to
* encapsulate the map
*
* @returns the preferred size for the component
*
*/
@Override
publicDimensiongetPreferredSize()
{
// the component needs to be large enough to encapsulate the map
returnnewDimension(WIDTH, HEIGHT);
}
/**
* Sets the tour and generation to display on this map to the specified tour and generation.
* These will be displayed the next time the paintComponent method is invoked by
* the run-time.
*
* @param currentTour the tour to display on this map
* @param generation the generation to display on this map
*/
publicvoidsetCurrentTour(TourcurrentTour, intgeneration)
{
this.tour = currentTour;
this.gen = generation;
// ask the Java Run-Time to invoke the paintComponent method at the next opportunity
this.repaint();
}
/**
* This method is invoked by the Java Run-Time whenever the component needs to be redrawn.
* It does not need to be invoked explicitly. It draws both the map and the current tour.
* It will be invoked shortly after the setCurrentTour method is invoked.
*
* @param g the graphics object to be used for all graphics operations
*
*/
@Override
publicvoidpaintComponent(Graphicsg)
{
Graphics2Dg2 = (Graphics2D)g;
// scale the Google map image file to this component
g2.drawImage(this.mapImage, 0, 0, this.getWidth(), this.getHeight(),
0, 0, 1600, 1200, null);
drawTour(g2);
// draw the current status (i.e., generation and current best tour)
Stringstatus = String.format("Map [%3d] %s", gen, (this.tour == null ? "<null>" : tour));
g2.setColor(Color.BLACK);
g2.drawString(status, 200, 25);
}
privatevoiddrawTour(Graphics2Dg2)
{
if(this.tour == null)
{
return;
}
finalintOFFSET = 20;
City[] cities = this.tour.getCities();
g2.setFont(newFont("SansSerif", Font.BOLD, 16));
doublex1, y1, x2, y2;
for (inti = 0; i < cities.length; i++)
{
x1 = Map.lngToX(cities[i].getLng(), this.getWidth());
y1 = Map.latToY(cities[i].getLat(), this.getHeight());
intnextCityIndex = i + 1;
if(i == cities.length - 1) // loop back to the first city
{
nextCityIndex = 0;
}
x2 = Map.lngToX(cities[nextCityIndex].getLng(), this.getWidth());
y2 = Map.latToY(cities[nextCityIndex].getLat(), this.getHeight());
Line2D.Doublepath = newLine2D.Double(x1, y1, x2, y2);
g2.setColor(Color.BLUE);
g2.draw(path);
g2.setColor(Color.RED);
g2.drawString(Integer.toString(i), (float)(x1 - OFFSET), (float)(y1 + OFFSET));
}
}
/**
* Converts the specified longitude to the x coordinate in the context of this component
*
* @param lng the longitude value to convert
* @return the x coordinate that corresponds to the specified longitude
*/
publicstaticdoublelngToX(doublelng, doublewidth)
{
return (lng - MIN_LNG) / (MAX_LNG - MIN_LNG) * width;
}
/**
* Converts the specified latitude to the y coordinate in the context of this component
*
* @param lng the latitude value to convert
* @return the y coordinate that corresponds to the specified latitude
*/
publicstaticdoublelatToY(doublelat, doubleheight)
{
return (lat - MIN_LAT) / (MAX_LAT - MIN_LAT) * height;
}
}