if (tileset != null) { int twidth = tileset.getTileWidth() + 1; int theight = tileset.getTileHeight() + 1; int tx = x / twidth; int ty = y / theight; int tilesPerRow = (getWidth() - 1) / twidth; int tileId = ty * tilesPerRow + tx;      while ((ret = tileset.getTile(tileId++)) == null) { if (tileId > tileset.getMaxTileId()) break; } }  return ret;
if(x>tilesPerRow*twidth) x = tilesPerRow*twidth-1;  int tileAt = (y / theight) * tilesPerRow + x/twidth;  return (Tile) tilesetMap.get(tileAt);
    public Tile getTileAtPoint(int x, int y) {        Tile ret = null;        if (tileset != null) {            int twidth = tileset.getTileWidth() + 1;            int theight = tileset.getTileHeight() + 1;            int tx = x / twidth;            int ty = y / theight;            int tilesPerRow = (getWidth() - 1) / twidth;            int tileId = ty * tilesPerRow + tx;            // Now that we're in the right "spot", find the next valid tile            // todo: Assumes to gap is so big that a tile can be found between the            // todo: right spot and the tile that was actually clicked, actually            // todo: I'm not sure whether this works at all. - Bjorn            while ((ret = tileset.getTile(tileId++)) == null) {                if (tileId > tileset.getMaxTileId()) break;            }        }        return ret;    }