/** * Iterated Difference Equation Demonstrator Applet * @author Robert J Morton * @version 27 November 1997 modified 07 August 2009, 18 April 2012 */ /* Iterates the two difference equations x = cx(1 - x) and x = x**2 + c according to which one the user selects on the control panel. Provides real-time Time Line and Bounce graphs so the user can see the equations being iterated. This applet employs separate dedicated TimeGr and BounceGr classes to implement real-time graph plotting. Its control panel is provided by a dedicated CtrlPanl class which is a container for separate dedicated classes SelPer to select the iteration period, SelEqn to select the desired equation to iterate, SelScan for selecting between single-scan mode continuous scanning mode, SetCval to provide a text entry field for the constant c in the equations, IncCval provides + - buttons to increment/ decrement c by 0.1 per hit, and SetButt which contains Start/Stop and Reset buttons. There is also a class SelPlot which allows you to select between having the Time Graph plot by joining successive plots with straight lines or having it mark each spot value of x with a short horizontal line equal in length to one time period. */ import java.awt.*; import javax.swing.*; import java.awt.image.BufferedImage; public class difeqnap extends JPanel implements Runnable { private static final Color bc = new Color(245,245,240), // panel background colour BC = new Color(238,238,238); // applet background colour private boolean RunFlag=false, // applet starts with no equation iterating INIT=true, // in-loop initialisation not yet done AutoStart = true; // auto-start required private int XE, // horizontal extent of window and JFrame. YE, // vertical extent of window and JFrame. XM = 10, // X-margin within frame YM = 12, // Y-margin within frame delay = 20, // initial three-second delay before autostart ls = 0; // English/Portuguese switch: default English private long P = 100, // inter-plot time frame (milliseconds) 100ms t; // system time at which a new plot is due to begin private BufferedImage TG = null, // reference for a Time Graph image object BG = null; // reference for a Bounce Graph image object private Graphics2D ga, // graphics reference for off-screen image gb; // graphics reference for off-screen image private formula f; // instance reference to the formula class private timegr tg; // reference for a Time Graph object private bouncegr bg; // reference for a Bounce Graph object private setbutt sb; // instance reference to the setbutt class private Thread T; // thread reference variable for this program private Font font = new Font("Sans",Font.BOLD,12); private FontMetrics fm; // dimensions of the above font public difeqnap(int XE, int YE, int ls) { this.XE = XE; // horizontal [X] dimension of the JPanel this.YE = YE; // vertical [Y] dimension of the JPanel this.ls = ls; // set the language swich position setBounds(0,0,XE,YE); // the JPanel fills the whole of the JFrame area setLayout(null); // JPanel has free-form layout [for graph] f = new formula(this); // create an instance of the formula class /* Create image object on which to draw Time Graph axes and get its graphics context. */ TG = new BufferedImage(545,180,BufferedImage.TYPE_INT_RGB); ga = TG.createGraphics(); /* Create the Time Graph panel, add it to the applet's window, morph it to bottom left corner of applet window and set its background colour. */ tg = new timegr(this,bc,f,ga,font,TG);//CREATE TIME-GRAPH PANEL add(tg); tg.setBounds(XM,YM,545,180); tg.setBackground(bc); /* Create image object on which to draw Bounce Graph axes and get its graphics context. */ BG = new BufferedImage(185,185,BufferedImage.TYPE_INT_RGB); gb = BG.createGraphics(); /* Create Bounce Graph panel, add it into the applet's window, morph it to bottom left corner of applet window and set its background colour. */ bg = new bouncegr(bc,f,gb,BG,font); add(bg); bg.setBounds(XM+360,YM+180,185,185); bg.setBackground(bc); f.setBG(bg); // pass instance reference of bounce graph to formula class /* Create the Control Panel, add it to the applet's window, morph it into bottom left corner of applet window, then set its background colour. */ ctrlpanl CP = new ctrlpanl(this,tg,bg,f,ls); add(CP); CP.setBounds(XM,YM+180,360,185); CP.setBackground(bc); T=new Thread(this); // Create a program thread T.start(); // and start it running // Set the system time at which first plot's iteration period will end. t = System.currentTimeMillis() + P; } void setSB(setbutt sb){this.sb=sb;} // instance reference to button panel void setPeriod(long p){P=p;} // set the formula iteration period void setRF(boolean b){RunFlag=b;} // set the Run() state public void run(){ // run the Track Recorder painting thread while(true){ // permanent loop runLoop(); // see below // get the time left in this plot's time-frame long s = t - System.currentTimeMillis(); if(s < 5) s = 5; // in case machine isn't fast enough try{Thread.currentThread().sleep(s); // sleep for the time remaining } catch(InterruptedException e){ } // catch interrupt from GUI // set system time at which next plot's time frame will end t = System.currentTimeMillis() + P; } } private void runLoop() { if(INIT){ // if in-loop initialisation has not yet been done if(delay-- < 0){ // if decremented delay has expired bg.reset(); // initialise the bounce-graph tg.reset(); // initialise the time-graph delay=20; // reinitialise the delay INIT=false; // signal that initialisation pass has been done } return; // bail out } if(AutoStart){ // if not yet automatically started if(delay-- < 0){ // if decremented delay has expired sb.whichButton(0); // hit the 'Start' button AutoStart=false; // signal that auto start has occurred } return; // bail out } if(RunFlag) { // if the iteration process is running f.iterate(); // update the value of x bg.Plot(); // paint next plot on the bounce graph tg.Plot(); // paint next plot on the time graph } } }