/** Write shape to disk. */ public void saveShape() { int colEnd = 0; int rowEnd = 0; Dimension dim = grid.getDimension(); int colStart = dim.width; int rowStart = dim.height; String lineSeperator = System.getProperty("line.separator"); StringBuffer text = new StringBuffer( "!Generator: Game of Life (http://www.bitstorm.org/gameoflife/)" + lineSeperator + "!Variation: 23/3" + lineSeperator + "!" + lineSeperator); for (int row = 0; row < dim.height; row++) { for (int col = 0; col < dim.width; col++) { if (grid.getCell(col, row)) { if (row < rowStart) rowStart = row; if (col < colStart) colStart = col; if (row > rowEnd) rowEnd = row; if (col > colEnd) colEnd = col; } } } for (int row = rowStart; row <= rowEnd; row++) { for (int col = colStart; col <= colEnd; col++) { text.append(grid.getCell(col, row) ? 'O' : '-'); } text.append(lineSeperator); } EasyFile file; try { file = new EasyFile(appletFrame, "Save Game of Life file"); file.setFileName(filename); file.setFileExtension(FILE_EXTENSION); file.writeText(text.toString()); } catch (FileNotFoundException e) { new AlertBox(appletFrame, "File error", "Couldn't open this file.\n" + e.getMessage()); } catch (IOException e) { new AlertBox(appletFrame, "File error", "Couldn't write to this file.\n" + e.getMessage()); } }
/** * Use EasyFile object to read GameOfLife-file from. * * @param file EasyFile-object * @throws IOException * @see org.bitstorm.util.EasyFile */ public void openShape(EasyFile file) throws IOException { Shape shape = makeShape(file.getFileName(), file.readText()); setShape(shape); }