示例#1
0
  @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);
        }
      }
    }
  }