DiagramShape shape = new DiagramShape();
shape.setClosed(true);
if(grid.containsAtLeastOneDashedLine(cells)) shape.setStrokeDashed(true);
TextGrid workGrid = new TextGrid(grid.getWidth(), grid.getHeight());
grid.copyCellsTo(cells, workGrid);
if (DEBUG){
System.out.println("Making closed shape from buffer:");
workGrid.printDebug();
}
TextGrid.Cell start = (TextGrid.Cell) cells.getFirst();
if(workGrid.isCorner(start)) shape.addToPoints(makePointForCell(start, workGrid, cellWidth, cellHeight, allRound));
TextGrid.Cell previous = start;
TextGrid.Cell cell = null;
CellSet nextCells = workGrid.followCell(previous);
if(nextCells.size() == 0) return null;
cell = (TextGrid.Cell) nextCells.getFirst();
if(workGrid.isCorner(cell)) shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound));
while(!cell.equals(start)){
nextCells = workGrid.followCell(cell, previous);
if(nextCells.size() == 1) {
previous = cell;
cell = (TextGrid.Cell) nextCells.getFirst();
if(!cell.equals(start) && workGrid.isCorner(cell))
shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound));
} else if(nextCells.size() > 1) {
return null;
}
}