0
你好我正在尝试使用java swing定时器以随机间隔生成一个Person对象。Java Swing Timer的随机时间间隔?
我正在使用函数生成一个随机泊松数,而且我计划使用随机生成的数作为延迟来创建下一个人对象。然而,即使泊松值表示9秒的时间间隔,它似乎也没有工作,因为所有事情都只产生彼此〜1秒。这是我在解决方案的尝试:
private ActionListener generatePerson = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
doSomething();
updateTimer();
}
private void doSomething()
{
try {
newPerson = new person(buildingType,currenttime,totnumoffloors);
newPerson.setArrivalTime(timeElapsed);
newPerson.printArrival();
peopleQueue[newPerson.currentFloor-1]++;
floorQueue[newPerson.currentFloor-1]++;
System.out.println("Added a new person to the queue.");
repaint();
System.out.println("Done repainting.");
} catch (IOException ex) {
System.out.println("Unable to create object!");
}
}
private void updateTimer()
{
int lol = 0;
periodicPerson.stop();
newPoissonTime = generatePoisson();
newPoissonTime = (newPoissonTime*1000);
lol = (int)newPoissonTime;
System.out.println("NEW POISSON TIME: " + lol);
periodicPerson.setDelay(lol);
periodicPerson.restart();
}
};
我将泊松时间乘以1000以将其转换为秒。
我然后产生初始计时器为:
periodicPerson = new Timer((int)generatePoisson()*1000, generatePerson);
我认为问题是,当它生成的泊松分布,它只是使用的是作为时间间隔,或者该延迟被简单地不工作。
非常感谢您提前!
哇!它的工作!我以为你需要停下来“重置”这个东西。非常感谢你,我是一个白痴:D –