title: java pacman wall collision not working correctly
body: <p>trying to code java pacman.i used bufferedimage to store the maze.To check pacman's collision with walls,i used 2d int array,value 0 means empty space &amp; pacman can move over it,1 is wall.pacman object should move 1 square at a time on keypress,stop on keyrelease. But pacman is not moving correctly,it sometimes moves over the wall square too,sometimes it moves and then stops completely even when it is in empty square . not sure where  iam going wrong. I used the below classes. Please could somebody help. </p>
  
  <pre><code>public class Map extends JPanel implements Runnable{
  
  private int TILE_W=15; //divide map into 15x15 tiles
  private int TILE_H=20; //there will be 20 tiles along width and 15 along height,total of 300 tiles
  
  private Image tile; // 15x15 png image
  
  private BufferedImage img; //stores maze image
  
  private long DELAY=50;
  
  private Pacman pacman;
  
  private int[][] mapdata={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}, 
                           {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
                           {1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1},
                           {1,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1},
                           {1,0,0,1,0,0,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                           {1,0,0,1,1,1,1,0,1,0,1,0,1,0,0,0,0,1,0,1},
                           {1,0,0,0,0,0,0,0,1,0,1,0,1,0,0,0,0,1,0,1},
                           {1,0,0,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                           {1,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,1},
                           {1,0,0,1,0,0,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                           {1,0,0,1,0,0,1,0,1,0,1,0,1,0,0,0,0,1,0,1},
                           {1,0,0,1,0,0,1,0,1,0,1,0,1,0,0,0,0,1,0,1},
                           {1,0,0,1,1,1,1,0,1,0,1,0,1,1,1,1,1,1,0,1},
                           {1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1},
                           {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
                          };
  
  public Map(){
    ImageIcon ict=new ImageIcon(this.getClass().getResource("tile.png"));
    tile=ict.getImage();
    addKeyListener(new KbListener());
    setFocusable(true);
    setDoubleBuffered(true);
    pacman=new Pacman(mapdata);
    img=new BufferedImage(350,350,BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2D=(Graphics2D) img.getGraphics();
    for(int x=0;x&lt;TILE_W;x++)
      for(int y=0;y&lt;TILE_H;y++){
              if(mapdata[x][y]==1)
                  g2D.drawImage(tile,y*15,x*15,this);
      }
    Thread t=new Thread(this);
    t.start();
  }
  
  @Override
  public void paintComponent(Graphics g){
      super.paintComponent(g);
      Graphics2D g2D=(Graphics2D) g;
      g2D.drawImage(img,0,0,this);
      g2D.drawImage(pacman.getImage(),pacman.getX(),pacman.getY(),this);
      Toolkit.getDefaultToolkit().sync();
      g.dispose();
  }
  
  public void run() {
      // TODO Auto-generated method stub
      while(true){
       repaint();
       pacman.move();
       try {
          Thread.sleep(DELAY);
       } catch (InterruptedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
       }
     }
  }
  
  private class KbListener extends KeyAdapter{
      public void keyReleased(KeyEvent e){
          pacman.keyReleased(e);
      }
  
      public void keyPressed(KeyEvent e){
          pacman.keyPressed(e);
      }
  }
  
  }
  
  
  public class Pacman {
  private Image pacman;
  private int x,y;
  private int dx,dy;
  
  private int[][] mapdata;
  private int mapx,mapy;
  
  public Pacman(int[][] mapdata){
      ImageIcon icp=new ImageIcon(this.getClass().getResource("pacman.png"));
      pacman=icp.getImage();
      x=30;
      y=15;
      dx=0;
      dy=0;
      mapx=2;
      mapy=1;
      this.mapdata=mapdata;
  }
  
  public void move(){
      x+=dx;
      y+=dy;  
  }
  
  public Image getImage(){
      return pacman;
  }
  
  public int getX(){
      return x;
  }
  
  public int getY(){
      return y;
  }
  
  public void keyPressed(KeyEvent e){
      int k=e.getKeyCode();
      if(k==KeyEvent.VK_LEFT){
          mapx=(x-15)/15;
          if(mapdata[mapx][mapy]==1)
              dx=0;
          else dx=-15; 
      }
      if(k==KeyEvent.VK_RIGHT){
          mapx=(x+15)/15;
          if(mapdata[mapx][mapy]==1)
              dx=0;
          else dx=15;
      }
      if(k==KeyEvent.VK_UP){
          mapy=(y-15)/15;
          if(mapdata[mapx][mapy]==1)
              dy=0;
          else dy=-15;
      }
      if(k==KeyEvent.VK_DOWN){
          mapy=(y+15)/15;
          if(mapdata[mapx][mapy]==1)
              dy=0;
          else dy=15;
      }
  }
  
  public void keyReleased(KeyEvent e){
      int k=e.getKeyCode();
      if(k==KeyEvent.VK_LEFT)
          dx=0;
      if(k==KeyEvent.VK_RIGHT)
          dx=0;
      if(k==KeyEvent.VK_UP)
          dy=0;
      if(k==KeyEvent.VK_DOWN)
          dy=0;
  }
  }
  </code></pre>