package Maze; import java.awt.Image; import java.io.File; import java.util.Scanner; import javax.swing.ImageIcon; public class map { private Scanner m; private String map[] = new String[14]; //holds one row of map information at a time private Image grass, wall ; int mapnum = 1; public map() //constructor { ImageIcon img = new ImageIcon("C://ji//background//grass.png"); //loads image file for grass grass = img.getImage(); //sets grass Image = to grass.png img = new ImageIcon("C://ji//background//wall.png"); wall = img.getImage(); openFile(mapnum); readFile(); closeFile(); } public Image getGrass() { return grass; //called when map is drawn so it knows which image to show } public Image getWall() { return wall; } public map upLevel(map m){ m.mapnum +=1; openFile(m.mapnum); readFile(); closeFile(); return m; } public int downLevel(map m){ m.mapnum -=1; openFile(m.mapnum); readFile(); closeFile(); return m.mapnum; } public String getMap(int x, int y) { // y = row, x = number of characters in String index = map[y].substring(x, x+1); //returns either a g or w return index; } public void openFile(int mapnum) { System.out.println("Load map Level "+ mapnum); try //try...catch lets the program handle errors without crashing { if (mapnum==1) m = new Scanner(new File("C://ji//maps//map.txt")); //reads map text file if (mapnum==2) m = new Scanner(new File("C://ji//maps//map2.txt")); //reads map text file if (mapnum==3) m = new Scanner(new File("C://ji//maps//map3.txt")); //reads map text file } catch(Exception e) { System.out.println("Error loading map"); } } public void readFile() { while(m.hasNext()) //loops while there is more to read { for(int i = 0; i < 14; i++) //one row at a time { map[i] = m.next(); //loads the text (g or w) into the map array } } } public void closeFile() { m.close(); //close the file when we are done with it. } } //end map class