0

现在我完全被一个涉及 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;
                }
            };
4

1 回答 1

1

我已经完全改变了代码,远离了摇摆工人,变成了一个摇摆定时器和一个动作事件。我的对象现在正在移动,问题是我无法控制此事件发生的次数。我想在说 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();
于 2013-04-25T19:29:28.670 回答