// Stuart Reges // 1/26/00 // // Class CritterFrame provides the user interface for a simple simulation // program. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class CritterFrame extends JFrame { private CritterModel myModel; private CritterPanel myPicture; private Timer myTimer; public CritterFrame() { // create frame and order list setTitle("CSE142 critter simulation"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container contentPane = getContentPane(); myModel = new CritterModel(100, 50); // set up critter picture panel and set size myPicture = new CritterPanel(myModel); setSize(CritterPanel.FONT_SIZE * myModel.getWidth()/2 + 125, CritterPanel.FONT_SIZE * myModel.getHeight() + 100); contentPane.add(myPicture, "Center"); addTimer(); // add timer controls to the south JPanel p = new JPanel(); JButton b1 = new JButton("start"); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myTimer.start(); } }); p.add(b1); JButton b2 = new JButton("stop"); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myTimer.stop(); } }); p.add(b2); JButton b3 = new JButton("step"); b3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { myModel.update(); myPicture.repaint(); } }); p.add(b3); contentPane.add(p, "South"); } // add a certain number of critters of a particular class to the simulation public void add(int number, Class c) { myModel.add(number, c); } // post: creates a timer that calls the model's update // method and repaints the display private void addTimer() { ActionListener updater = new ActionListener() { public void actionPerformed(ActionEvent e) { myModel.update(); myPicture.repaint(); } }; myTimer = new Timer(100, updater); myTimer.setCoalesce(true); } }