I'm trying to write an implementation of MapPanel by Stepan Rutz in JavaFX.
Stepan's map allows the user to move around the map while the map tiles haven't been loaded and I'm trying to get that same effect.To do it I'm using Task to run the painting of the tiles in the background whenever is required (pretty often).
The thing is that whenever a new tile that hasn't been painted before needs to be added to the map the program creates a new worker to do the task of creating the tile (a petition to my server where they are created).
I think that I need to restrain somehow the number of tasks that are being created but I don't know how to accomplish it while having the sensation of smoothness when moving the map.
Here is the code that creates the tiles:
1.First a for loop for every tile that is needed:
public void paintMap(Point mapPosition, Point scalePosition) { double width = this.getWidth(); double height = this.getHeight(); int x0 = (int) Math.floor((this.mapPosition.getX()) / TILE_SIZE); int y0 = (int) Math.floor((this.mapPosition.getY()) / TILE_SIZE); int x1 = (int) Math.ceil((mapPosition.getX() + width) / TILE_SIZE); int y1 = (int) Math.ceil((mapPosition.getY() + height) / TILE_SIZE); int dy = y0 * TILE_SIZE - (int) this.mapPosition.getY(); for(int y = y0; y < y1; ++y) { int dx = x0 * TILE_SIZE - (int) this.mapPosition.getX(); for(int x = x0; x < x1; ++x) { paintTile(dx, dy, x, y); dx += TILE_SIZE; } dy += TILE_SIZE; } }
- The function that paints the tile:
- The function that creates the task:
- And the Task class:
Источник: https://stackoverflow.com/questions/780 ... he-program