/** * Stop/Start/Reset Buttons for CtrlPanl.class re DifEqnAp.class * @author Robert J Morton * @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 of with the equation not iterating private int ls =0; // language switch: default = English private String s; // for multi-language setbutt(difeqnap ap, timegr tg, bouncegr bg, formula f, int ls) { this.ap = ap; // instance ref. of main applet class to local variable this.tg = tg; // instance ref. of timegr class to local variable this.bg = bg; // instance ref. of bouncegr class to local variable this.f = f; // instance ref. of formula class to local variable this.ls = ls; // language switch setLayout(null); // to allow the button panel to be laid out manually s = "Start"; // English/Portuguese if(ls != 0) s = "Iniciar"; a = new JButton(); // create the 'start' button a.setText(s); // annotate it with the word "Start" add(a); // add it to this panel a.setBounds(5,1,110,33); // set its position and size a.addActionListener( // add a listener new stopbut(0,this) // to listen for when it is pressed ); s = "Clear"; // English/Portuguese if(ls != 0) s = "Limpar"; b = new JButton(); // create the 'clear' button b.setText(s); // annotate it with the word "Clear" add(b); // add it to this panel b.setBounds(120,1,110,33); // set its position and size b.addActionListener( // add a listener new stopbut(1,this) // to listen for when it is pressed ); } void Stop() { //stop the iteration process Relabel(RunFlag = false); //clear the 'run' flag to stop iteration ap.setRF(false); //update the Run Flag state in main applet } 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 System.out.println("stbutt.java: graph cleared:"); System.out.println("iteration process stopped and reset."); } private void Relabel(boolean Flag) { if(Flag) { // if the run flag is set s = "Stop"; // English/Portuguese if(ls != 0) s = "Parar"; a.setText(s); // re-label the button 'Stop' System.out.println("setbutt.java:"); System.out.println("App is iterating the equation..."); } else { // otherwise... s = "Start"; // English/Portuguese if(ls != 0) s = "Iniciar"; a.setText(s); // re-label the button 'Start' System.out.println("setbutt.java: Stopped because"); System.out.println("the iteration process halted."); } } // only called by the listener class below void whichButton(int i) { // IF A CONTROL BUTTON HAS BEEN PRESSED if(i == 0) { // if 'Start/Stop' button has been pressed Relabel(RunFlag = !RunFlag); // start selected equation iterating if(RunFlag) f.reset(); // if starting, initialise formula class ap.setRF(RunFlag); // update the Run Flag state in main applet } else // if CLEAR button has been pressed reset(); // clear graphs and reset x to 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 public stopbut(int id, setbutt sb) { //constructor for a new checkbox event this.id = id; //s et id number pertaining to this instance this.sb = sb; // set the reference to the instance of the class } // from which it came. (there will only be one instance) //an event has occurred from checkbox 'id' public void actionPerformed(ActionEvent e) { switch(id) { case 0: sb.whichButton(0); break; // the stop/start button was pressed case 1: sb.whichButton(1); // the clear button was pressed } } }