-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocatorMap.java
executable file
·359 lines (308 loc) · 12.5 KB
/
LocatorMap.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// LocatorMap.java provides a class for a locator map.
//----------------------------------------------------
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Observable;
import java.util.Observer;
import java.text.DecimalFormat;
import javax.swing.JPanel;
import javax.swing.JViewport;
public class LocatorMap extends JPanel implements MouseListener,
MouseMotionListener, Observer
{
private imgViewer applet; // reference to the applet
private Image markImage; // image for marking the current location
private Point loc; // location of position indicator (in pixels)
private DecimalFormat LatLonFormat;
private Image offScreenBuffer = null; // off screen drawing buffer for
// eliminating flicker on some browsers
// define the options for the locator map
public static final int GEOGRAPHIC_MAP = 0;
public static final int US_GEOGRAPHIC_MAP = 1;
public static final int SINUSOIDAL_MAP = 2;
public static final int POLARSTEREOGRAPHIC_MAP = 3;
private static final int NUM_MAPS = 4;
private LocatorMapImpl currentMap; // reference to the current locator map
private LocatorMapImpl maps[]; // array for the locator maps
// Constructor for the locatorMap
//-------------------------------
LocatorMap(imgViewer parent)
{
applet = parent; // Save parent reference
setBackground(Color.BLACK); // Set the background black
// load the location marker image
markImage = applet.getImage(applet.getCodeBase(),
"graphics/ctxmarker.gif");
// create the locator map objects
maps = new LocatorMapImpl[NUM_MAPS];
// default to not showing a map
currentMap = null;
loc = new Point(1,1);
// Set up the formatter for lat/lon output
LatLonFormat = new DecimalFormat (" 0.00;-0.00");
// route wanted events to this object
addMouseListener(this);
addMouseMotionListener(this);
// default the size to the geographic image size until one is actually
// specified
setSize(300,300);
}
// initialize the starting location
//---------------------------------
public void initialize(double startLat, double startLong)
{
// set the correct map for the current sensor
setCurrentMap(applet.sensorMenu.getCurrentSensor().locatorMap);
// initialize the displayed location
LatLong latLong = new LatLong(startLat, startLong);
setLoc(latLong);
}
// method to return a map's configuration object
//----------------------------------------------
public GeographicLocatorMapConfig getMapConfig(int mapNumber)
{
LocatorMapImpl map = getMap(mapNumber);
return map.getMapConfig();
}
// method to return the locator map for a given map number. If the map
// has not been created yet, it is created.
//---------------------------------------------------------------------
private LocatorMapImpl getMap(int mapNumber)
{
// load the locator maps only when they are really needed in case
// the user never looks at a dataset that doesn't need one of them
if (maps[mapNumber] == null)
{
if (mapNumber == SINUSOIDAL_MAP)
maps[SINUSOIDAL_MAP] = new SinusoidalLocatorMap(applet);
else if (mapNumber == US_GEOGRAPHIC_MAP)
{
maps[US_GEOGRAPHIC_MAP] = new GeographicLocatorMap(applet,
new USLocatorMapConfig());
}
else if (mapNumber == POLARSTEREOGRAPHIC_MAP)
{
maps[POLARSTEREOGRAPHIC_MAP]
= new PolarStereographicLocatorMap(applet);
}
else
{
maps[mapNumber] = new GeographicLocatorMap(applet,
new LocatorMapConfig());
}
}
return maps[mapNumber];
}
// method to set the currently display map image
//----------------------------------------------
private void setCurrentMap(int newMapNumber)
{
currentMap = getMap(newMapNumber);
// if there is an offscreen buffer, black fill it in case the new
// map is smaller than the previous map (otherwise a portion of the
// old map might be visibile if the applet window is dragged to be
// very large)
if (offScreenBuffer != null)
{
Graphics offg = offScreenBuffer.getGraphics();
int width = offScreenBuffer.getWidth(null);
int height = offScreenBuffer.getHeight(null);
offg.setColor(Color.BLACK);
offg.fillRect(0, 0, width, height);
offg.dispose();
}
setSize(currentMap.imageSize);
setPreferredSize(currentMap.imageSize);
// make sure the parent scrolling area picks up the new size
applet.locatorMapScroll.doLayout();
}
// Method to set the location from a Latitude, Longitude value
//------------------------------------------------------------
private void setLoc(LatLong latLong)
{
loc = currentMap.latLongToPixel(latLong);
updateScroll();
}
// Method to set the location from a grid col/row
//-----------------------------------------------
private void setLoc(int gridCol, int gridRow)
{
loc = currentMap.gridToPixel(gridCol, gridRow);
updateScroll();
}
// Method to set the displayed scroll area location to display the
// current location
//----------------------------------------------------------------
private void updateScroll()
{
int xScrollLoc;
int yScrollLoc;
// calculate the correct scroll location for the locator map
Dimension parentSize = applet.locatorMapScroll.getSize();
int width = applet.locatorMapScroll.getVerticalScrollBar().getWidth();
int height
= applet.locatorMapScroll.getHorizontalScrollBar().getHeight();
int adjustX = (parentSize.width - width)/2;
int adjustY = (parentSize.height - height)/2;
xScrollLoc = loc.x - adjustX;
if (xScrollLoc < 1)
xScrollLoc = 1;
else if (xScrollLoc > (currentMap.imageSize.width - parentSize.width
+ width))
{
xScrollLoc = currentMap.imageSize.width - parentSize.width
+ width + 3;
}
yScrollLoc = loc.y - adjustY;
if (yScrollLoc < 1)
yScrollLoc = 1;
else if (yScrollLoc > (currentMap.imageSize.height - parentSize.height
+ height))
{
yScrollLoc = currentMap.imageSize.height - parentSize.height
+ height + 3;
}
// set the new scroll position and force a repaint
JViewport view = applet.locatorMapScroll.getViewport();
Point upperleft = new Point(xScrollLoc, yScrollLoc);
view.setViewPosition(upperleft);
applet.locatorMapScroll.revalidate();
repaint();
}
// This method is overwritten to reduce flickering on the paint() method...
//-------------------------------------------------------------------------
public void update(Graphics g) { paint(g); }
// Method for the Observer interface
//----------------------------------
public void update(Observable ob, Object arg)
{
int sensorMapNumber = applet.sensorMenu.getCurrentSensor().locatorMap;
if ((maps[sensorMapNumber] != currentMap) || (currentMap == null))
setCurrentMap(sensorMapNumber);
// update the icon location in the locator map
MosaicData md = applet.imgArea.md;
setLoc(md.gridCol,md.gridRow);
}
// The paint() method redraws all current images
//----------------------------------------------
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// don't paint anything if the map hasn't been set
if (currentMap == null)
return;
Dimension mapSize = getSize();
// make sure the offScreenBuffer is large enough (if it exists)
// in case the different locator maps are different sizes
if ((offScreenBuffer != null) &&
((offScreenBuffer.getHeight(null) < mapSize.height)
||(offScreenBuffer.getWidth(null) < mapSize.width)))
{
// not large enough, so eliminate
offScreenBuffer.flush();
offScreenBuffer = null;
}
// allocate the off screen drawing buffer if not available
if (offScreenBuffer == null)
{
offScreenBuffer = createImage(mapSize.width,mapSize.height);
}
// get a graphics context for drawing to the off screen buffer
Graphics offg = offScreenBuffer.getGraphics();
// draw the background image, lines, and location marker
offg.drawImage(currentMap.mapImage,0,0,this);
if (currentMap.useBoundaryImage)
{
offg.drawImage(currentMap.worldBoundaries, 0, 0, this);
}
offg.drawImage(markImage, loc.x-markImage.getWidth(null)/2,
loc.y-markImage.getHeight(null)/2, this);
// dispose the off screen graphics context to help garbage collection
offg.dispose();
// copy the off screen buffer to the display
g.drawImage(offScreenBuffer,0,0,this);
}
// Process Mouse pressed events
//-----------------------------
public void mousePressed(MouseEvent event)
{
if (currentMap == null)
return;
int x = event.getX();
int y = event.getY();
currentMap.moveTo(x, y);
}
// method to handle the mouse leaving the area
//--------------------------------------------
public void mouseExited(MouseEvent event)
{
// clear status bar when leaving to clear lat/long
applet.statusBar.showStatus("");
}
// Dummy event handlers required for a MouseListener interface
public void mouseReleased(MouseEvent event) { }
public void mouseClicked(MouseEvent event) { }
public void mouseEntered(MouseEvent event) { }
// Display the lat/long and grid location under the mouse cursor
//--------------------------------------------------------------
public void mouseMoved(MouseEvent event)
{
if (currentMap == null)
return;
int x = event.getX();
int y = event.getY();
Sensor currSensor = applet.sensorMenu.getCurrentSensor();
NavigationModel nm = currSensor.navModel;
// get the current lat/long and grid location under the mouse cursor
LatLong latLong = currentMap.pixelToLatLong(x, y);
Point gridLoc = currentMap.pixelToGrid(x,y);
// build the location message using a string buffer for efficiency
StringBuffer msg = new StringBuffer();
// add the lat/long to the message if it is available
if (latLong != null)
{
msg.append("Lat/Long: ");
msg.append(LatLonFormat.format(latLong.latitude));
msg.append(", ");
msg.append(LatLonFormat.format(latLong.longitude));
msg.append(" degrees");
}
// add the grid location to the message if grid entry is supported
if (!currSensor.hideGridEntry)
{
msg.append(", ");
msg.append(nm.getModelName());
msg.append(" ");
msg.append(nm.getColName());
msg.append("/");
msg.append(nm.getRowName());
msg.append(" ");
msg.append(nm.getColumnString(gridLoc.x));
msg.append(", ");
msg.append(nm.getRowString(gridLoc.y));
}
// display the message in the status bar
applet.statusBar.showStatus(msg.toString());
}
// Dummy event handler required for a MouseMotionListener interface
public void mouseDragged(MouseEvent event) { }
public void cleanup()
{
if (offScreenBuffer != null)
{
for (int i = 0; i < maps.length; i++)
{
if (maps[i] != null)
maps[i].cleanup();
}
offScreenBuffer.flush();
offScreenBuffer = null;
}
}
}