Help: I cant get the controls right
I am writing pacman in Java,but the controls don't work. I have added all the keyListeners (I think), and the program compiles without problem, but the controls don't respond at all. Could you please let me know what Im doing wrong.
THANX
Here is the code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class PacmanM extends JPanel implements ActionListener, KeyListener
{
private ImageIcon images[];
private static final int TOTAL_IMAGES=14, ANIM_DELAY=50;
private int currentImage = 0;
private Timer animationTimer;
private int forwardBy = 5;
private int count = 0;
private int delay = 0;
public static int N =1;
//public static final int UP = 0;
//public static final int DOWN = 1;
public static final int LEFT = 7;
public static final int RIGHT = 0;
private int currentDirection = RIGHT;
private int startImage = 0 ;
private int endImage = 6 ;
public PacmanM(int delay)
{
this.delay = delay;
setupAnimation();
}
public PacmanM()
{
setupAnimation();
}
public void setupAnimation()
{
this.setSize(this.getPreferredSize());
this.images = new ImageIcon[TOTAL_IMAGES];
for (int i = 0; i < images.length; i++)
{
images = new ImageIcon("pac"+i+".gif");
}
startAnimation();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(0,0,this.getWidth(), this. getHeight());
if (images[currentImage].getImageLoadStatus() == MediaTracker.COMPLETE)
{
images[currentImage].paintIcon(this, g, count, 0);
if (count > (this.getWidth()-images[0].getIconWidth()))
{ forwardBy = -5;
startImage = 7;
endImage = 13;
currentImage = startImage;
}
if (count < 0)
{
forwardBy = 5;
startImage = 0;
endImage = 6;
currentImage = startImage;
}
count += forwardBy;
}
if (++currentImage > endImage) currentImage = startImage;
System.out.println(currentImage);
}
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_LEFT)
{
currentDirection = LEFT;
forwardBy = -5;
startImage = 7;
endImage = 13;
currentImage = startImage;
}
else if (e.getKeyCode()==KeyEvent.VK_RIGHT)
{
forwardBy = 5;
currentDirection = RIGHT;
startImage = 0;
currentImage = startImage;
}
else if (e.getKeyCode()=='a')
{
forwardBy = 0;
}
}
public void keyTyped (KeyEvent e) {};
public void keyReleased (KeyEvent e) {};
public void startAnimation()
{
if (animationTimer == null)
{
currentImage = 0;
animationTimer = new Timer( ANIM_DELAY + delay, this);
animationTimer.start();
}
else //continue animating..
{
if (!animationTimer.isRunning())
animationTimer.restart();
}
}
public void stopAnimation (){ animationTimer.stop(); }
public void actionPerformed(ActionEvent e){ repaint(); }
public Dimension getMinimumSize() { return getPreferredSize(); }
public Dimension getPreferredSize (){return new Dimension(400,400);}
public static void main(String[] args )
{
PacmanM PacMan = new PacmanM();
JFrame main = new JFrame("PacMan");
main.getContentPane().add(PacMan, BorderLayout.CENTER);
main.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
main.pack();
main.show();
}
}

