动画线程和 EDT [英] Animation Thread and EDT

查看:23
本文介绍了动画线程和 EDT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在 与 Inerdia 讨论的那样较早的帖子,
有些事情仍然很奇怪当我在一些 JPanel 中(EDT 肯定是我检查了方法检查)然后我调用了一些动画线程(线程扩展线程)来启动,在线程内我没有通过检查在 EDT 上.
所以我想我应该是因为动画应该在 EDT 上,所以我用 runnable 和 invokeAndWait() 包装了 animate 方法,但仍然在动画线程中得到了我不在 EDT 上,同时调用我之前说过的代码是在 EDT 上,所以,我的 invokeLater 似乎没有将该动画放在 EDT 上?这是为什么?

As I discussed with Inerdia on the earlier post,
something is still strange When I'm in some JPanel (EDT for sure-I checked with the method check) and then I call some animation thread(the thread extend Thread) to start, inside the thread I'm not on EDT by check.
So I guess I should be because animation should be on EDT, so I wrapped the animate method with runnable and invokeAndWait(), but still got that in the animation thread I'm not on EDT, while calling to that code as I said earlier is on EDT, so, my invokeLater seems not to place that animation on EDT? why is that?

相关代码(在用runnable包装animate方法并传递给稍后调用之前:
因此,在 JPanel 上有一行:

Relevant code(before wrapping the animate method with runnable and passing to invoke later:
So, being on a JPanel there is a line:

Animate(trainRailRoadTrack);  

实现是:

void Animate(ArrayList<RailroadSquare> i_TrainRailRoadTrack) {
    ArrayList<JPanelRailoadSquare> playerRailoadPanelsTrack = getRelevantRailroads(i_TrainRailRoadTrack);
    new SuspendedAnimation(playerRailoadPanelsTrack).start();
    jPanelBoard1.GetGameManager().EmptyPlayerSolution();
}

private class SuspendedAnimation extends Thread
{
    private ArrayList<JPanelRailoadSquare> m_PlayerRailoadPanelsTrack;

    public SuspendedAnimation(ArrayList<JPanelRailoadSquare> i_PlayerRailoadPanelTrack)
    {
        m_PlayerRailoadPanelsTrack = i_PlayerRailoadPanelTrack;
    }

    @Override
    public void run()
    {
       m_IsAnimationNeeded = true;
       for (JPanelRailoadSquare currRailoadSquare: m_PlayerRailoadPanelsTrack)
       {
           System.out.println("Is on Event dispatch thread: "+SwingUtilities.isEventDispatchThread());
           currRailoadSquare.SetGoingTrain();
           repaint();                            
           try
           {
               Thread.sleep(150);

           }
           catch (InterruptedException e){}
           currRailoadSquare.UnSetGoingTrain();
           repaint();                       
    }
}

推荐答案

SuspendedAnimation.run() 内部,您不是在 EDT 上.这就是您需要使用 invokeLater() 的地方,而不是在调用 Animate() 时:

Inside of SuspendedAnimation.run() you're not on the EDT. That's where you need to use invokeLater(), not when calling Animate():

@Override
public void run()
{
    // We're outside the EDT in most of run()
    m_IsAnimationNeeded = true;
    for (JPanelRailoadSquare currRailoadSquare: m_PlayerRailoadPanelsTrack)
    {
        SwingUtilities.invokeAndWait(new Runnable() {
            // The code that "talks" to Swing components has to be put on
            // the EDT
            currRailoadSquare.SetGoingTrain();
            repaint();
        });

        // We want to keep sleeping outside the EDT.
        try
        {
            Thread.sleep(150);
        }
        catch (InterruptedException e){}

        SwingUtilities.invokeAndWait(new Runnable() {
            currRailoadSquare.UnSetGoingTrain();
            repaint();                       
        }
    }
}

这篇关于动画线程和 EDT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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