Animation Help

I am trying to make a game with java. I have a class that draws a game figure. When I try to move the game figure when the mouse goes over the screen, it does nothing. How do I get it to repaint itself? i tryed using repaint();
[234 byte] By [Nazgul42a] at [2007-10-3 11:58:36]
# 1
How are you "trying to move the figure"?Please post some code or describe what you are trying to do.
zadoka at 2007-7-15 14:34:33 > top of Java-index,Security,Cryptography...
# 2
Most probably you're not using the EDT correctly. Search for Event Dispatch Thread and the related animation repainting issues.
kirillga at 2007-7-15 14:34:33 > top of Java-index,Security,Cryptography...
# 3

import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.io.*;

import javax.imageio.ImageIO;

import javax.swing.*;

public class ImageMotion extends JPanel {

BufferedImage image;

Rectangle r;

public ImageMotion(BufferedImage image) {

this.image = image;

r = new Rectangle(100, 100, image.getWidth(), image.getHeight());

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.drawImage(image, r.x, r.y, this);

}

private void moveImage(int dx, int dy) {

r.x += 2*dx;

r.y += 2*dy;

repaint();

}

private MouseMotionListener waver = new MouseMotionAdapter() {

Point lastP = new Point();

public void mouseMoved(MouseEvent e) {

Point p = e.getPoint();

int dx = p.x < lastP.x ? -1 : p.x > lastP.x ? 1 : 0;

int dy = p.y < lastP.y ? -1 : p.y > lastP.y ? 1 : 0;

lastP.setLocation(p);

moveImage(dx, dy);

}

};

public static void main(String[] args) throws IOException {

BufferedImage image = ImageIO.read(new File("images/dukeWaveRed.gif"));

ImageMotion test = new ImageMotion(image);

test.addMouseMotionListener(test.waver);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

}

crwooda at 2007-7-15 14:34:33 > top of Java-index,Security,Cryptography...
# 4
I Found out what I was doing wrong. I was moving it, but then i was setting it back to the same position again.
Nazgul42a at 2007-7-15 14:34:33 > top of Java-index,Security,Cryptography...