コード例 #1
0
ファイル: GMap.java プロジェクト: joshuasm/gmap-viewer
  /** Main method used to build the image based on a large number of tiles. */
  public void buildImage(
      BufferedImage toReturn,
      int x,
      int y,
      int w,
      int h,
      int zoom,
      int cachedZoom,
      GMapListener listener) {
    // validate
    // if(x < 0 || y < 0 || w <= 0 || h <= 0) return getDefaultImage(w,h);

    // if(toReturn != null) Graphics2D g = toReturn.createGraphics();

    // find index of point
    int xIndex = x / GDataSource.sourceSize.width;
    int yIndex = y / GDataSource.sourceSize.height;

    // find coord of our starting point
    int xCoord = x % GDataSource.sourceSize.width;
    int yCoord = y % GDataSource.sourceSize.height;

    // Checks for invalid xCoord and yCoord
    if (xCoord < 0) {
      xCoord = 0;
    }
    if (yCoord < 0) {
      yCoord = 0;
    }

    // load this index
    BufferedImage image = getIndexedImage(xIndex, yIndex, zoom, cachedZoom, listener);

    // get info about the image
    // Dimension imageSize = new Dimension(image.getWidth(),image.getHeight());

    // Holds number of row and column images needed
    int rowImages;
    int colImages;

    // find the width of what we CAN paint
    int paintWidth = GDataSource.sourceSize.width - xCoord;
    int paintHeight = GDataSource.sourceSize.height - yCoord;

    // Calculate number of row images
    if ((h - paintHeight) % 256 == 0) {
      rowImages = 1 + (h - paintHeight) / 256;
    } else {
      rowImages = 2 + (h - paintHeight) / 256;
    }

    // Calculate number of column images
    if ((w - paintWidth) % 256 == 0) {
      colImages = 1 + (w - paintWidth) / 256;
    } else {
      colImages = 2 + (w - paintWidth) / 256;
    }

    // Overal Image coordinates
    int xImage = 0;
    int yImage = 0;

    // DEBUG
    // System.out.println(x + " " + y + " " + w + " " + h + " " + rowImages + " " + colImages);
    // System.out.println();

    // set listener
    if (listener != null) listener.updateGMapTaskSize(rowImages * colImages);

    // a counter for the listener
    int completed = 0;

    // Iteratively loops through all CACHED images and paints them
    for (int row = 0; row < rowImages; row++) {
      for (int col = 0; col < colImages; col++) {
        int thisXIndex = x / GDataSource.sourceSize.width + col;
        int thisYIndex = y / GDataSource.sourceSize.height + row;

        getSpecificImage(x, y, w, h, col, row, toReturn, zoom, cachedZoom, listener, true);
        if (getGDataSource().isCached(thisXIndex, thisYIndex, zoom)) {
          if (listener != null) {
            listener.updateGMapCompleted(completed);
            completed++;
          }
        }
        if (listener.asynchronousGMapStopFlag()) return;
      }
    }

    // do the UNCACHED IMAGES NEXT
    for (int row = 0; row < rowImages; row++) {
      for (int col = 0; col < colImages; col++) {
        int thisXIndex = x / GDataSource.sourceSize.width + col;
        int thisYIndex = y / GDataSource.sourceSize.height + row;

        if (!getGDataSource().isCached(thisXIndex, thisYIndex, zoom)) {
          getSpecificImage(x, y, w, h, col, row, toReturn, zoom, cachedZoom, listener, false);
          if (listener != null) {
            listener.updateGMapCompleted(completed);
            completed++;
            if (listener.asynchronousGMapStopFlag()) return;
          }
        }
      }
    }

    // the dispatch to GDraw object
    gDraw.draw(toReturn, new GPhysicalPoint(x, y, zoom), zoom);
  }