Search code examples
javaappletawtlagevent-dispatch-thread

java delay method crashes system


I'm trying to make a program that models two characters that are racing along a track in a java applet using netbeans. I currently have the racing logic and graphics working fine, but I'm having trouble implementing a delay to show each move the characters make individually.

To create a delay, I currently have this method written:

public void delay()
{

try
            {
            Thread.sleep(500);
            }

catch (InterruptedException ie)
        {
            ie.printStackTrace();
        }

}

However, this causes the program to lag so much that it crashes the appletViewer. How can I delay the system such that it doesn't lag the system?

Thank you for any responses.


Solution

  • Which thread do you call delay()? The UI thread? If it is the case, you may your applet not responsive and break your app. Try the callback function approach.