Maze.java
import javax.swing.*; public class Maze { public static void main(String[] args) { JFrame mainframe = new JFrame(); mainframe.add(new Board()); mainframe.setTitle("Noah's Game"); mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainframe.setSize(512,512); mainframe.setLocationRelativeTo(null); mainframe.setVisible(true); } }
Board.java
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.event.*; import javax.swing.*; import java.*; public class Board extends JPanel implements ActionListener { private Map m; private Timer timer; private Player p; private String Message = "Winner!"; private boolean Win = false; private Font font = new Font("Serif", Font.BOLD,48); public Board() { timer = new Timer(25, this); timer.start(); m = new Map(); p = new Player(); addKeyListener(new Al()); setFocusable(true); } public void actionPerformed(ActionEvent e) { repaint(); if(m.getMap(p.getTileX(), p.getTileY()).equals("f")) { Message = "Winner!"; Win = true; } } public void paint(Graphics g) { super.paint(g); if(!Win){ for (int y = 0; y < 16; y++) { for (int x = 0; x < 16; x++) { if (m.getMap(x, y).equals("f")) { g.drawImage(m.getFinish(), x * 32, y * 32, null); } if (m.getMap(x, y).equals("g")) { g.drawImage(m.getDirt(), x * 32, y * 32, null); } if (m.getMap(x, y).equals("w")) { g.drawImage(m.getWall(), x * 32, y * 32, null); } } } g.drawImage(p.getPlayer(), p.getTileX() * 32, p.getTileY() * 32, null); } if(Win){ g.setColor(Color.MAGENTA); g.setFont(font); g.drawString(Message, 150, 300); } } public class Al extends KeyAdapter { public void keyPresed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_W) { if (!m.getMap(p.getTileX(), p.getTileY() - 1).equals("w")) ; p.move(0, -1); } if (e.getKeyCode() == KeyEvent.VK_A) { if (!m.getMap(p.getTileX(), p.getTileY() + 1).equals("w")) ; p.move(0, 1); } if (e.getKeyCode() == KeyEvent.VK_S) { if (!m.getMap(p.getTileX() - 1, p.getTileY()).equals("w")) ; p.move(-1, 0); } if (e.getKeyCode() == KeyEvent.VK_D) { if (!m.getMap(p.getTileX() + 1, p.getTileY()).equals("w")) ; p.move(1, 0); } } public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } } }
Map.java
import java.util.*; import java.awt.*; import java.io.*; import javax.swing.ImageIcon; public class Map { private Scanner m; private String Map[] = new String[16]; private Image dirt , wall, finish; public void map() { ImageIcon img = new ImageIcon("C://Noah's World//dirt.png"); dirt = img.getImage(); img = new ImageIcon("C://Noah's World//wall.png"); wall = img.getImage(); img = new ImageIcon("C://Noah's World//finish.png"); finish = img.getImage(); openFile(); readFile(); closeFile(); } public Image getFinish() { return finish; } public Image getDirt() { return dirt; } public Image getWall() { return wall; } public String getMap(int x, int y) { String index = Map[y].substring(x ,x + 1); return index; } public void openFile() { try{ m = new Scanner(new File("C://Noah's World//Map.txt")); }catch(Exception e) { System.out.println("error loading map"); } } public void readFile() { while(m.hasNext()) { for(int i = 0; i < 14; i++) { Map[i] = m.next(); } } } public void closeFile() { m.close(); } }
Player.ja
import java.awt.Image; import javax.swing.ImageIcon; public class Player { private int tileX, tileY; private Image player; public Player() { ImageIcon img = new ImageIcon("C://Noah's World//Player.png"); player = img.getImage(); tileX = 1; tileY = 1; } public Image getPlayer() { return player; } public int getTileX() { return tileX; } public int getTileY() { return tileY; } public void move(int dx, int dy) { tileX += dx; tileY += dy; } }
Error Report
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Map.getMap(Map.java:48) at Board.actionPerformed(Board.java:28) at javax.swing.Timer.fireActionPerformed(Timer.java:312) at javax.swing.Timer$DoPostEvent.run(Timer.java:244) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:671) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Map.getMap(Map.java:48) at Board.paint(Board.java:42) at javax.swing.JComponent.paintChildren(JComponent.java:887) at javax.swing.JComponent.paint(JComponent.java:1063) at javax.swing.JComponent.paintChildren(JComponent.java:887) at javax.swing.JComponent.paint(JComponent.java:1063) at javax.swing.JLayeredPane.paint(JLayeredPane.java:585) at javax.swing.JComponent.paintChildren(JComponent.java:887) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482) at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413) at javax.swing.RepaintManager.paint(RepaintManager.java:1206) at javax.swing.JComponent.paint(JComponent.java:1040) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115) at java.awt.Container.paint(Container.java:1967) at java.awt.Window.paint(Window.java:3877) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677) at javax.swing.RepaintManager.access$700(RepaintManager.java:59) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:671) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97) Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at Map.getMap(Map.java:48) at Board.paint(Board.java:42) at javax.swing.JComponent.paintChildren(JComponent.java:887) at javax.swing.JComponent.paint(JComponent.java:1063) at javax.swing.JComponent.paintChildren(JComponent.java:887) at javax.swing.JComponent.paint(JComponent.java:1063) at javax.swing.JLayeredPane.paint(JLayeredPane.java:585) at javax.swing.JComponent.paintChildren(JComponent.java:887) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5228) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1482) at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1413) at javax.swing.RepaintManager.paint(RepaintManager.java:1206) at javax.swing.JComponent.paint(JComponent.java:1040) at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39) at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:78) at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:115) at java.awt.Container.paint(Container.java:1967) at java.awt.Window.paint(Window.java:3877) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:781) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:728) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:677) at javax.swing.RepaintManager.access$700(RepaintManager.java:59) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1621) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:701) at java.awt.EventQueue.access$000(EventQueue.java:102) at java.awt.EventQueue$3.run(EventQueue.java:662) at java.awt.EventQueue$3.run(EventQueue.java:660) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:671) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139) at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)