Exemplo n.º 1
0
  /**
   * Tries to get tiles of a lower or higher zoom level (one or two level difference) from cache and
   * use it as a placeholder until the tile has been loaded.
   */
  public void loadPlaceholderFromCache(TileCache cache) {
    BufferedImage tmpImage = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) tmpImage.getGraphics();
    // g.drawImage(image, 0, 0, null);
    for (int zoomDiff = 1; zoomDiff < 5; zoomDiff++) {
      // first we check if there are already the 2^x tiles
      // of a higher detail level
      int zoom_high = zoom + zoomDiff;
      if (zoomDiff < 3 && zoom_high <= JMapViewer.MAX_ZOOM) {
        int factor = 1 << zoomDiff;
        int xtile_high = xtile << zoomDiff;
        int ytile_high = ytile << zoomDiff;
        double scale = 1.0 / factor;
        g.setTransform(AffineTransform.getScaleInstance(scale, scale));
        int paintedTileCount = 0;
        for (int x = 0; x < factor; x++) {
          for (int y = 0; y < factor; y++) {
            Tile tile = cache.getTile(source, xtile_high + x, ytile_high + y, zoom_high);
            if (tile != null && tile.isLoaded()) {
              paintedTileCount++;
              tile.paint(g, x * SIZE, y * SIZE);
            }
          }
        }
        if (paintedTileCount == factor * factor) {
          image = tmpImage;
          return;
        }
      }

      int zoom_low = zoom - zoomDiff;
      if (zoom_low >= JMapViewer.MIN_ZOOM) {
        int xtile_low = xtile >> zoomDiff;
        int ytile_low = ytile >> zoomDiff;
        int factor = (1 << zoomDiff);
        double scale = factor;
        AffineTransform at = new AffineTransform();
        int translate_x = (xtile % factor) * SIZE;
        int translate_y = (ytile % factor) * SIZE;
        at.setTransform(scale, 0, 0, scale, -translate_x, -translate_y);
        g.setTransform(at);
        Tile tile = cache.getTile(source, xtile_low, ytile_low, zoom_low);
        if (tile != null && tile.isLoaded()) {
          tile.paint(g, 0, 0);
          image = tmpImage;
          return;
        }
      }
    }
  }