document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1.  
  2. import java.awt.Color;
  3. import java.awt.event.WindowAdapter;
  4. import java.awt.event.WindowEvent;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.io.PrintWriter;
  9. import java.util.ArrayList;
  10.  
  11. import javax.swing.JFrame;
  12. import javax.swing.JOptionPane;
  13.  
  14. public class MazeMapMaker extends JFrame{
  15.     static int rows = 30;
  16.     static int columns = 30;
  17.     int panelSize = 20;
  18.     static int map[][] = new int[columns][rows];
  19.     ArrayList<String> mapList = new ArrayList<String>();
  20.     int level = 0;
  21.     boolean levelsExistAlready = false;
  22.    
  23.     public MazeMapMaker(){
  24.         getMapList();
  25.         getLevelChoice();
  26.         if(level != -1){
  27.             loadMap();
  28.             this.setResizable(false);
  29.             this.setSize((columns*panelSize)+50, (rows*panelSize)+70);
  30.             this.setTitle("Maze Map Maker");
  31.             this.setLayout(null);
  32.            
  33.             this.addWindowListener(new WindowAdapter(){
  34.                 public void windowClosing(WindowEvent e) {
  35.                     saveMap();
  36.                     new MainMenu();
  37.                 }
  38.             });
  39.            
  40.             this.setLocationRelativeTo(null);
  41.            
  42.             for(int y = 0; y < columns; y++){
  43.                 for(int x = 0; x < rows; x++){
  44.                     MapMakerTile tile = new MapMakerTile(x, y);
  45.                     tile.setSize(panelSize-1, panelSize-1);
  46.                     tile.setLocation((x*panelSize)+23, (y*panelSize)+25);
  47.                     if(map[x][y] == 0){
  48.                         tile.setBackground(Color.PINK);
  49.                     }else{
  50.                         tile.setBackground(Color.WHITE);
  51.                     }
  52.                    
  53.                     tile.setVisible(true);
  54.                    
  55.                     this.add(tile);
  56.                 }
  57.             }
  58.             this.setVisible(true);
  59.         }else{
  60.             new MainMenu();
  61.         }
  62.     }
  63.    
  64.     public void getMapList(){
  65.         for(int i = 0; i < 99; i++){
  66.             File map = new File("./Level "+i+".map");
  67.             if(map.exists()){
  68.                 System.out.println("Level "+i+" exists");
  69.                 mapList.add("Level "+i+".map");
  70.                 levelsExistAlready = true;
  71.             }
  72.         }
  73.     }
  74.    
  75.     public void getLevelChoice(){
  76.         if(levelsExistAlready){
  77.             String maps[] = new String[99];
  78.             mapList.toArray(maps);
  79.             maps[mapList.size()] = "New level";
  80.             String choice = (String)JOptionPane.showInputDialog(null, "Which level would you like to play?", "Maze Level Selector", JOptionPane.QUESTION_MESSAGE, null, maps, maps[0]);
  81.             System.out.println(choice);
  82.             if(choice != null && !choice.equals("New level")){
  83.                 level = Integer.parseInt((choice.replace("Level ", "").replace(".map", "")));
  84.             }else if(choice == null){
  85.                 level = -1;
  86.             }else{
  87.                 level = mapList.size();
  88.             }
  89.         }
  90.     }
  91.    
  92.     public void saveMap(){
  93.         try{
  94.         PrintWriter writer = new PrintWriter("Level "+level+".map", "UTF-8");
  95.         for(int y = 0; y < columns; y++){
  96.             for(int x = 0; x < rows; x++){
  97.                 writer.print(map[x][y]);
  98.             }
  99.             writer.print("\\r\\n");
  100.         }
  101.         writer.close();
  102.         }catch(Exception e){
  103.             e.printStackTrace();
  104.         }
  105.     }
  106.    
  107.     public void loadMap(){
  108.         try{
  109.             BufferedReader br = new BufferedReader(new FileReader("Level "+level+".map"));
  110.             StringBuilder sb = new StringBuilder();
  111.             String line = br.readLine();
  112.  
  113.             while (line != null) {
  114.                 sb.append(line);
  115.                 sb.append(System.lineSeparator());
  116.                 line = br.readLine();
  117.             }
  118.             String mapStr = sb.toString();
  119.            
  120.             int counter = 0;
  121.             for(int y = 0; y < columns; y++){
  122.                 for(int x = 0; x < rows; x++){
  123.                     String mapChar = mapStr.substring(counter, counter+1);
  124.                     if(!mapChar.equals("\\r\\n") && !mapChar.equals("\\n")&& !mapChar.equals("\\r")){//If it\'s a number
  125.                         //System.out.print(mapChar);
  126.                         map[x][y] = Integer.parseInt(mapChar);
  127.                     }else{//If it is a line break
  128.                         x--;
  129.                         //System.out.print(mapChar);
  130.                     }
  131.                     counter++;
  132.                 }
  133.             }
  134.         }catch(Exception e){
  135.             System.out.println("Unable to load existing map(if exists), creating new map.");
  136.             for(int y = 0; y < columns; y++){
  137.                 for(int x = 0; x < rows; x++){
  138.                     map[x][y] = 0;
  139.                 }
  140.             }
  141.         }
  142.     }
  143. }
  144.  
');