Java Swing 循环运行多个模拟

标签 java swing

我编写了一个简单的模拟,其中两个以不同速度移动的球尝试移动到框架的中间,然后移回其起始位置。在指定的 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 
    }
 }

最佳答案

Is it possible to loop through and run multiple simulations automatically?

是的。当模拟结束时,不要尝试替换 View 。相反,

  • 定时器上调用stop()

  • 调用generateCsvFile()来保存结果。

  • 与第一次完全一样地初始化模拟模型。

  • 定时器上调用restart()

在下面的示例中,模型是一个名为 dataint,模拟只是将其渲染并递增到 1000。重复模拟次数的count作为参数传递给Simulation构造函数。代替 frame.setSize(),按照讨论 here 重写绘图面板的 getPreferredSize() .

image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 * @see https://stackoverflow.com/a/37293513/230513
 */
public class SimTest {

    private void display() {
        JFrame f = new JFrame("SimTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Simulation(8));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static class Simulation extends JPanel {

        private final Font font = this.getFont().deriveFont(36f);
        private int count;
        private int data;
        Timer t = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                repaint();
                if ((data += 100) == 1000) {
                    t.stop();
                    System.out.println("Saving results…");//generateCsvFile();
                    if (--count == 0) {
                        System.out.println("Fin!");
                        System.exit(0);
                    } else {
                        init();
                        t.restart();
                    }
                }
            }
        });

        public Simulation(int count) {
            this.count = count;
            init();
            t.start();
        }

        private void init() {
            data = 0;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setFont(font);
            g.drawString(String.valueOf(data), 8, g.getFontMetrics().getHeight());
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(256, 128);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new SimTest()::display);
    }
}

关于Java Swing 循环运行多个模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37283109/

相关文章:

Java正则表达式奇怪的东西

java - 在测量之间创建延迟

java - JComponent 未在 JPanel 内绘制

java - 如何从构造函数返回自定义颜色

java - 不可编辑的 JComboBox 边框

java - 无法写入内容: failed to lazily initialize a collection of role using OpenEntityManagerInViewFilter

java - 这只是一个错字吗?

java - 当条件设置为 false 时,While 循环执行最后一次

java - 使用 Swing 运行文件不更新 GUI 中的更改

java - 使用 swingworker 动画递归三角剖分算法