JButton用于开始动画而不是更新

时间:2018-02-23 05:12:42

标签: java swing animation applet japplet

我遇到的问题是,当程序完成绘制GUI时,我想要一个按钮,它将收集来自其他Jobjects(JTextFields,Sliders等)的信息,并使用它来动态构建结果的图形。一个montecarlo模拟。但是,repaint();仅在程序退出EventListener时发生,而不是在绘制图形之后,以及在动画的每个步骤中发生。我认为这是程序结构化方式的一个更普遍的问题。

public class Runtime extends JApplet implements Runnable{
Border newSimulation;

    public void init() {
         this.setSize(800,600);
         try {
             SwingUtilities.invokeAndWait(new Runnable() {
                 public void run() {
                     createGUI();
                 }
             });
         } catch (Exception e) { 
             System.err.println("createGUI didn't complete successfully");
         }
     }
}

public class Graph extends JPanel implements MouseListener{
    JPanel graph;

     public Graph(){
         super(new BorderLayout());
         super.setSize(800, 600);
         addParticleConstraints();
         addControlPanel();
         super.add(graph, BorderLayout.SOUTH);
     }

     private JPanel addButton(JPanel control){
         JButton start = new JButton();
         start.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e)
             {
                 beginSimulation();
                 graph.revalidate();//these do nothing
                 graph.repaint();
                 simulate();

             }
         });
     }

     private void beginSimulation(){
         gatherData();
         drawGraph();
         graph.revalidate(); //both of these do nothing
         graph.repaint();
     }

     private void simulate(){
         while(particles[noOfIsotopes] < noOfParticles){
             graph.revalidate(); //both do nothing
             graph.repaint();
             for(int i = 0; i < noOfParticles; i++){
                 montecarlo();
             }
                  addPoints();//adds points to graph by image.createGraphics() as graph 
                             //is stored as buffered image and modified
         }
     }
}

0 个答案:

没有答案