Search in sources :

Example 1 with MapRectangle

use of org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle in project Course_Generator by patrovite.

the class JMapViewer method paintComponent.

@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();
    // calculate the length of the grid (number of squares per edge)
    int gridLength = 1 << zoom;
    // 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;
                    if (scrollWrapEnabled) {
                        // in case tilex is out of bounds, grab the tile to use for wrapping
                        int tilexWrap = (((tilex % gridLength) + gridLength) % gridLength);
                        tile = tileController.getTile(tilexWrap, tiley, zoom);
                    } else {
                        tile = tileController.getTile(tilex, tiley, zoom);
                    }
                    if (tile != null) {
                        tile.paint(g, posx, posy);
                        if (tileGridVisible) {
                            g.drawRect(posx, posy, tilesize, tilesize);
                        }
                    }
                    painted = true;
                }
                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;
    if (scrollWrapEnabled) {
        g.drawLine(0, h2 - center.y, getWidth(), h2 - center.y);
        g.drawLine(0, h2 - center.y + mapSize, getWidth(), h2 - center.y + mapSize);
    } else {
        g.drawRect(w2 - center.x, h2 - center.y, mapSize, mapSize);
    }
    // keep x-coordinates from growing without bound if scroll-wrap is enabled
    if (scrollWrapEnabled) {
        center.x = center.x % mapSize;
    }
    if (mapPolygonsVisible && mapPolygonList != null) {
        for (MapPolygon polygon : mapPolygonList) {
            if (polygon.isVisible())
                paintPolygon(g, polygon);
        }
    }
    if (mapRectanglesVisible && mapRectangleList != null) {
        for (MapRectangle rectangle : mapRectangleList) {
            if (rectangle.isVisible())
                paintRectangle(g, rectangle);
        }
    }
    if (mapMarkersVisible && mapMarkerList != null) {
        for (MapMarker marker : mapMarkerList) {
            if (marker.isVisible())
                paintMarker(g, marker);
        }
    }
    attribution.paintAttribution(g, getWidth(), getHeight(), getPosition(0, 0), getPosition(getWidth(), getHeight()), zoom, this);
}
Also used : MapPolygon(org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon) MapMarker(org.openstreetmap.gui.jmapviewer.interfaces.MapMarker) Point(java.awt.Point) MapRectangle(org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle) Point(java.awt.Point)

Example 2 with MapRectangle

use of org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle in project Course_Generator by patrovite.

the class JMapViewer method setDisplayToFitMapElements.

/**
 * Sets the displayed map pane and zoom level so that all chosen map elements are
 * visible.
 */
public void setDisplayToFitMapElements(boolean markers, boolean rectangles, boolean polygons) {
    int nbElemToCheck = 0;
    if (markers && mapMarkerList != null)
        nbElemToCheck += mapMarkerList.size();
    if (rectangles && mapRectangleList != null)
        nbElemToCheck += mapRectangleList.size();
    if (polygons && mapPolygonList != null)
        nbElemToCheck += mapPolygonList.size();
    if (nbElemToCheck == 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();
    if (markers) {
        for (MapMarker marker : mapMarkerList) {
            if (marker.isVisible()) {
                int x = tileSource.LonToX(marker.getLon(), mapZoomMax);
                int y = tileSource.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);
            }
        }
    }
    if (rectangles) {
        for (MapRectangle rectangle : mapRectangleList) {
            if (rectangle.isVisible()) {
                x_max = Math.max(x_max, tileSource.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax));
                y_max = Math.max(y_max, tileSource.LatToY(rectangle.getTopLeft().getLat(), mapZoomMax));
                x_min = Math.min(x_min, tileSource.LonToX(rectangle.getTopLeft().getLon(), mapZoomMax));
                y_min = Math.min(y_min, tileSource.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax));
            }
        }
    }
    if (polygons) {
        for (MapPolygon polygon : mapPolygonList) {
            if (polygon.isVisible()) {
                for (ICoordinate c : polygon.getPoints()) {
                    int x = tileSource.LonToX(c.getLon(), mapZoomMax);
                    int y = tileSource.LatToY(c.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());
    int newZoom = mapZoomMax;
    int x = x_max - x_min;
    int y = y_max - y_min;
    while (x > width || y > height) {
        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);
}
Also used : MapPolygon(org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon) MapMarker(org.openstreetmap.gui.jmapviewer.interfaces.MapMarker) ICoordinate(org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate) MapRectangle(org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle) Point(java.awt.Point)

Aggregations

Point (java.awt.Point)2 MapMarker (org.openstreetmap.gui.jmapviewer.interfaces.MapMarker)2 MapPolygon (org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon)2 MapRectangle (org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle)2 ICoordinate (org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate)1