I am writing a code to move shapes by a particular amount. I have put this code in a loop so that the program moves the shapes 10 times. But, rather than just showing the last output, I want to see the shapes moving. So, I have implemented the sleep method in a separate thread. But, still the shapes move fast and I am only seeing the final output.
Anybody knows what is the issue here and how can I resolve it?
Code:
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("START"))
{
for(int i=0;i<100;i++)
{
pf.particleFilter();
repaint();
sleepFunction();
}
}
}
private void sleepFunction()
{
System.out.println("In sleep thread");
Thread mythread = new Thread()
{
public void run()
{
try
{
System.out.println("Going to sleep!");
Thread.sleep(1000);
}
catch(InterruptedException ex)
{
Thread.currentThread().interrupt();
}
}
};
mythread.start();
}
public void particleFilter()
{
move();
}
public void move()
{
setXPosition_robot(robot_x);
setYPosition_robot(robot_y);
setXPosition_particle(particle_x);
setYPosition_particle(particle_y);
}