Я пытаюсь создать анимацию. Предполагается перемещение двух изображений - автомобиля и человека. Но человек состоит из двух образов, так что это выглядит как правильное движение. Я пытаюсь поместить массив изображений в мой код, но он не работает. Не могли бы вы помочь с этим?
Вот мой код
public class Main extends JFrame implements Runnable{
int x=0;
public Image car = new ImageIcon( "car.png").getImage();
public Image[] human;
private Thread thread;
public Main() {
super("Animation");
this.setSize(700,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
thread = new Thread(this);
thread.start();
}
public void paint(Graphics g) {
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(),this.getHeight());//background
g.drawImage(car,x,150,null);
human = new Image[2];
human[0] = new ImageIcon("move.png").getImage();
human[1] = new ImageIcon("stop.png").getImage();
for (int i=0; i<2; i++)
{
g.drawImage(human[i], x , 200,null);
}
}
public static void main(String[] args) {
Main f = new Main();
f.setVisible(true);
f.setLocationRelativeTo(null);
}
@Override
public void run() {
while(true)
{
x++;
repaint();
try {
Thread.sleep(100);
}catch(InterruptedException e) {
System.out.println("Error!");
}
}
}
}