/** * 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()); fireMapViewChanged(); }
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 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"); tileController.setTileSource(tileSource); zoomSlider.setMinimum(tileSource.getMinZoom()); zoomSlider.setMaximum(tileSource.getMaxZoom()); tileController.cancelOutstandingJobs(); if (zoom > tileSource.getMaxZoom()) setZoom(tileSource.getMaxZoom()); repaint(); }
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); }
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 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); }
public void setTileLoader(TileLoader loader) { tileController.setTileLoader(loader); }
/* (non-Javadoc) * @see org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener#getTileCache() */ public TileCache getTileCache() { return tileController.getTileCache(); }
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); int iMove = 0; int tilex = center.x / Tile.SIZE; int tiley = center.y / Tile.SIZE; int off_x = (center.x % Tile.SIZE); int off_y = (center.y % Tile.SIZE); 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 = Tile.SIZE - off_x; int diff_top = off_y; int diff_bottom = Tile.SIZE - 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 = -Tile.SIZE; int y_min = -Tile.SIZE; 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, Tile.SIZE, Tile.SIZE); } } Point p = move[iMove]; posx += p.x * Tile.SIZE; posy += p.y * Tile.SIZE; tilex += p.x; tiley += p.y; } iMove = (iMove + 1) % move.length; } } for (OverlayPainter op : overlayPainterList) { op.paintOverlay(g); } // outer border of the map int mapSize = Tile.SIZE << 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) { Point p = getMapPosition(marker.getLat(), marker.getLon()); if (p != null) { marker.paint(g, p); } } } }