动画线程和 EDT
相关代码(在用 runnable 包装 animate 方法并传递给稍后调用之前:
因此,在 JPanel 上有一行:
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
SuspishedAnimation.run()
内部,您不在 EDT 上。这就是您需要使用invokeLater()
的地方,而不是在调用Animate()
时:Inside of
SuspendedAnimation.run()
you're not on the EDT. That's where you need to useinvokeLater()
, not when callingAnimate()
: