动画线程和美国东部时间 [英] Animation Thread and EDT

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

问题描述

当我与Inerdia在earlier帖子,结果
东西仍然是陌生的。当我在一些JPanel的是(EDT肯定,我的方法检查选中),然后我把一些动画线程(线程延长线)开始,内螺纹我不是EDT支票。结果
所以我想我应该是因为动画应该是美国东部时间,所以我包裹着的可运行和invokeAndWait()的动画的方法,但还是得到了在动画线程我不是EDT,一边打电话到code作为我前面说的是美国东部时间上的,所以,我的invokeLater似乎不放在EDT的动画?这是为什么?

相关code(带可运行包裹动画的方法,并通过以后调用之前:结果
所以,正对一个JPanel有一行:

 动画(trainRailRoadTrack);

实施

 无效动画(ArrayList的< RailroadSquare> i_TrainRailRoadTrack){
    ArrayList的< JPanelRailoadSquare> playerRailoadPanelsTrack = getRelevantRailroads(i_TrainRailRoadTrack);
    新SuspendedAnimation(playerRailoadPanelsTrack)。开始();
    。jPanelBoard1.GetGameManager()EmptyPlayerSolution();
}私有类SuspendedAnimation继承Thread
{
    私人的ArrayList< JPanelRailoadSquare> m_PlayerRailoadPanelsTrack;    公共SuspendedAnimation(ArrayList的< JPanelRailoadSquare> i_PlayerRailoadPanelTrack)
    {
        m_PlayerRailoadPanelsTrack = i_PlayerRailoadPanelTrack;
    }    @覆盖
    公共无效的run()
    {
       m_IsAnimationNeeded = TRUE;
       对于(JPanelRailoadSquare currRailoadSquare:m_PlayerRailoadPanelsTrack)
       {
           的System.out.println(是事件调度线程:+ SwingUtilities.isEventDispatchThread());
           currRailoadSquare.SetGoingTrain();
           重绘();
           尝试
           {
               视频下载(150);           }
           赶上(InterruptedException的E){}
           currRailoadSquare.UnSetGoingTrain();
           重绘();
    }
}


解决方案

里面的 SuspendedAnimation.run()你的的上美东时间。这就是你需要使用的invokeLater(),不打电话时动画()

  @覆盖
公共无效的run()
{
    //我们是EDT外最运行()
    m_IsAnimationNeeded = TRUE;
    对于(JPanelRailoadSquare currRailoadSquare:m_PlayerRailoadPanelsTrack)
    {
        SwingUtilities.invokeAndWait(新的Runnable(){
            //将code说,会谈,Swing组件已被提上
            //将EDT
            currRailoadSquare.SetGoingTrain();
            重绘();
        });        //我们要保持睡眠的EDT之外。
        尝试
        {
            视频下载(150);
        }
        赶上(InterruptedException的E){}        SwingUtilities.invokeAndWait(新的Runnable(){
            currRailoadSquare.UnSetGoingTrain();
            重绘();
        }
    }
}

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?

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

Implementation is:

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

解决方案

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

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

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