/** * Text entry field and + - adjustment buttons for 'c' for CtrlPanl.class re DifEqnAp.class * @author Robert John Morton UK-YE572246C * @version 27 November 1997 modified 07 August 2009, 18 April 2012 */ /* This class contains a text entry field in which the user may enter a new value for the constant c in the currently selected difference equation. It also contains two buttons + and - for incrementing and decrementing the current value of the constant c in the selected difference equation.*/ 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 cval extends JPanel { private setbutt sb; // instance reference to Start/Stop/Reset panel private formula F; // instance reference to the formula class private JLabel L; // label for the text field private JTextField t; // text field to hold and set c private JButton a, b; // 2 button id's and references private double c = 3.68; // value for access by main panel private static final double D[] = {+0.1, -0.1}; // increment/decrement amounts private boolean EqnFlag = false; // which equation is in force cval(setbutt sb, formula F, Color pc) { this.sb = sb; // instance ref of setbutt panel to local variable this.F = F; // instance ref of formula class to local variable setLayout(null); // NOTE: this panel is 100 by 50 pixels setBackground(pc); // set panel colour /* Create an instance of the panel's title label, add it to this panel, then set its position within the panel and its size. */ L = new JLabel("Adjust c"); add(L); L.setBounds(0, 0, 70, 25); /* Create an instance of the '+' button, label it, add it to this panel, set its position within the panel and its size, set button's annotation margins to minimum and create a listener to detect when it has been pressed. */ a = new JButton(); a.setText("+"); add(a); a.setBounds(95, 0, 30, 24); a.setMargin(new Insets(3,3,3,3)); a.addActionListener(new cbut(0,this)); /* Create an instance of the '-' button, label it, add it to this panel, set its position within the panel and its size, set button's annotation margins to minimum and create a listener to detect when it has been pressed. */ b = new JButton(); b.setText("-"); add(b); b.setBounds(95, 27, 30, 24); b.setMargin(new Insets(3,3,3,3)); b.addActionListener(new cbut(1,this)); /* Create an instance of text entry field, add it to this panel, set its position within the panel and its size, create and set a font for its text and create a listener to detect when a carriage- return has been entered. */ t = new JTextField(); add(t); t.setBounds(0, 25, 90, 25); t.setFont(new Font("Sans", Font.BOLD, 14)); t.addActionListener(new cbut(3, this)); // display the initial value of c in the text field t.setText(String.valueOf(c)); } void setEF(boolean b) {EqnFlag = b;} //set which equation is to be used void initCval() { // TO SET c DEFAULT VALUES [also called by seleqn] if(EqnFlag) // if using x = x**2 + c c = -1.5; // set appropriate default value for c else // else x = cx(1 - x) is in use c = +3.68; // set appropriate default value for c ShowReset(); // see below } private void CheckLimits() { // ENSURE c IS WITHIN GRAPH LIMITS if(EqnFlag) { // if using x = x**2 + c if(c > +2.5) c = +2.5; // set appropriate limits for c if(c < -2.5) c = -2.5; } else { // else if using x = cx(1 - x) if(c > 4.1) c = 4.1; // set appropriate limits for c if(c < 0.1) c = .1; } ShowReset(); // see below } private void ShowReset() { // for local code economy F.setC(c); // update the c-value in the formula class String s = String.valueOf(c); // convert c-value to string /* If string is more than 9 characters, chop its end off after the 9th character. */ if(s.length() > 9) s = s.substring(0,9); t.setText(s); // display c in the text field sb.reset(); // 'hit' the reset button } /* EVENT HANDLERS ONLY CALLED BY THE LISTENER CLASS BELOW Add the increment to current value of 'c' and ensure that it is still within the limits of the graph. */ void incC(int i) { c += D[i]; CheckLimits(); } void setC() { // SET THE VALUE OF 'C' THAT WAS TYPED INTO THE TEXT FIELD double x = c; // preserve the original value of c /* Try to parse a number from the entered text by creating a Double Double object of the numeric value of the string in the text field then extracting its value as an ordinary double. */ try { c = Double.valueOf(t.getText()).doubleValue(); } /* If a valid number cannot be parsed from the text currently con- tained in the field, put the original value of c back in text field. */ catch(NumberFormatException f) { c = x; } CheckLimits(); // ensure entered value was within limits } } // LISTENS FOR THE + - BUTTONS & C/R FROM TEXT FIELD class cbut implements ActionListener { int id; // one of the above events cval cv; // the application that called: always the above class /* Constructor for a new checkbox event: sets 'id' number of the event type and the reference to the instance of the above class from which it came. (there will only be one instance). */ public cbut(int id, cval cv) { this.id = id; this.cv = cv; } // a checkbox event has occurred from checkbox 'id' public void actionPerformed(ActionEvent e) { switch(id) { case 0: cv.incC(0); break; // + button pressed case 1: cv.incC(1); break; // - button pressed case 2: cv.setC(); break; // C/R from text field } } }