【发布时间】:2016-05-17 17:48:59
【问题描述】:
我编写了一个简单的模拟程序,其中 2 个以不同速度移动的球尝试移动到帧的中间,然后返回到它们的起始位置。在指定的末尾,totalTime,我结束模拟并记录此数据。
我的问题是,是否可以自动循环并运行多个模拟?目前,当我的totalTime 启动时,动画只是冻结但窗口没有关闭。理想情况下,我想,我希望看到旧窗口关闭,然后弹出一个新窗口,显示不同球的新速度。
所以我的代码看起来像这样:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
double rSpeed = 0;
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setSize(500, 500);
Rectangle r = frame.getBounds();
frame.add(new MoveAgents(r.getWidth(), rSpeed));
}
});
}
public MoveAgents(double w, double rFrameSpeed) {
//initialize my 2 balls and their speeds and starting locations
Timer timer = new Timer(15, null);
timer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
totalTime++;
if (totalTime == 1000) {
timer.stop();
try {
generateCsvFile(OUTPUT_FILE);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
//otherwise, do things
}
}
【问题讨论】: