/** * Stop/Start/Reset Buttons for CtrlPanl.class re DifEqnAp.class * @author Robert J Morton UK-YE572246C * @version 27 November 1997 modified 07 August 2009, 18 April 2012 */ /* This Panel class contains 4 buttons: Stop, Start, Reset, Spot to control the operation of the applet */ import java.awt.*; // for graphics operations (GUI) import java.awt.event.*; // for the new-fangled 1.1 event handling import javax.swing.*; // library for constructing Swing GUI public class setbutt extends JPanel { private difeqnap ap; // instance reference of the main applet class private timegr tg; // instance reference of the timegr class private bouncegr bg; // instance reference of the bouncegr class private formula f; // instance reference of the formula class private JButton a, b; // references for the 2 buttons private boolean RunFlag = false; // applet starts off with the equation not iterating setbutt(difeqnap ap, timegr tg, bouncegr bg, formula f) { this.ap = ap; // instance ref. of main applet class this.tg = tg; // instance ref. of timegr class this.bg = bg; // instance ref. of bouncegr class this.f = f; // instance ref. of formula class setLayout(null); // to allow the button panel to be laid out manually /* Create the 'start' button, annotate it with the word "Start", add it to this panel, set its position and size add a listener to listen for when it is pressed. */ a = new JButton(); a.setText("Start"); add(a); a.setBounds(5, 1, 110, 33); a.addActionListener(new stopbut(0, this)); /* Create the 'cleat' button, annotate it with the word "Clear", add it to this panel, set its position and size add a listener to listen for when it is pressed. */ b = new JButton(); b.setText("Clear"); add(b); b.setBounds(120,1,110, 33); b.addActionListener(new stopbut(1, this)); } /* Stop the iteration process by clearing the 'run' flag to stop iteration and updating the Run Flag state in main applet. */ void Stop() { Relabel(RunFlag = false); ap.setRF(false); } void reset() { // USED BY RESET BUTTON, EQN CHANGE, C CHANGE RunFlag = false; // stop the program from iterating Relabel(false); // relabel the button to 'Start' ap.setRF(false); // stop the iteration loop in the main applet tg.reset(); // reinitialise the time graph bg.reset(); // reinitialise the bounce graph ap.showStatus("Graphs cleared, iteration process stopped and reset."); } private void Relabel(boolean Flag) { if(Flag) { // if the run flag is set a.setText("Stop"); // re-label the button 'Stop' ap.showStatus("Applet is iterating the equation..."); } else { // otherwise... a.setText("Start"); // re-label the button 'Start' ap.showStatus("Iteration process halted."); } } /* THIS METHOD IS ONLY CALLED BY THE LISTENER CLASS BELOW IF A CONTROL BUTTON HAS BEEN PRESSED */ void whichButton(int i) { if(i == 0) { // If 'Start/Stop' button has been pressed Relabel(RunFlag = !RunFlag); // start iterating the selected equation if(RunFlag) f.reset(); // if starting, initialise formula class ap.setRF(RunFlag); // update Run Flag state in main applet } else reset(); // Else, CLEAR button has been pressed, so clear } // graphs and reset 'x' to its start value. } // LISTENS FOR EVENTS FROM START/STOP AND CLEAR BUTTONS class stopbut implements ActionListener { int id; // one of the above events setbutt sb; // the application that called: always the above class /* Constructor for a new checkbox event: by setting the 'id' number pertaining to this instance and the reference to the instance of the class from which it came. (there will only be one instance). */ public stopbut(int id, setbutt sb) { this.id = id; this.sb = sb; } // an event has occurred from checkbox 'id' public void actionPerformed(ActionEvent e) { switch(id) { case 0: sb.whichButton(0); break; // stop/start button case 1: sb.whichButton(1); break; // clear button } } }