现在我完全被一个涉及WorldWind的项目困住了,但这是SwingWorker和swing Timer类的一个普遍问题。
基本上,我在一个地球仪上有一个形状,它有一个拉特朗坐标,每一段时间,我试图沿着拉特朗的一个预定向量移动它。除了实际的计时器事件之外,一切都应该正常工作。我试过使用多个东西从一个标准的计时器,调用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;
}
};
我已经完全更改了代码,不再使用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();