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();

}

}

[10822 byte] By [pars40a] at [2007-9-28 6:22:50]
# 1

I know your problem, your using passive rendering (by calling repaint() from your Timer class).

repaint() doesn't paint immediately. It goes into some sort of a queue and then later when the JVM decides to paint it then will the painting be updated.

try replacing that statement with paintImmediately(0,0,width_of_your_panel,height_of_your_panel)

this method paints immediately as stated. (another kind of active rendering)

~Jeffrey

Jeffrey_Cheea at 2007-7-9 17:34:42 > top of Java-index,Other Topics,Java Game Development...
# 2
Jeffrey,could you please point me to where exactly I should put that statement. I'm sorry but I don't have much experience. I tried to replace the repaint() statement in "actionPerformed" with the statement you mentioned, but it didn't work. Thanx for the reply!
pars40a at 2007-7-9 17:34:42 > top of Java-index,Other Topics,Java Game Development...
# 3

eh. after looking thru your code, I realised your 1st class did not do any painting at all. (you did not override the paintComponent() method).

Your painting is done in the 2nd class.

So I think the timer to refresh the display should be in the 2nd class.

Try to move the animationTimer into the class Game and see if it works.

~Jeffrey

Jeffrey_Cheea at 2007-7-9 17:34:42 > top of Java-index,Other Topics,Java Game Development...
# 4
You are right! it worked. Thanks a lot and happy new year
pars40a at 2007-7-9 17:34:42 > top of Java-index,Other Topics,Java Game Development...