public static void main(String[] args) { // read arguments and respond correctly File in; File out; String tileDirectory; if (args.length == 0 || args.length > 2) { in = null; out = null; tileDirectory = ""; System.err.println("Incorrect number of arguments. Required: Filename (tileset path)"); System.exit(0); } else if (args.length == 1) { // load old file in = new File(args[0]); out = in; Scanner s = null; try { s = new Scanner(in); } catch (FileNotFoundException e) { System.out.println("Could not find input file."); System.exit(0); } tileDirectory = s.nextLine(); } else { in = null; out = new File(args[0]); tileDirectory = args[1]; try { File f = new File(tileDirectory); if (!f.isDirectory()) { throw new IOException("Tileset does not exist"); } } catch (IOException e) { System.err.println(e); System.out.println("This error is likely thanks to an invalid tileset path"); System.exit(0); } } MapBuilder test = new MapBuilder("Map Editor", in, out, tileDirectory); // Build GUI SwingUtilities.invokeLater( new Runnable() { public void run() { test.CreateAndDisplayGUI(); } }); }
public Map loadMap(File input) throws FileNotFoundException { Scanner s = new Scanner(input); tileDir = s.nextLine(); int width = s.nextInt(); int height = s.nextInt(); Map toReturn = new Map(width, height, tileDir); s.nextLine(); // eat up rest of line. for (int y = 0; y < height; y++) { String line = s.nextLine(); Scanner lineReader = new Scanner(line); List<Tile> tList = new ArrayList<Tile>(); for (int x = 0; x < width; x++) { String[] values = lineReader.next().split("/"); String name = values[0]; int[] picLocation = new int[2]; for (int i = 0; i < picLocation.length; i++) { picLocation[i] = Integer.parseInt(values[1].split("_")[i]); } ImageIcon img = null; try { img = new ImageIcon(getTile(tileDir, picLocation[0], picLocation[1], DISPLAY_SCALE)); } catch (IOException e) { System.out.println("Could not find image."); img = new ImageIcon( new BufferedImage( TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE, BufferedImage.TYPE_INT_RGB)); } int avoid = Integer.parseInt(values[2]); int def = Integer.parseInt(values[3]); String[] movString = values[4].split(","); int[] moveCost = new int[movString.length]; for (int i = 0; i < moveCost.length; i++) { moveCost[i] = Integer.parseInt(movString[i]); } String special = values[5]; Tile t = new Tile( img, name, avoid, def, moveCost, special, true, "" + picLocation[0] + "_" + picLocation[1]); tList.add(t); t.setMaximumSize(new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.setPreferredSize(new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.addMouseListener(new MapButtonListener()); } toReturn.addRow(tList); } return toReturn; }