How can i add time limitation in a Maze Game using java

I am making a maze game in Java. I have made a maze board, A start point and a end point. When I reach the end point then it exit and show a winning message. But I can not add a time limitation. Suppose player have to reach the end point with 30 seconds otherwise he lose the game. Please help me.Here is my code i have done so far.......Maze.java\[code\]package Maze;import javax.swing.JFrame;public class Maze { public static void main(String args[]) { new Maze(); } public Maze() { JFrame f= new JFrame(); f.setTitle("Maze Game"); f.add(new Board()); f.setSize(460,480); f.setLocationRelativeTo(f); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }}\[/code\]Board.java\[code\] package Maze;import java.awt.*;import java.awt.event.*;import javax.swing.*;// @SuppressWarnings("serial")@SuppressWarnings("serial")public class Board extends JPanel implements ActionListener { private Timer timer; private Map m; private Player p; private boolean win=false; long startTime = System.currentTimeMillis(); long elapsedTime; //private String Message=""; //private Font font=new Font("Serif",Font.BOLD,50); public Board() { long elapsedTime = System.currentTimeMillis() - startTime; elapsedTime=elapsedTime/1000; m= new Map(); p= new Player(); addKeyListener(new Al()); setFocusable(true); timer=new Timer(25, this); timer.start(); } public void actionPerformed(ActionEvent e) { if(m.getMap(p.getTileX(), p.getTileY()).equals("f")) { //Message="WINNER"; win=true; } if(elapsedTime>=5) win=true; repaint(); } public void paint(Graphics g) { super.paint(g); if(!win) { for(int y=0;y<14;y++) { for(int x=0;x<14;x++) { if(m.getMap(x,y).equals("f")) g.drawImage(m.getFinish(), x*32, y*32, null); if(m.getMap(x, y).equals("w")) g.drawImage(m.getWall(), x*32, y*32, null); if(m.getMap(x, y).equals("g")) g.drawImage(m.getGrass(), x*32, y*32, null); } } g.drawImage(p.getPlayer(), p.getTileX()*32, p.getTileY()*32,null); } if(win) { g.drawImage(m.getWinn(), 32, 32, null); // g.setColor(Color.ORANGE); //g.setFont(font); //g.drawString(Message, 150, 200); } } public class Al extends KeyAdapter{ public void keyPressed(KeyEvent e){ int keycode= e.getKeyCode(); if(keycode==KeyEvent.VK_UP ){ if(!m.getMap(p.getTileX(),p.getTileY()-1).equals("w")){ p.move( 0, -1); } } if(keycode==KeyEvent.VK_DOWN ){ if(!m.getMap(p.getTileX(),p.getTileY()+1).equals("w")){ p.move( 0, 1); } } if(keycode==KeyEvent.VK_LEFT ){ if(!m.getMap(p.getTileX()-1,p.getTileY()).equals("w")){ p.move( -1, 0); } } if(keycode==KeyEvent.VK_RIGHT ){ if(!m.getMap(p.getTileX()+1,p.getTileY()).equals("w")){ p.move( 1, 0 ); } } } /* public void keyRealeased(KeyEvent e){ } public void keyTyped(KeyEvent e){ }*/ } }\[/code\]Map.java\[code\]package Maze;import java.awt.*;import java.io.*;import java.util.*;import javax.swing.ImageIcon;public class Map { private Scanner m; private String Map[]=new String[14]; private Image grass,wall,finish,winn; public Map(){ ImageIcon img = new ImageIcon("C://project//7.jpg"); grass = img.getImage(); img = new ImageIcon("C://project//2.jpg"); wall = img.getImage(); img=new ImageIcon("C://project//hell.gif"); finish=img.getImage(); img=new ImageIcon("C://project//12.jpg"); winn=img.getImage(); openfile(); readfile(); closefile(); } public Image getGrass() { return grass; } public Image getWall() { return wall; } public Image getFinish() { return finish; } public Image getWinn() { return winn; } 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://project//Map.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block //e.printStackTrace(); System.out.println("Error loading file."); } } public void readfile(){ while(m.hasNext()){ for(int i=0;i<14;i++){ Map=m.next(); } } } public void closefile(){ m.close();}}\[/code\]Player.java\[code\]package Maze;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://project//5990.gif"); 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; }}\[/code\]and here is the .txt fileMap.txt\[code\]wwwwwwwwwwwwwwwggggggwgggggwwggwwggwgwwggwwwgggwwwggwggwwgwgggggggwwgwwgggwggwwwgggwwgggwgggwggwwwwggwggwwwggggwwgwwgggggwwggwwgggggwwwgwggwwggwggggwgwwgwwwwwgwwwwggwgwwggggwgfgggggwwwwwwwwwwwwwww\[/code\]
 
Top