使用SwingWorker设置坐标

时间:2013-04-23 17:51:55

标签: timer swingworker worldwind

现在我完全陷入了一个涉及WorldWind的项目,但它是SwingWorker和swing Timer Classes的一个普遍问题。

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

现在,当我按下“Animate”按钮时,它会调用此函数:

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

1 个答案:

答案 0 :(得分:1)

我完全改变了代码,远离了swing工作者,转变为一个swing计时器和一个动作事件。我的目标现在正在移动,问题是我无法控制此事件发生的次数。我想在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();