Changeset 9263 in osm for applications/viewer


Ignore:
Timestamp:
2008-07-23T17:32:42+02:00 (16 years ago)
Author:
stotz
Message:

Replaced: strategy algorithm for displaying tiles of other zoom levels while the correct tiles are being loaded

Location:
applications/viewer/jmapviewer
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java

    r9095 r9263  
    7979                map.addMapMarker(new MapMarkerDot(49.807, 8.644));
    8080
    81                 // map.setDisplayPositionByLatLon(49.807, 8.644, 11);
     81                map.setDisplayPositionByLatLon(49.807, 8.6, 11);
     82                //map.setTileGridVisible(true);
    8283        }
    8384
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

    r9095 r9263  
    304304                                Tile tile = getTile(tilex, tiley_tmp, zoom);
    305305                                if (tile != null) {
    306                                         if (!tile.isLoaded()) {
    307                                                 // Paint stretched preview from the next lower zoom
    308                                                 // level (if already loaded)
    309                                                 Tile parent = tile.getParentTile(tileCache);
    310                                                 if (parent != null && parent.isLoaded()) {
    311                                                         int parentx = tile.getXtile() % 2;
    312                                                         int parenty = tile.getYtile() % 2;
    313                                                         parent.parentPaint(g, x, y, parentx, parenty);
    314                                                 } else
    315                                                         tile.paint(g, x, y);
    316                                         } else
    317                                                 tile.paint(g, x, y);
     306                                        tile.paint(g, x, y);
     307                                        if (tileGridVisible)
     308                                                g.drawRect(x, y, Tile.WIDTH, Tile.HEIGHT);
    318309                                }
    319                                 if (tileGridVisible)
    320                                         g.drawRect(x, y, Tile.WIDTH, Tile.HEIGHT);
    321310                                tiley_tmp++;
    322311                        }
    323312                        tilex++;
    324313                }
     314                g.fillOval(getWidth()/2 - 5, getHeight()/2 - 5, 10, 10);
     315                g.drawString("Test", 50, 10);
    325316                if (!mapMarkersVisible || mapMarkerList == null)
    326317                        return;
     
    400391                        tile = new Tile(tilex, tiley, zoom, loadingImage);
    401392                        tileCache.addTile(tile);
     393                        tile.loadPlaceholderFromCache(tileCache);
    402394                }
    403395                if (!tile.isLoaded()) {
     
    409401                                                return;
    410402                                        try {
    411                                                 // Thread.sleep(500);
     403                                                Thread.sleep(500);
    412404                                                tile.loadTileImage();
    413405                                                repaint();
     
    470462        }
    471463
     464        public TileCache getTileCache() {
     465                return tileCache;
     466        }
     467
    472468}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Tile.java

    r9095 r9263  
    44
    55import java.awt.Graphics;
     6import java.awt.Graphics2D;
     7import java.awt.geom.AffineTransform;
    68import java.awt.image.BufferedImage;
    79import java.io.DataInputStream;
     
    5355
    5456        /**
     57         * Tries to get tiles of a lower or higher zoom level (one or two level
     58         * difference) from cache and use it as a placeholder until the tile has
     59         * been loaded.
     60         */
     61        public void loadPlaceholderFromCache(TileCache cache) {
     62                BufferedImage tmpImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
     63                Graphics2D g = (Graphics2D) tmpImage.getGraphics();
     64                // g.drawImage(image, 0, 0, null);
     65                for (int zoomDiff = 1; zoomDiff < 3; zoomDiff++) {
     66                        // first we check if there are already the 2^x tiles
     67                        // of a higher detail level
     68                        int zoom_high = zoom + zoomDiff;
     69                        if (zoom_high <= JMapViewer.MAX_ZOOM) {
     70                                int factor = 1 << zoomDiff;
     71                                int xtile_high = xtile << zoomDiff;
     72                                int ytile_high = ytile << zoomDiff;
     73                                double scale = 1.0 / factor;
     74                                g.setTransform(AffineTransform.getScaleInstance(scale, scale));
     75                                int paintedTileCount = 0;
     76                                for (int x = 0; x < factor; x++) {
     77                                        for (int y = 0; y < factor; y++) {
     78                                                Tile tile = cache.getTile(xtile_high + x, ytile_high + y, zoom_high);
     79                                                if (tile != null && tile.isLoaded()) {
     80                                                        paintedTileCount++;
     81                                                        tile.paint(g, x * WIDTH, y * HEIGHT);
     82                                                }
     83                                        }
     84                                }
     85                                if (paintedTileCount == factor * factor) {
     86                                        image = tmpImage;
     87                                        return;
     88                                }
     89                        }
     90
     91                        int zoom_low = zoom - zoomDiff;
     92                        if (zoom_low >= JMapViewer.MIN_ZOOM) {
     93                                int xtile_low = xtile >> zoomDiff;
     94                                int ytile_low = ytile >> zoomDiff;
     95                                int factor = (1 << zoomDiff);
     96                                double scale = (double) factor;
     97                                AffineTransform at = new AffineTransform();
     98                                int translate_x = (xtile % factor) * WIDTH;
     99                                int translate_y = (ytile % factor) * HEIGHT;
     100                                at.setTransform(scale, 0, 0, scale, -translate_x, -translate_y);
     101                                g.setTransform(at);
     102                                Tile tile = cache.getTile(xtile_low, ytile_low, zoom_low);
     103                                if (tile != null && tile.isLoaded()) {
     104                                        tile.paint(g, 0, 0);
     105                                        image = tmpImage;
     106                                        return;
     107                                }
     108                        }
     109                }
     110        }
     111
     112        /**
    55113         * @return tile number on the x axis of this tile
    56114         */
     
    88146        }
    89147
    90         /**
    91          * Retrieves the "parent tile" from the specified tile cache. A parent tile
    92          * is the tile of a lower zoom level (coarser resolution) at the same
    93          * position of the map. Parent tiles are used for a preview until the tile
    94          * has been loaded.
    95          *
    96          * @param tileCache
    97          * @return
    98          */
    99         public Tile getParentTile(TileCache tileCache) {
    100                 if (zoom < 1)
    101                         return null;
    102                 return tileCache.getTile(xtile / 2, ytile / 2, zoom - 1);
    103         }
    104 
    105148        public synchronized void loadTileImage() throws IOException {
    106149                if (loaded)
     
    132175                if (image == null)
    133176                        return;
    134                 /*
    135                  * if (image.getHeight() < OSMMap.TILE_HEIGHT || image.getWidth() <
    136                  * OSMMap.TILE_WIDTH) { x += (OSMMap.TILE_WIDTH - image.getWidth()) / 2;
    137                  * y += (OSMMap.TILE_HEIGHT - image.getHeight()) / 2; }
    138                  */
    139177                g.drawImage(image, x, y, null);
    140         }
    141 
    142         public void paint(Graphics g, int x, int y, int stretch) {
    143                 if (image == null)
    144                         return;
    145                 int tx = x * stretch;
    146                 int ty = y = stretch;
    147                 g.drawImage(image, x, y, tx, ty, 0, 0, image.getWidth(), image.getHeight(), null);
    148         }
    149 
    150         /**
    151          * Paints one-fourth of the tile image {@link Graphics} <code>g</code> at
    152          * the position <code>x</code>/<code>y</code>.
    153          *
    154          * @param g
    155          * @param x
    156          * @param y
    157          * @param partx
    158          *            (0 or 1) selects if the left or right part of tile should be
    159          *            painted
    160          * @param party
    161          *            (0 or 1) selects if the upper or lower part of tile should be
    162          *            painted
    163          */
    164         public void parentPaint(Graphics g, int x, int y, int partx, int party) {
    165                 int sx = 0;
    166                 int sy = 0;
    167                 if (partx == 1)
    168                         sx = WIDTH_HALF;
    169                 if (party == 1)
    170                         sy = HEIGHT_HALF;
    171                 g.drawImage(image, x, y, x + WIDTH, y + HEIGHT, sx, sy, sx + WIDTH_HALF, sy + HEIGHT_HALF,
    172                                 null);
    173178        }
    174179
Note: See TracChangeset for help on using the changeset viewer.