TOTALLY FRUSTRATED: can't get my animation to work. please help!
I'm trying to make a pacman game. At the beginning I had everything in one file, and theprogram worked fine. But since the code was getting big, I decided to break the code down into two files (see the codes below). The first one handles pacman animation, and the second file paints it to the screen and adds keyboard controls. Now here is where the problem starts.
Now that I have separated the files, the program compiles but I only see the first image of pacman on the screen, which means that the animation doesn't start. But when I minimize and maximize the screen, I see the second image of pacman and so on. I know that this has something to do with repaint, but what? I've tried everything. (if I try to add repaint(); to the end of paintComponent, the animation starts but its TOO FAST, meaning that pacman's mouth moves too quickly and he moves around too fast)
I'll be very grateful if anyone can help (if possible support your guidance with an example statement)
Thanx and Happy New Year
Here are the codes:
THIS file handles creating pacman animation with GIF images
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
publicclass PacmanMextends JPanelimplements ActionListener
{
ImageIcon images[];
staticfinalint TOTAL_IMAGES=28, ANIM_DELAY=34;
int currentImage = 0;
private Timer animationTimer;
privateint delay = 50;
int startImage = 0 ;
int endImage = 6 ;
public PacmanM(int delay)
{
this.delay = delay;
setupAnimation();
}
public PacmanM()
{
setupAnimation();
}
// method for creating animation using Pacman images
publicvoid setupAnimation()
{
this.images =new ImageIcon[TOTAL_IMAGES];
for (int i = 0; i < images.length; i++)
{
images[i] =new ImageIcon("pac"+i+".gif");
}
startAnimation();
}
publicvoid startAnimation()
{
if (animationTimer ==null)
{
currentImage = 0;
animationTimer =new Timer( ANIM_DELAY + delay,this);
animationTimer.start();
}
else
{
if (!animationTimer.isRunning())
animationTimer.restart();
}
}
publicvoid stopAnimation (){ animationTimer.stop();}
publicvoid actionPerformed(ActionEvent e){ repaint();}
}
====================================================
The below code paints pacman to the screen and adds keyboard control
-
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
publicclass Gameextends JPanelimplements ActionListener, KeyListener{
publicstaticfinalint RIGHT = 0;
publicstaticfinalint LEFT = 7;
publicstaticfinalint UP = 14;
publicstaticfinalint DOWN = 21;
int currentDirection = RIGHT;
int nextDirection;
int forwardBy = 5;
int upBy = 0;
int xCount = 60;
int yCount = 85;
PacmanM pac =new PacmanM();
publicvoid paintComponent(Graphics g)
{
super.paintComponent(g);
this.addKeyListener(this);
requestFocus();
if (pac.images[pac.currentImage].getImageLoadStatus() == MediaTracker.COMPLETE)
{
anim.images[anim.currentImage].paintIcon(this, g, xCount, yCount);
xCount += forwardBy;
yCount += upBy;
if (++pac.currentImage > pac.endImage) pac.currentImage = pac.startImage;
}
}
// Setting up the keyboard controls for Pacman's movements
publicvoid keyPressed(KeyEvent e)
{
// Setting up controls for the "left" arrow key
if (e.getKeyCode()==KeyEvent.VK_LEFT)
{
if (currentDirection != LEFT)
{
currentDirection = LEFT;
forwardBy = -5;
upBy = 0;
pac.startImage = 7;
pac.endImage = 13;
pac.currentImage = pac.startImage;
}
}
// Setting up controls for the "right" arrow key
if (e.getKeyCode()==KeyEvent.VK_RIGHT)
{
if (currentDirection != RIGHT)
{
currentDirection = RIGHT;
forwardBy = 5;
upBy = 0;
pac.startImage = 0;
pac.endImage = 6;
pac.currentImage = pac.startImage;
}
}
// Setting up controls for the "up" arrow key
if (e.getKeyCode()==KeyEvent.VK_UP)
{
if (currentDirection != UP)
{
currentDirection = UP;
upBy = -5;
forwardBy = 0;
pac.startImage = 14;
pac.endImage = 20;
pac.currentImage = pac.startImage;
}
}
// Setting up controls for the "down" arrow key
if (e.getKeyCode()==KeyEvent.VK_DOWN)
{
if (currentDirection != DOWN)
{
currentDirection = DOWN;
upBy = 5;
forwardBy = 0;
pac.startImage = 21;
pac.endImage = 27;
pac.currentImage = pac.startImage;
}
}
}
publicvoid keyReleased (KeyEvent e){};
publicvoid keyTyped (KeyEvent e){};
public Dimension getMinimumSize(){return getPreferredSize();}
public Dimension getPreferredSize (){returnnew Dimension(750,750);}
publicvoid actionPerformed(ActionEvent e){ repaint();}
publicstaticvoid main(String[] args )
{
Game app =new Game();
JFrame main =new JFrame("PACMAN");
main.getContentPane().add(app, BorderLayout.CENTER);
main.addWindowListener(new WindowAdapter()
{
publicvoid windowClosing(WindowEvent e)
{
System.exit(0);
}
});
main.pack();
main.show();
}
}

