Ambidextrous
Ambidextrous

Reputation: 872

No delay in GUI execution even after implementing sleep in a separate thread

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

Upvotes: 1

Views: 107

Answers (1)

camickr
camickr

Reputation: 324147

The sleep function executes in a separate thread so it does not cause the Event Dispatch Thread (EDT) to sleep, so the EDT keeps on executing.

This is a good thing as you never want the EDT to sleep since this will prevent the GUI from repainting itself.

To do animation you need to use a Swing Timer. Read the section from the Swing tutorial on How to Use Swing Timers for more information.

I find the example in the tutorial a little complicated so here is the most basic example I can think of:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class TimerTime extends JPanel implements ActionListener
{
    private JLabel timeLabel;

    public TimerTime()
    {
        timeLabel = new JLabel( new Date().toString() );
        add( timeLabel );

        Timer timer = new Timer(1000, this);
        timer.setInitialDelay(1);
        timer.start();
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        timeLabel.setText( new Date().toString() );
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("TimerTime");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new TimerTime() );
        frame.setLocationByPlatform( true );
        frame.pack();
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

Upvotes: 4

Related Questions