/* bifurcaton diagram for f(x) = a * x * (1-x) */ // // bifurcation.java // bifurcation // // Created by me on Wed Mar 19 2003. import java.awt.*; import java.applet.*; import java.awt.event.*; public class bifurcation extends Applet { BifAnim anim=null; public static double amn, amx, xmn, xmx; public static int aDiv, iStart, nIt; double a,x; int x0,y0,x1,y1; int i,ai; Point pressed; static Button ssButton; static nCanvas pic; static TextField aMin; static TextField aDivision; static TextField nStart; static TextField aMax; static TextField nIteration; Label label1; Label label2; Label label3; Label label4; Label label5; Label label6; static TextField xMin; Label label7; static TextField xMax; Label label8; static TextField aCurr; Label label9; static TextField xCurr; public void init() { setLayout(null); resize(630,470); setBackground(new Color(0.8F, 0.8F, 0.8F)); pic = new nCanvas(); pic.setBounds(106,9,514,451); pic.setBackground(new Color(1.0F, 1.0F, 1.0F)); pic.addMouseMotionListener(new mmaCanvas()); pic.addMouseListener(new maCanvas()); add(pic); pic.makeBuffer(); aMin = new TextField(); aMin.setText("2.9"); aMin.reshape(13,30,82,24); aMin.setBackground(Color.white); add(aMin); aMax = new TextField(); aMax.setText("4.0"); aMax.reshape(13,82,82,24); aMax.setBackground(Color.white); add(aMax); aDivision = new TextField(); aDivision.setText("1000"); aDivision.reshape(13,134,82,24); aDivision.setBackground(Color.white); add(aDivision); xMin = new TextField(); xMin.setText("0.0"); xMin.reshape(13,188,82,24); xMin.setBackground(Color.white); add(xMin); xMax = new TextField(); xMax.setText("1.0"); xMax.reshape(13,238,82,24); xMax.setBackground(Color.white); add(xMax); nStart = new TextField(); nStart.setText("1000"); nStart.reshape(13,295,82,24); nStart.setBackground(Color.white); add(nStart); nIteration = new TextField(); nIteration.setText("100"); nIteration.reshape(13,343,82,24); nIteration.setBackground(Color.white); add(nIteration); aCurr = new TextField(); aCurr.reshape(20,384,75,17); aCurr.setBackground(Color.white); add(aCurr); xCurr = new TextField(); xCurr.reshape(20,404,75,17); xCurr.setBackground(Color.white); add(xCurr); ssButton = new Button("start"); ssButton.reshape(35,434,60,23); //ssButton.setBackground(new Color(1.0F, 1.0F, 1.0F)); ssButton.addMouseListener(new maButton()); add(ssButton); label1 = new Label("a:Min"); label1.reshape(6,7,78,20); add(label1); label2 = new Label("a:Max"); label2.reshape(6,59,78,20); add(label2); label3 = new Label("a:Division"); label3.reshape(6,111,78,20); add(label3); label4 = new Label("n:Start"); label4.reshape(6,274,78,20); add(label4); label5 = new Label("n:Iteration"); label5.reshape(6,322,78,20); add(label5); label6 = new Label("x:Min"); label6.reshape(6,165,51,22); add(label6); label7 = new Label("x:Max"); label7.reshape(6,215,51,22); add(label7); label8 = new Label("a"); label8.reshape(9,381,10,22); add(label8); label9 = new Label("x"); label9.reshape(9,401,10,22); add(label9); } // MouseAdapter for button class maButton extends MouseAdapter{ public void mouseClicked(MouseEvent e){ ssButton_Clicked(); } } void ssButton_Clicked() { if(ssButton.getLabel()=="start"){ anim=new BifAnim(); anim.myStart(); } else { anim.myStop(); } } // MouseAdapter for Canvas class maCanvas extends MouseAdapter{ public void mousePressed(MouseEvent e){ pic_MousePressed(e); } public void mouseReleased(MouseEvent e){ pic_MouseReleased(e); } } void pic_MousePressed(MouseEvent event) { if(anim.isAlive()) anim.myStop(); pressed=event.getPoint(); } void pic_MouseReleased(MouseEvent event) { Dimension ps = pic.getSize(); Point p0=pressed; Point p1=event.getPoint(); int tmp; int x0,x1,y0,y1; x0=Math.min(p0.x,p1.x); x1=Math.max(p0.x,p1.x); y0=Math.min(p0.y,p1.y); y1=Math.max(p0.y,p1.y); if(x0!=x1 && y0!=y1){ //getNumbers(); aMin.setText(Double.toString(amn+((double)x0)/((double)ps.width)*(amx-amn))); aMax.setText(Double.toString(amn+((double)x1)/((double)ps.width)*(amx-amn))); xMin.setText(Double.toString(xmx-((double)y1)/((double)ps.height)*(xmx-xmn))); xMax.setText(Double.toString(xmx-((double)y0)/((double)ps.height)*(xmx-xmn))); } } // MouseMotionAdapter for Canvas class mmaCanvas extends MouseMotionAdapter{ public void mouseMoved(MouseEvent e){ pic_MouseMoved(e); } public void mouseDragged(MouseEvent e){ pic_MouseDragged(e); } } void pic_MouseMoved(MouseEvent event){ Dimension ps = pic.getSize(); Point tmp=event.getPoint(); //getNumbers(); aCurr.setText(Double.toString(amn+((double)tmp.x)/((double)ps.width)*(amx-amn))); xCurr.setText(Double.toString(xmx-((double)tmp.y)/((double)ps.height)*(xmx-xmn))); } void pic_MouseDragged(MouseEvent event) { Dimension ps; int tx0,tx1,ty0,ty1,tmp; //temporary variables tx0=pressed.x; ty0=pressed.y; tx1=event.getX(); ty1=event.getY(); if(tx0>tx1){tmp=tx0; tx0=tx1; tx1=tmp; } if(ty0>ty1){tmp=ty0; ty0=ty1; ty1=tmp; } if((tx0!=tx1) && (ty0!=ty1)){ Graphics gtmp; pic.repaint(); gtmp=pic.getGraphics(); gtmp.setColor(new Color(1.0F,0.0F,0.0F)); gtmp.drawRect(tx0,ty0,tx1-tx0,ty1-ty0); } } static void getNumbers(){ double tmp; try{ iStart=Integer.parseInt(nStart.getText()); }catch(NumberFormatException e){ nStart.setText("1000"); iStart=1000; }; try{ nIt=Integer.parseInt(nIteration.getText()); }catch(NumberFormatException e){ nIteration.setText("100"); nIt=100; }; try{ aDiv=Integer.parseInt(aDivision.getText()); }catch(NumberFormatException e){ aDivision.setText("1000"); aDiv=1000; }; try{ amn=Double.valueOf(aMin.getText()).doubleValue(); }catch(NumberFormatException e){ aMin.setText("2.9"); amn=2.9; }; try{ amx=Double.valueOf(aMax.getText()).doubleValue(); }catch(NumberFormatException e){ aMax.setText("4.0"); amx=4.0; }; try{ xmn=Double.valueOf(xMin.getText()).doubleValue(); }catch(NumberFormatException e){ xMin.setText("0.0"); xmn=0.0; }; try{ xmx=Double.valueOf(xMax.getText()).doubleValue(); }catch(NumberFormatException e){ xMax.setText("1.0"); xmx=1.0; }; if(amn>amx){ tmp=amn; amn=amx; amx=tmp;} if(xmn>xmx){ tmp=xmn; xmn=xmx; xmx=tmp;} } /* public void bifurc(){ anim = new BifAnim(); anim.start(); } public double f (double a, double x){ return(a*x*(1-x)); } */ }