I have coded up a simple simulation where 2 balls moving at different speeds try to move to the middle of the frame and then move back to their starting positions. At the end a specified, totalTime
, I end the simulation and I record this data.
My question is, is it possible to loop through and run multiple simulations automatically? Currently, when my totalTime
is up, the animation just freezes but the window doesn't close. Ideally I guess, I'd like to see the old window close and a new window pop up with new speeds for the different balls.
So my code looks something like this:
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
}
}