/** * Text entry field and + - adjustment buttons for c for CtrlPanl.class re DifEqnAp.class * @author Robert John Morton * @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 int ls = 0; // language switch: 0=English 1=Portuguese private String s; // for multi-language 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, int ls) { this.sb = sb; // instance ref of setbutt panel to local variable this.F = F; // instance ref of formula class to local variable this.ls = ls; // language switch setLayout(null); // NOTE: this panel is 100 by 50 pixels setBackground(pc); // set panel colour s = "Adjust c"; // English/Portuguese if(ls != 0) s = "Ajustar c"; L = new JLabel(s); // create the panel's title label add(L); // add it to this panel L.setBounds(0,0,70,25); // set its position & size within the panel a = new JButton(); // create the + button a.setText("+"); // annotate the button with a + symbol add(a); // add it to this panel a.setBounds(95, 0, 30, 24); // set its position & size within the panel a.setMargin( new Insets(3,3,3,3) // set button's annotation margins to minimum ); a.addActionListener( new cbut(0, this) // create a listener for it ); b = new JButton(); // create the - button b.setText("-"); // annotate the button with a - symbol add(b); // add it to this panel b.setBounds(95,27,30,24); // set its position & size within the panel b.setMargin( new Insets(3,3,3,3) // set button's annotation margins to minimum ); b.addActionListener( new cbut(1, this) // create a listener for it ); t = new JTextField(); // create the text entry field add(t); // add it to this panel t.setBounds(0,25,90,25); // set its position & size within the panel t.setFont( // set font for numbers in text field new Font("Sans",Font.BOLD,14) ); t.addActionListener( new cbut(3,this) // create a listener for it ); t.setText(String.valueOf(c)); // display initial value of c in text field } 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 to string if(s.length() > 9) // if string is more than 9 characters s = s.substring(0,9); // chop its end off after the 9th character t.setText(s); // display c in the text field sb.reset(); // 'hit' the reset button } // EVENT HANDLERS ONLY CALLED BY THE LISTENER CLASS BELOW void incC(int i) { c += D[i]; // add the increment to current value of c CheckLimits(); // ensure it is still within graph limits } void setC() { double x = c; // preserve the original value of c /* Try to parse a number from the entered text by creating a 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 cont- ained 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 public cbut(int id, cval cv) { this.id = id; // set id number of the event type this.cv = cv; // set the reference to the instance of the above class } // from which it came. (there will only be one instance) //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(); // C/R from text field } } }