在Java小程序不断绘制图像 [英] Drawing images continuously in Java Applet

查看:158
本文介绍了在Java小程序不断绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在小程序得出100张图像。当我这样做,我不能看图像的过程太快了。所以我加了休眠功能,这样我可以从一个图像到另一个给过渡之间的停顿。但是,工作异常。我没有看到任何图片,我认为睡眠是越来越一次又一次地叫。请帮忙。结果
这里是我的code:结果

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)
            {

            }
        }
    }

}

}

现在你可以看到我有一个从0到99的图像,我想他们我的小程序窗口,并显示图像后1秒的延迟应该在那里。但这种情况并非如此。请帮助

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

推荐答案

睡眠将冻结EDT(事件分派线程)。由于Swing是单线程的框架下,任何模块(如睡眠),$ P $从自EDT <的油漆从的所谓上下文中运行pvents的EDT / em>的。不要使用休眠,使用 定时 来代替。

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.

另外要注意,这是不好的做法,例外的和不处理它。这将隐藏可能发生在你的code严重意想不到的事情,至少打印错误消息。

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.

这篇关于在Java小程序不断绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆