Хочу создать анимацию движения иконки поезда, но все никак не могу понять как это сделать. С фигурами получилось с картинкой - нет, подскажите.
public class TrainMoveAnimation extends JPanel implements Runnable {
public static final String NAME = "Title Game";
public static final int WIDTH = 490;
public static final int HEIGHT = 450;
private Image image;
private Graphics2D g2;
public TrainMoveAnimation() {
loadImage();
new Thread(this).start();
}
private void loadImage() {
try {
image = ImageIO.read(new File("F://train.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void paint(Graphics g) {
super.paint(g);
g2 = (Graphics2D) g;
g2.drawImage(image, 0, 0, null);
}
public static void main(String[] args) {
TrainMoveAnimation game = new TrainMoveAnimation();
game.setSize(WIDTH, HEIGHT);
JFrame frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(WIDTH, HEIGHT);
frame.add(game);
frame.setVisible(true);
}
@Override
public void run() {
int x = 0;
int y = 0;
while (true) {
if (g2 != null) {
g2.drawImage(image, x, 0, null);
super.repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
x += 25;
}
}
}
}