This repository has been archived by the owner on May 24, 2023. It is now read-only.
/
JMapViewer.java
790 lines (706 loc) · 25.7 KB
/
JMapViewer.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
package org.openstreetmap.gui.jmapviewer;
//License: GPL. Copyright 2008 by Jan Peter Stotz
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.font.TextAttribute;
import java.awt.geom.Rectangle2D;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle;
import org.openstreetmap.gui.jmapviewer.interfaces.TileCache;
import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener;
import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
/**
*
* Provides a simple panel that displays pre-rendered map tiles loaded from the
* OpenStreetMap project.
*
* @author Jan Peter Stotz
*
*/
public class JMapViewer extends JPanel implements TileLoaderListener {
private static final long serialVersionUID = 1L;
/**
* Vectors for clock-wise tile painting
*/
protected static final Point[] move = { new Point(1, 0), new Point(0, 1), new Point(-1, 0), new Point(0, -1) };
public static final int MAX_ZOOM = 22;
public static final int MIN_ZOOM = 0;
protected List<MapMarker> mapMarkerList;
protected List<MapRectangle> mapRectangleList;
protected boolean mapMarkersVisible;
protected boolean mapRectanglesVisible;
protected boolean tileGridVisible;
protected TileController tileController;
/**
* x- and y-position of the center of this map-panel on the world map
* denoted in screen pixel regarding the current zoom level.
*/
protected Point center;
/**
* Current zoom level
*/
protected int zoom;
protected JSlider zoomSlider;
protected JButton zoomInButton;
protected JButton zoomOutButton;
private TileSource tileSource;
// Attribution
private Image attrImage;
private String attrTermsUrl;
public static final Font ATTR_FONT = new Font("Arial", Font.PLAIN, 10);
public static final Font ATTR_LINK_FONT;
static {
HashMap<TextAttribute, Integer> aUnderline = new HashMap<TextAttribute, Integer>();
aUnderline.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
ATTR_LINK_FONT = ATTR_FONT.deriveFont(aUnderline);
}
/**
* Creates a standard {@link JMapViewer} instance that can be controlled via
* mouse: hold right mouse button for moving, double click left mouse button
* or use mouse wheel for zooming. Loaded tiles are stored the
* {@link MemoryTileCache} and the tile loader uses 4 parallel threads for
* retrieving the tiles.
*/
public JMapViewer() {
this(new MemoryTileCache(), 4);
new DefaultMapController(this);
}
public JMapViewer(TileCache tileCache, int downloadThreadCount) {
super();
tileSource = new OsmTileSource.Mapnik();
tileController = new TileController(tileSource, tileCache, this);
mapMarkerList = new LinkedList<MapMarker>();
mapRectangleList = new LinkedList<MapRectangle>();
mapMarkersVisible = true;
mapRectanglesVisible = true;
tileGridVisible = false;
setLayout(null);
initializeZoomSlider();
setMinimumSize(new Dimension(tileSource.getTileSize(), tileSource.getTileSize()));
setPreferredSize(new Dimension(400, 400));
setDisplayPositionByLatLon(50, 9, 3);
//setToolTipText("");
}
@Override
public String getToolTipText(MouseEvent event) {
// Point screenPoint = event.getLocationOnScreen();
// Coordinate c = getPosition(screenPoint);
return super.getToolTipText(event);
}
protected void initializeZoomSlider() {
zoomSlider = new JSlider(MIN_ZOOM, tileController.getTileSource().getMaxZoom());
zoomSlider.setOrientation(JSlider.VERTICAL);
zoomSlider.setBounds(10, 10, 30, 150);
zoomSlider.setOpaque(false);
zoomSlider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
setZoom(zoomSlider.getValue());
}
});
add(zoomSlider);
int size = 18;
try {
ImageIcon icon = new ImageIcon(getClass().getResource("images/plus.png"));
zoomInButton = new JButton(icon);
} catch (Exception e) {
zoomInButton = new JButton("+");
zoomInButton.setFont(new Font("sansserif", Font.BOLD, 9));
zoomInButton.setMargin(new Insets(0, 0, 0, 0));
}
zoomInButton.setBounds(4, 155, size, size);
zoomInButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
zoomIn();
}
});
add(zoomInButton);
try {
ImageIcon icon = new ImageIcon(getClass().getResource("images/minus.png"));
zoomOutButton = new JButton(icon);
} catch (Exception e) {
zoomOutButton = new JButton("-");
zoomOutButton.setFont(new Font("sansserif", Font.BOLD, 9));
zoomOutButton.setMargin(new Insets(0, 0, 0, 0));
}
zoomOutButton.setBounds(8 + size, 155, size, size);
zoomOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
zoomOut();
}
});
add(zoomOutButton);
}
/**
* Changes the map pane so that it is centered on the specified coordinate
* at the given zoom level.
*
* @param lat
* latitude of the specified coordinate
* @param lon
* longitude of the specified coordinate
* @param zoom
* {@link #MIN_ZOOM} <= zoom level <= {@link #MAX_ZOOM}
*/
public void setDisplayPositionByLatLon(double lat, double lon, int zoom) {
setDisplayPositionByLatLon(new Point(getWidth() / 2, getHeight() / 2), lat, lon, zoom);
}
/**
* Changes the map pane so that the specified coordinate at the given zoom
* level is displayed on the map at the screen coordinate
* <code>mapPoint</code>.
*
* @param mapPoint
* point on the map denoted in pixels where the coordinate should
* be set
* @param lat
* latitude of the specified coordinate
* @param lon
* longitude of the specified coordinate
* @param zoom
* {@link #MIN_ZOOM} <= zoom level <=
* {@link TileSource#getMaxZoom()}
*/
public void setDisplayPositionByLatLon(Point mapPoint, double lat, double lon, int zoom) {
int x = OsmMercator.LonToX(lon, zoom);
int y = OsmMercator.LatToY(lat, zoom);
setDisplayPosition(mapPoint, x, y, zoom);
}
public void setDisplayPosition(int x, int y, int zoom) {
setDisplayPosition(new Point(getWidth() / 2, getHeight() / 2), x, y, zoom);
}
public void setDisplayPosition(Point mapPoint, int x, int y, int zoom) {
if (zoom > tileController.getTileSource().getMaxZoom() || zoom < MIN_ZOOM)
return;
// Get the plain tile number
Point p = new Point();
p.x = x - mapPoint.x + getWidth() / 2;
p.y = y - mapPoint.y + getHeight() / 2;
center = p;
setIgnoreRepaint(true);
try {
int oldZoom = this.zoom;
this.zoom = zoom;
if (oldZoom != zoom) {
zoomChanged(oldZoom);
}
if (zoomSlider.getValue() != zoom) {
zoomSlider.setValue(zoom);
}
} finally {
setIgnoreRepaint(false);
repaint();
}
}
/**
* Sets the displayed map pane and zoom level so that all map markers are
* visible.
*/
public void setDisplayToFitMapMarkers() {
if (mapMarkerList == null || mapMarkerList.size() == 0)
return;
int x_min = Integer.MAX_VALUE;
int y_min = Integer.MAX_VALUE;
int x_max = Integer.MIN_VALUE;
int y_max = Integer.MIN_VALUE;
int mapZoomMax = tileController.getTileSource().getMaxZoom();
for (MapMarker marker : mapMarkerList) {
int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax);
int y = OsmMercator.LatToY(marker.getLat(), mapZoomMax);
x_max = Math.max(x_max, x);
y_max = Math.max(y_max, y);
x_min = Math.min(x_min, x);
y_min = Math.min(y_min, y);
}
int height = Math.max(0, getHeight());
int width = Math.max(0, getWidth());
// System.out.println(x_min + " < x < " + x_max);
// System.out.println(y_min + " < y < " + y_max);
// System.out.println("tiles: " + width + " " + height);
int newZoom = mapZoomMax;
int x = x_max - x_min;
int y = y_max - y_min;
while (x > width || y > height) {
// System.out.println("zoom: " + zoom + " -> " + x + " " + y);
newZoom--;
x >>= 1;
y >>= 1;
}
x = x_min + (x_max - x_min) / 2;
y = y_min + (y_max - y_min) / 2;
int z = 1 << (mapZoomMax - newZoom);
x /= z;
y /= z;
setDisplayPosition(x, y, newZoom);
}
/**
* Sets the displayed map pane and zoom level so that all map markers are
* visible.
*/
public void setDisplayToFitMapRectangle() {
if (mapRectangleList == null || mapRectangleList.size() == 0)
return;
int x_min = Integer.MAX_VALUE;
int y_min = Integer.MAX_VALUE;
int x_max = Integer.MIN_VALUE;
int y_max = Integer.MIN_VALUE;
int mapZoomMax = tileController.getTileSource().getMaxZoom();
for (MapRectangle rectangle : mapRectangleList) {
x_max = Math.max(x_max, OsmMercator.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax));
y_max = Math.max(y_max, OsmMercator.LatToY(rectangle.getTopLeft().getLat(), mapZoomMax));
x_min = Math.min(x_min, OsmMercator.LonToX(rectangle.getTopLeft().getLon(), mapZoomMax));
y_min = Math.min(y_min, OsmMercator.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax));
}
int height = Math.max(0, getHeight());
int width = Math.max(0, getWidth());
// System.out.println(x_min + " < x < " + x_max);
// System.out.println(y_min + " < y < " + y_max);
// System.out.println("tiles: " + width + " " + height);
int newZoom = mapZoomMax;
int x = x_max - x_min;
int y = y_max - y_min;
while (x > width || y > height) {
// System.out.println("zoom: " + zoom + " -> " + x + " " + y);
newZoom--;
x >>= 1;
y >>= 1;
}
x = x_min + (x_max - x_min) / 2;
y = y_min + (y_max - y_min) / 2;
int z = 1 << (mapZoomMax - newZoom);
x /= z;
y /= z;
setDisplayPosition(x, y, newZoom);
}
/**
* Calculates the latitude/longitude coordinate of the center of the
* currently displayed map area.
*
* @return latitude / longitude
*/
public Coordinate getPosition() {
double lon = OsmMercator.XToLon(center.x, zoom);
double lat = OsmMercator.YToLat(center.y, zoom);
return new Coordinate(lat, lon);
}
/**
* Converts the relative pixel coordinate (regarding the top left corner of
* the displayed map) into a latitude / longitude coordinate
*
* @param mapPoint
* relative pixel coordinate regarding the top left corner of the
* displayed map
* @return latitude / longitude
*/
public Coordinate getPosition(Point mapPoint) {
return getPosition(mapPoint.x, mapPoint.y);
}
/**
* Converts the relative pixel coordinate (regarding the top left corner of
* the displayed map) into a latitude / longitude coordinate
*
* @param mapPointX
* @param mapPointY
* @return
*/
public Coordinate getPosition(int mapPointX, int mapPointY) {
int x = center.x + mapPointX - getWidth() / 2;
int y = center.y + mapPointY - getHeight() / 2;
double lon = OsmMercator.XToLon(x, zoom);
double lat = OsmMercator.YToLat(y, zoom);
return new Coordinate(lat, lon);
}
/**
* Calculates the position on the map of a given coordinate
*
* @param lat
* @param lon
* @param checkOutside
* @return point on the map or <code>null</code> if the point is not visible
* and checkOutside set to <code>true</code>
*/
public Point getMapPosition(double lat, double lon, boolean checkOutside) {
int x = OsmMercator.LonToX(lon, zoom);
int y = OsmMercator.LatToY(lat, zoom);
x -= center.x - getWidth() / 2;
y -= center.y - getHeight() / 2;
if (checkOutside) {
if (x < 0 || y < 0 || x > getWidth() || y > getHeight())
return null;
}
return new Point(x, y);
}
/**
* Calculates the position on the map of a given coordinate
*
* @param lat
* @param lon
* @return point on the map or <code>null</code> if the point is not visible
*/
public Point getMapPosition(double lat, double lon) {
return getMapPosition(lat, lon, true);
}
/**
* Calculates the position on the map of a given coordinate
*
* @param coord
* @return point on the map or <code>null</code> if the point is not visible
*/
public Point getMapPosition(Coordinate coord) {
if (coord != null)
return getMapPosition(coord.getLat(), coord.getLon());
else
return null;
}
/**
* Calculates the position on the map of a given coordinate
*
* @param coord
* @return point on the map or <code>null</code> if the point is not visible
* and checkOutside set to <code>true</code>
*/
public Point getMapPosition(Coordinate coord, boolean checkOutside) {
if (coord != null)
return getMapPosition(coord.getLat(), coord.getLon(), checkOutside);
else
return null;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int iMove = 0;
int tilesize = tileSource.getTileSize();
int tilex = center.x / tilesize;
int tiley = center.y / tilesize;
int off_x = (center.x % tilesize);
int off_y = (center.y % tilesize);
int w2 = getWidth() / 2;
int h2 = getHeight() / 2;
int posx = w2 - off_x;
int posy = h2 - off_y;
int diff_left = off_x;
int diff_right = tilesize - off_x;
int diff_top = off_y;
int diff_bottom = tilesize - off_y;
boolean start_left = diff_left < diff_right;
boolean start_top = diff_top < diff_bottom;
if (start_top) {
if (start_left) {
iMove = 2;
} else {
iMove = 3;
}
} else {
if (start_left) {
iMove = 1;
} else {
iMove = 0;
}
} // calculate the visibility borders
int x_min = -tilesize;
int y_min = -tilesize;
int x_max = getWidth();
int y_max = getHeight();
// paint the tiles in a spiral, starting from center of the map
boolean painted = true;
int x = 0;
while (painted) {
painted = false;
for (int i = 0; i < 4; i++) {
if (i % 2 == 0) {
x++;
}
for (int j = 0; j < x; j++) {
if (x_min <= posx && posx <= x_max && y_min <= posy && posy <= y_max) {
// tile is visible
Tile tile = tileController.getTile(tilex, tiley, zoom);
if (tile != null) {
painted = true;
tile.paint(g, posx, posy);
if (tileGridVisible) {
g.drawRect(posx, posy, tilesize, tilesize);
}
}
}
Point p = move[iMove];
posx += p.x * tilesize;
posy += p.y * tilesize;
tilex += p.x;
tiley += p.y;
}
iMove = (iMove + 1) % move.length;
}
}
// outer border of the map
int mapSize = tilesize << zoom;
g.drawRect(w2 - center.x, h2 - center.y, mapSize, mapSize);
// g.drawString("Tiles in cache: " + tileCache.getTileCount(), 50, 20);
if (mapRectanglesVisible && mapRectangleList != null) {
for (MapRectangle rectangle : mapRectangleList) {
Coordinate topLeft = rectangle.getTopLeft();
Coordinate bottomRight = rectangle.getBottomRight();
if (topLeft != null && bottomRight != null) {
Point pTopLeft = getMapPosition(topLeft.getLat(), topLeft.getLon(), false);
Point pBottomRight = getMapPosition(bottomRight.getLat(), bottomRight.getLon(), false);
if (pTopLeft != null && pBottomRight != null) {
rectangle.paint(g, pTopLeft, pBottomRight);
}
}
}
}
if (mapMarkersVisible && mapMarkerList != null) {
for (MapMarker marker : mapMarkerList) {
paintMarker(g, marker);
}
}
paintAttribution(g);
}
/**
* Paint a single marker.
*/
protected void paintMarker(Graphics g, MapMarker marker) {
Point p = getMapPosition(marker.getLat(), marker.getLon());
if (p != null) {
marker.paint(g, p);
}
}
/**
* Moves the visible map pane.
*
* @param x
* horizontal movement in pixel.
* @param y
* vertical movement in pixel
*/
public void moveMap(int x, int y) {
center.x += x;
center.y += y;
repaint();
}
/**
* @return the current zoom level
*/
public int getZoom() {
return zoom;
}
/**
* Increases the current zoom level by one
*/
public void zoomIn() {
setZoom(zoom + 1);
}
/**
* Increases the current zoom level by one
*/
public void zoomIn(Point mapPoint) {
setZoom(zoom + 1, mapPoint);
}
/**
* Decreases the current zoom level by one
*/
public void zoomOut() {
setZoom(zoom - 1);
}
/**
* Decreases the current zoom level by one
*/
public void zoomOut(Point mapPoint) {
setZoom(zoom - 1, mapPoint);
}
public void setZoom(int zoom, Point mapPoint) {
if (zoom > tileController.getTileSource().getMaxZoom() || zoom < tileController.getTileSource().getMinZoom()
|| zoom == this.zoom)
return;
Coordinate zoomPos = getPosition(mapPoint);
tileController.cancelOutstandingJobs(); // Clearing outstanding load
// requests
setDisplayPositionByLatLon(mapPoint, zoomPos.getLat(), zoomPos.getLon(), zoom);
}
public void setZoom(int zoom) {
setZoom(zoom, new Point(getWidth() / 2, getHeight() / 2));
}
/**
* Every time the zoom level changes this method is called. Override it in
* derived implementations for adapting zoom dependent values. The new zoom
* level can be obtained via {@link #getZoom()}.
*
* @param oldZoom
* the previous zoom level
*/
protected void zoomChanged(int oldZoom) {
zoomSlider.setToolTipText("Zoom level " + zoom);
zoomInButton.setToolTipText("Zoom to level " + (zoom + 1));
zoomOutButton.setToolTipText("Zoom to level " + (zoom - 1));
zoomOutButton.setEnabled(zoom > tileController.getTileSource().getMinZoom());
zoomInButton.setEnabled(zoom < tileController.getTileSource().getMaxZoom());
}
public boolean isTileGridVisible() {
return tileGridVisible;
}
public void setTileGridVisible(boolean tileGridVisible) {
this.tileGridVisible = tileGridVisible;
repaint();
}
public boolean getMapMarkersVisible() {
return mapMarkersVisible;
}
/**
* Enables or disables painting of the {@link MapMarker}
*
* @param mapMarkersVisible
* @see #addMapMarker(MapMarker)
* @see #getMapMarkerList()
*/
public void setMapMarkerVisible(boolean mapMarkersVisible) {
this.mapMarkersVisible = mapMarkersVisible;
repaint();
}
public void setMapMarkerList(List<MapMarker> mapMarkerList) {
this.mapMarkerList = mapMarkerList;
repaint();
}
public List<MapMarker> getMapMarkerList() {
return mapMarkerList;
}
public void setMapRectangleList(List<MapRectangle> mapRectangleList) {
this.mapRectangleList = mapRectangleList;
repaint();
}
public List<MapRectangle> getMapRectangleList() {
return mapRectangleList;
}
public void addMapMarker(MapMarker marker) {
mapMarkerList.add(marker);
repaint();
}
public void removeMapMarker(MapMarker marker) {
mapMarkerList.remove(marker);
repaint();
}
public void addMapRectangle(MapRectangle rectangle) {
mapRectangleList.add(rectangle);
repaint();
}
public void removeMapRectangle(MapRectangle rectangle) {
mapRectangleList.remove(rectangle);
repaint();
}
public void setZoomContolsVisible(boolean visible) {
zoomSlider.setVisible(visible);
zoomInButton.setVisible(visible);
zoomOutButton.setVisible(visible);
}
public boolean getZoomContolsVisible() {
return zoomSlider.isVisible();
}
public void setTileSource(TileSource tileSource) {
if (tileSource.getMaxZoom() > MAX_ZOOM)
throw new RuntimeException("Maximum zoom level too high");
if (tileSource.getMinZoom() < MIN_ZOOM)
throw new RuntimeException("Minumim zoom level too low");
this.tileSource = tileSource;
tileController.setTileSource(tileSource);
zoomSlider.setMinimum(tileSource.getMinZoom());
zoomSlider.setMaximum(tileSource.getMaxZoom());
tileController.cancelOutstandingJobs();
if (zoom > tileSource.getMaxZoom()) {
setZoom(tileSource.getMaxZoom());
}
boolean requireAttr = tileSource.requiresAttribution();
if (requireAttr) {
attrImage = tileSource.getAttributionImage();
attrTermsUrl = tileSource.getTermsOfUseURL();
} else {
attrImage = null;
attrTermsUrl = null;
}
repaint();
}
public void tileLoadingFinished(Tile tile, boolean success) {
repaint();
}
public boolean isMapRectanglesVisible() {
return mapRectanglesVisible;
}
/**
* Enables or disables painting of the {@link MapRectangle}
*
* @param mapMarkersVisible
* @see #addMapRectangle(MapRectangle)
* @see #getMapRectangleList()
*/
public void setMapRectanglesVisible(boolean mapRectanglesVisible) {
this.mapRectanglesVisible = mapRectanglesVisible;
repaint();
}
/*
* (non-Javadoc)
*
* @see
* org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener#getTileCache
* ()
*/
public TileCache getTileCache() {
return tileController.getTileCache();
}
public void setTileLoader(TileLoader loader) {
tileController.setTileLoader(loader);
}
private void paintAttribution(Graphics g) {
if (!tileSource.requiresAttribution())
return;
// Draw attribution
Font font = g.getFont();
g.setFont(ATTR_LINK_FONT);
Rectangle2D termsStringBounds = g.getFontMetrics().getStringBounds("Background Terms of Use", g);
int textHeight = (int) termsStringBounds.getHeight() - 5;
int termsTextY = getHeight() - textHeight;
if (attrTermsUrl != null) {
int x = 2;
int y = getHeight() - textHeight;
g.setColor(Color.black);
g.drawString("Background Terms of Use", x + 1, y + 1);
g.setColor(Color.white);
g.drawString("Background Terms of Use", x, y);
}
// Draw attribution logo
if (attrImage != null) {
int x = 2;
int height = attrImage.getHeight(null);
int y = termsTextY - height - textHeight - 5;
g.drawImage(attrImage, x, y, null);
}
g.setFont(ATTR_FONT);
Coordinate topLeft = getPosition(0, 0);
Coordinate bottomRight = getPosition(getWidth(), getHeight());
String attributionText = tileSource.getAttributionText(zoom, topLeft, bottomRight);
if (attributionText != null) {
Rectangle2D stringBounds = g.getFontMetrics().getStringBounds(attributionText, g);
int x = getWidth() - (int) stringBounds.getWidth();
int y = getHeight() - textHeight;
g.setColor(Color.black);
g.drawString(attributionText, x + 1, y + 1);
g.setColor(Color.white);
g.drawString(attributionText, x, y);
}
g.setFont(font);
}
}