I am trying to draw about 100 images on the Applet. When i did that I was not able to look at an image as the process was too fast. So I added sleep function so that I can give a pause between transition from one image to another. But that worked abnormally. I could not see any pictures and I think the sleep is getting called again and again. Please help.
Here is my code:
public class Test extends Applet
{
public void init()
{
setSize(1000,1000);
}
public void make(Graphics g,int i)
{
}
public void paint(Graphics g)
{
int i=0;
for(i=0;i<100;i++)
{
if(i!=65)
{
Image img = getImage(getDocumentBase(), "abc"+i+".png");
g.drawImage(img, 0, 0, this);
try
{
Thread.sleep(1000);
}
catch(Exception exception)
{
}
}
}
}
}
Now you can see I have images from 0 to 99 and I want them on my Applet window and after an image is displayed 1 sec delay should be there. But this is not the case. Please help
sleep
will freeze the EDT (Event Dispatching Thread). Since Swing is single threaded framework, anything that blocks (like sleep), prevents the EDT from running since paint
is called from the context of the EDT. Don't use sleep, use Timer
instead.
Another note, it's bad practice to catch
an exception and not handling it. This will hide serious unexpected things that might occur in your code, at least print the error message.