import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; /** * The class that manages the model and the view. * Here, it starts independent threads for the model and its view, * and listens for button-presses that pause/resume the animation * in the view. * * @author David Mutchler. * Created October 18, 2008. */ public class Controller implements ActionListener { private View view; /** * Starts independent threads for the model and the view. * * @param model Model of this application -- here, just a number * that goes up and down over time * @param view View of the model -- here, just a point whose * x-value represents time and whose y-value represents * the model's data */ public Controller(Model model, View view) { this.view = view; new Thread(model).start(); new Thread(view).start(); } /** * Responds to a press of the pause/resume button * by toggling between the is-paused and not-paused animation state. * * @param event The event that invoked this method * (presumably a button-press) */ public void actionPerformed(ActionEvent event) { JButton pauseButton = (JButton) event.getSource(); if (pauseButton.getText().contains("pause")) { pauseButton.setText("Press me to resume the animation"); } else { pauseButton.setText("Press me to pause the animation"); } this.view.togglePause(); } }