【问题标题】:Using SwingWorker to set a coordinate使用 SwingWorker 设置坐标
【发布时间】:2013-04-17 01:21:11
【问题描述】:

现在我完全被一个涉及 WorldWind 的项目所困扰,但它是 SwingWorker 和 swing Timer 类的普遍问题。

基本上我在地球上有一个形状,它有一个 LatLon 坐标,并且每隔一段时间,我都试图沿着 LatLon 中的预定向量移动它。除了实际的计时器事件之外,一切都应该正常工作。我已经尝试使用标准计时器中的多种东西,调用 System.getCurrentTimeMill() 并增加它,但它们都没有奏效。

现在,当我点击“动画”按钮时,它会调用这个函数:

private void animate(LatLon pos) throws InvocationTargetException, InterruptedException   {
        // TODO Auto-generated method stub

        if(SwingUtilities.isEventDispatchThread())
        {

            timer = new Timer(speed, this);
            timer.setInitialDelay(pause);
            timer.setRepeats(false);
            while (count < 5)
            {
                timer.start();
                CircleWorker.execute();
                sphere.setLocation(pos);
                count ++;
            }

        }

    else{
            SwingUtilities.invokeAndWait(new Runnable()
            {
                @Override
                public void run(){
                    int count = 0;

                    //timer = new Timer(speed, this);
                    timer.setInitialDelay(pause);
                    while (count < 30)
                    {
                        timer.start();
                        CircleWorker.execute();
                        count ++;
                        checker2();
                    }
                }});

这是我的 SwingWorker:

SwingWorker<LatLon, Void> CircleWorker = new SwingWorker<LatLon, Void>()
            {


                @Override
                protected LatLon doInBackground() 
                {
                    //checker();


                    double lat = changeAm.getLatitude().getDegrees() + currentPos.getLatitude().getDegrees();
                    double lon = changeAm.getLongitude().getDegrees() + currentPos.getLongitude().getDegrees();
                    // sets lat lon to the amounts in each individual amount

                    currentPos = LatLon.fromDegrees(lat, lon);
                    counter ++;

                    //checker2();

                    return currentPos;
                }
                @Override
                public void done()
                {
                    //checker3();
                    currentPos = ATLANTA;
                }
            };

【问题讨论】:

  • Timer 在自己的线程中执行 TimerTask。 Timer 是异步的。您的代码似乎不知道这一点。在您第一次拨打start 后,计时器将启动。随后的调用将做的很少。
  • 据我所知,计时器现在根本没有运行。调用 animate 函数后,球体就消失了。

标签: timer swingworker worldwind


【解决方案1】:

我已经完全改变了代码,从 swing worker 移出,变成了一个 swing Timer 和一个动作事件。我的对象现在正在移动,问题是我无法控制此事件发生的次数。我想在 30 或 100 次或多少次处决后停止它。

当前代码:

    private void animate() throws InvocationTargetException, InterruptedException   {
        // TODO Auto-generated method stub
        currentPos = ATLANTA;


        ActionListener sphereAction  = new ActionListener()
        {
            private int count = 0;
            @Override
            public void actionPerformed(ActionEvent evt) 
            {
                double lat = changeAm.getLatitude().getDegrees() + currentPos.getLatitude().getDegrees();
                double lon = changeAm.getLongitude().getDegrees() + currentPos.getLongitude().getDegrees();
                // sets lat lon to the amounts in each individual amount

                currentPos = LatLon.fromDegrees(lat, lon);


                //checker2();

                sphere.setLocation(currentPos); 
                setCount(getCount() + 1);
            }
            public int getCount() {
                return count;
            }
            public void setCount(int count) {
                this.count = count;
            }

        };

        Timer timer = new Timer(speed, sphereAction);
        timer.start();

        //Obviously this isn't working.
        if (count == 10)
            timer.stop();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-07
    • 2014-02-06
    • 2013-05-20
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多