Rectangle affectedBounds = new Rectangle(x, y, 1, 1);
int minX = x, minY = y; int maxX = x, maxY = y;
    private void pour(MapLayer layer, int x, int y,            Tile newTile, Tile oldTile) {        if (newTile == oldTile) return;        Rectangle affectedBounds = new Rectangle(x, y, 1, 1);        Stack stack = new Stack();        MapLayer before = new MapLayer(layer);        before.setId(layer.getId());        MapLayer after;        stack.push(new Point(x, y));        while (!stack.empty()) {            // Remove the next tile from the stack            Point p = (Point)stack.pop();            // If the tile it meets the requirements, set it and push its            // neighbouring tiles on the stack.            if (currentMap.inBounds(p.x, p.y) &&                    layer.getTileAt(p.x, p.y) == oldTile)            {                layer.setTileAt(p.x, p.y, newTile);                stack.push(new Point(p.x, p.y - 1));                stack.push(new Point(p.x, p.y + 1));                stack.push(new Point(p.x + 1, p.y));                stack.push(new Point(p.x - 1, p.y));                if (!affectedBounds.contains(p.x, p.y)) {                    if (p.x < affectedBounds.x) {                        affectedBounds.width += affectedBounds.x - p.x;                        affectedBounds.x = p.x;                    } else if (p.x > affectedBounds.x + affectedBounds.width) {                        affectedBounds.width = p.x - affectedBounds.x;                    }                    if (p.y < affectedBounds.y) {                        affectedBounds.height += affectedBounds.y-p.y;                        affectedBounds.y = p.y;                    } else if (p.y > affectedBounds.y + affectedBounds.height) {                        affectedBounds.height = p.y - affectedBounds.y;                    }                }            }        }        after = new MapLayer(affectedBounds);        after.copyFrom(layer);        after.setId(layer.getId());        MapLayerEdit mle = new MapLayerEdit(currentMap, before, after);        mle.setPresentationName("Fill");        undoSupport.postEdit(mle);    }