//Step 1 First calculate the resolution value according to the tile size and the resolution level Dimension tileSize = rnd.getTileSize(); int levels = rnd.getResolutionLevels(); int powerX = (int) (Math.log(tileSize.width)/Math.log(2)); int powerY = (int) (Math.log(tileSize.height)/Math.log(2)); int index = 0; int vx = 0, vy = 0; for (int i = levels-1; i >= 0; i--) { vx = powerX-index; vy = powerY-index; resolutionMap.put(i, new ResolutionLevel(i, vx, vy)); index++; } //Step 2 // -Initializes the tiles depending on the selected resolution, // - determine the number of rows and columns // - Size of the image. Dimension d = rnd.getTileSize(); int w = d.width; int h = d.height; int edgeWidth = w; int edgeHeight = h; ResolutionLevel rl = resolutionMap.get(getSelectedResolutionLevel()); int px = rl.getPowerAlongX(); int py = rl.getPowerAlongY(); rl = resolutionMap.get(getResolutionLevels()-1); int mx = rl.getPowerAlongX(); int my = rl.getPowerAlongY(); int size = (int) (rnd.getSizeX()/Math.pow(2, mx-px)); edgeWidth = w; int n = size/w; tiledImageSizeX = n*w; if (n*w < size) { edgeWidth = size-n*w; tiledImageSizeX += edgeWidth; n++; } numberOfColumns = n; size = (int) (rnd.getSizeY()/Math.pow(2, my-py)); edgeHeight = h; n = size/h; tiledImageSizeY = n*h; if (n*h < size) { edgeHeight = size-n*h; tiledImageSizeY += edgeHeight; n++; } numberOfRows = n; int index = 0; Tile tile; Region region; int x = 0; int y = 0; int ww; int hh; if (numberOfColumns <= 0) numberOfColumns = 1; if (numberOfColumns <= 0) numberOfColumns = 1; for (int i = 0; i < numberOfRows; i++) { if (i == (numberOfRows-1)) hh = edgeHeight; else hh = h; for (int j = 0; j < numberOfColumns; j++) { if (j == (numberOfColumns-1)) ww = edgeWidth; else ww = w; index = i*numberOfColumns+j; tile = new Tile(index, i, j); region = new Region(x, y, ww, hh); tile.setRegion(region); x += d.width; tiles.put(index, tile); } y += d.height; x = 0; } //Step 3 //Determine the tiles corresponding to the viewport. Rectangle selectedView; Dimension d = rnd.getTileSize(); int width = d.width; int height = d.height; int cs = selectedView.x/width; int rs = selectedView.y/height; int ih = selectedView.width/width; int iv = selectedView.height/height; int columns int index; Tile t; int h = rs+iv+1; int w = cs+ih+1; cs = cs-1; rs = rs-1; if (cs < 0) cs = 0; if (rs < 0) rs = 0; for (int i = rs; i <= h; i++) { for (int j = cs; j <= w; j++) { index = i*columns+j; t = tiles.get(index); if (t != null) { //load the tile using your code since the tile host the correct region } } } //Resolution level is an helper class with 3 fields class Resolution { /** The power of 2 along the X-axis.*/ int powerAlongX; /** The power of 2 along the Y-axis.*/ int powerAlongY; /** The resolution level.*/ private int level; } //Class Tile has the following field class Tile { /** The row index. */ private int row; /** The column index. */ private int column; /** The image to display if loaded. */ private Object image; /** The index associated to the tile.*/ private int index; /** The region covered by the tile.*/ private Region region; } //Region class Region { /** The x-coordinate of the top-left corner of the region. */ private int xLocation; /** The y-coordinate of the top-left corner of the region. */ private int yLocation; /** The width of the region. */ private int width; /** The height of the region. */ private int height; }