import java.awt.*; import java.util.*; import java.applet.Applet; import java.awt.event.*; public class orbits extends Applet implements Runnable, ActionListener{ // Applet for plotting orbits. Stefan Emet 1998. TextField TextField_parameter_a, TextField_parameter_b, TextField_X_0; Choice Choice_function; Button Button_Draw, Button_Continue, Button_Stop; Thread Thread_RUN; Panel Panel_top, Panel_right, Panel_right_param, Panel_bottom, Panel_X_0, Panel_Buttons; Label Label_top, Label_parameter_a, Label_parameter_b, Label_X_0; figure figure_plot; // class figure controls the plotting figure_text figure_text_f; // class Function_text updates the function texts boolean CLEAR_figure=true, DRAW_ORBIT, FIRST, CONTINUE; int ACTIVE_FUNCTION=0, t=0, // time: t x_pix_inc=30, x_pix_ind=0, run=0,click=0; final static int COSINE=0,NONLINEAR=1,LOGISTIC=2,LINEAR=3; double a=0.5, b=1, X_0=0, X_t; // system: X_t, X_0 String[] Function = {"Cos( X(t) )","0.5 ( X(t)^3 + X(t) )", "a X(t) ( 1-X(t) )","a X(t) + b"}; public void init() { setLayout(new BorderLayout(5,5)); // sets the main-border Panel_top = new Panel(); Label_top = new Label("The forward-orbit of a chosen map"); Panel_top.add(Label_top); add("North",Panel_top); Panel_right = new Panel(); Panel_right.setLayout(new GridLayout(0,1,5,5)); Panel_right.add(new Label("Choose function")); Choice_function = new Choice(); // allocate memory for the Choice_function.addItem("cos(x)"); Choice_function.addItem(".5(x^3+x)"); Choice_function.addItem("a x (1-x)"); Choice_function.addItem("a x + b"); //Choice_function.addActionListener(this); Panel_right.add(Choice_function); Panel_right_param = new Panel(); Panel_right_param.setLayout(new GridLayout(2,2,3,3)); Panel_right_param.add(Label_parameter_a = new Label("a :")); Panel_right_param.add(TextField_parameter_a = new TextField(""+a,3)); Panel_right_param.add(Label_parameter_b = new Label("b :")); Panel_right_param.add(TextField_parameter_b = new TextField(""+b,3)); Panel_right.add(Panel_right_param); Panel_right.add(new Label(" ")); add("East",Panel_right); // puts the Choice-menu and parameter-field to the right Panel_bottom = new Panel(); Panel_bottom.setLayout(new GridLayout(1,0,5,5)); // 1-row figure_text_f = new figure_text(this); Panel_bottom.add(figure_text_f); Panel_X_0 = new Panel(); Panel_X_0.add(new Label("X(0) :")); Panel_X_0.add(TextField_X_0 = new TextField(""+X_0,3)); Panel_bottom.add(Panel_X_0); Panel_Buttons = new Panel(); Panel_Buttons.setLayout(new GridLayout(1,0,3,3)); Panel_Buttons.add(Button_Draw= new Button("Draw")); Panel_Buttons.add(Button_Stop= new Button("Stop")); Panel_Buttons.add(Button_Continue= new Button("Continue")); Button_Draw.addActionListener(this); Button_Stop.addActionListener(this); Button_Continue.addActionListener(this); Panel_bottom.add(Panel_Buttons); add("South",Panel_bottom); figure_plot = new figure(this); add("Center", figure_plot); } public double f(double x){ switch (ACTIVE_FUNCTION) { case COSINE: return Math.cos(x); case NONLINEAR: return 0.5*(Math.pow(x,3)+x); case LOGISTIC: return a*x*(1-x); case LINEAR: return a*x+b; } return 0; } public void run(){ t=0; x_pix_ind=0; X_t = X_0 = Double.valueOf(TextField_X_0.getText()).doubleValue(); a = Double.valueOf(TextField_parameter_a.getText()).doubleValue(); b = Double.valueOf(TextField_parameter_b.getText()).doubleValue(); FIRST = true; while (DRAW_ORBIT){ if (x_pix_ind % x_pix_inc == 0){ X_t=f(X_t); t++; } figure_plot.repaint(); try { Thread.sleep(50);} // try{} catch -block slows down the drawings catch ( InterruptedException e ) {} x_pix_ind++; } } public void start(){ if (Thread_RUN==null){ Thread_RUN=new Thread(this); } Thread_RUN.start(); // calls the run -method } public void stop(){ Thread_RUN.stop(); DRAW_ORBIT=false; Thread_RUN=null; // the thread to null } /*public boolean lostFocus(Event e, Object arg){ showStatus("Lost !"); return false; } public boolean gotFocus(Event e, Object arg){ showStatus("Got Focus !"); return false; } */ public void actionPerformed(ActionEvent e){ Object source=e.getSource(); if (source.equals(Choice_function)){ ACTIVE_FUNCTION=Choice_function.getSelectedIndex(); set_CLEAR(true); DRAW_ORBIT=false; figure_text_f.repaint(); } else if(source.equals(Button_Draw)){ stop(); set_CLEAR(true); DRAW_ORBIT=true; Button_Draw.setEnabled(false); Button_Continue.setEnabled(false); Button_Stop.setEnabled(true); start(); } else if(source.equals(Button_Stop)){ Button_Draw.setEnabled(true); Button_Continue.setEnabled(true); Button_Stop.setEnabled(false); Thread_RUN.suspend(); } else if(source.equals(Button_Continue)){ Button_Continue.setEnabled(false); Button_Stop.setEnabled(true); Thread_RUN.resume(); } } public void set_CLEAR(boolean set){ CLEAR_figure=set; } public void set_FIRST(boolean set){ FIRST=set; } public void set_x_pix_ind(int set){ x_pix_ind=set; } } // class orbits class figure extends Canvas { orbits orbit; double y_pix_inc; int figure_WIDTH, figure_HEIGHT, AXES_WIDTH, AXES_HEIGHT, AXES_x0=15, AXES_y0=20, ii=1, x_pix, y_pix, y_pix_a, y_pix_b; figure(orbits pointer){ super(); orbit = pointer; } public void paint(Graphics g){ figure_WIDTH=getSize().width; // the width of the Plot-window figure_HEIGHT=getSize().height; // the height of the Plot-window AXES_WIDTH=figure_WIDTH-29; // width of the axes in the Plot-window AXES_HEIGHT=figure_HEIGHT-39; // height of the axes in the Plot-window update(g); } public void update(Graphics g){ if (orbit.CLEAR_figure){ orbit.set_CLEAR(false); g.setColor(Color.white); g.fillRect(0,0,figure_WIDTH,figure_HEIGHT); g.setColor(Color.black); g.drawRect(0,0,figure_WIDTH-1,figure_HEIGHT-1); axis(g); } if (orbit.FIRST){ orbit.set_FIRST(false); x_pix=AXES_x0; y_pix=y_pix_a=y_pix_b=y_pixel(orbit.X_0); } if (orbit.DRAW_ORBIT){ // draw the orbit one step forward draw(g); if (orbit.x_pix_ind % AXES_WIDTH == 0){ orbit.set_CLEAR(true); x_pix=AXES_x0; } else x_pix++; } } // update public void axis(Graphics g){ g.drawRect(AXES_x0,AXES_y0,AXES_WIDTH,AXES_HEIGHT); g.drawString("X(t)",AXES_x0+1,AXES_y0-3);g.drawString("t",AXES_x0+AXES_WIDTH+2,AXES_y0+AXES_HEIGHT+10); switch (orbit.ACTIVE_FUNCTION) { case orbit.COSINE: g.drawString("1",AXES_x0-8,AXES_y0+4); g.drawLine(AXES_x0-2,AXES_y0,AXES_x0+2,AXES_y0); g.drawString("0",AXES_x0-8,AXES_y0+(int)(0.5*AXES_HEIGHT)+4); g.drawLine(AXES_x0-2,AXES_y0+(int)(0.5*AXES_HEIGHT),AXES_x0+2,AXES_y0+(int)(0.5*AXES_HEIGHT)); g.drawString("-1",AXES_x0-10,AXES_y0+AXES_HEIGHT+4); g.drawLine(AXES_x0-2,AXES_y0+AXES_HEIGHT,AXES_x0+2,AXES_y0+AXES_HEIGHT); break; case orbit.NONLINEAR: g.drawString("+Inf",AXES_x0-15,AXES_y0+5); g.drawString("1",AXES_x0-8,AXES_y0+(int)(0.2*AXES_HEIGHT)+4); g.drawLine(AXES_x0-2,AXES_y0+(int)(0.2*AXES_HEIGHT),AXES_x0+2,AXES_y0+(int)(0.2*AXES_HEIGHT)); g.drawString("0",AXES_x0-8,AXES_y0+(int)(0.5*AXES_HEIGHT)+4); g.drawLine(AXES_x0-2,AXES_y0+(int)(0.5*AXES_HEIGHT),AXES_x0+2,AXES_y0+(int)(0.5*AXES_HEIGHT)); g.drawString("-1",AXES_x0-10,AXES_y0+(int)(0.8*AXES_HEIGHT)+4); g.drawLine(AXES_x0-2,AXES_y0+(int)(0.8*AXES_HEIGHT),AXES_x0+2,AXES_y0+(int)(0.8*AXES_HEIGHT)); g.drawString("-Inf",AXES_x0-15,AXES_y0+AXES_HEIGHT+5); break; case orbit.LOGISTIC: g.drawString("1",AXES_x0-8,AXES_y0+(int)(0.2*AXES_HEIGHT)+4); g.drawLine(AXES_x0-2,AXES_y0+(int)(0.2*AXES_HEIGHT),AXES_x0+2,AXES_y0+(int)(0.2*AXES_HEIGHT)); g.drawString("0",AXES_x0-8,AXES_y0+(int)(0.8*AXES_HEIGHT)+4); g.drawLine(AXES_x0-2,AXES_y0+(int)(0.8*AXES_HEIGHT),AXES_x0+2,AXES_y0+(int)(0.8*AXES_HEIGHT)); g.drawString("-Inf",AXES_x0-15,AXES_y0+AXES_HEIGHT+5); break; case orbit.LINEAR: break; } } public void draw(Graphics g){ if ((orbit.x_pix_ind % orbit.x_pix_inc)==0){ y_pix_a=y_pix_b; // update the y_points y_pix_b=y_pixel(orbit.X_t); // the next point y_pix_inc=(double)(y_pix_b-y_pix_a)/(orbit.x_pix_inc); cross(g,x_pix,y_pix_a); // put a cross at the point (t,X(t)) g.drawLine(x_pix,AXES_y0+AXES_HEIGHT-2,x_pix,AXES_y0+AXES_HEIGHT+2); g.drawString(""+(orbit.t-1), x_pix-3,AXES_y0+AXES_HEIGHT+13); } else{ g.drawLine(x_pix,y_pix,x_pix+1,y_pix_a+(int)((orbit.x_pix_ind % orbit.x_pix_inc)*y_pix_inc)); y_pix=y_pix_a+(int)((orbit.x_pix_ind % orbit.x_pix_inc)*y_pix_inc); } } public void cross(Graphics g, int x, int y){ g.setColor(Color.red); g.drawLine(x-1,y-1,x+1,y+1); g.drawLine(x-1,y+1,x+1,y-1); g.setColor(Color.black); } public int y_pixel(double in){ int y_pix_value=0; switch (orbit.ACTIVE_FUNCTION) { case orbit.COSINE: y_pix_value = (int)(0.5*AXES_HEIGHT*(1-in)); break; case orbit.NONLINEAR: y_pix_value = (in > 1) ? (int)(AXES_HEIGHT*0.18*(1-Math.min((double)(in/100000),1))) :( (in < -1) ? (int)(AXES_HEIGHT*(0.82+0.18*(Math.min((double)(-in/100000),1.0)))) : (int)(AXES_HEIGHT*(0.2+0.3*(1-in)))); break; case orbit.LOGISTIC: y_pix_value = (in > 1) ? (int)(AXES_HEIGHT*0.2*(1-Math.min((double)(in/10),1))) :( (in < 0) ? (int)(AXES_HEIGHT*(0.82+0.18*(Math.min((double)(-in/100000),1.0)))) : (int)(AXES_HEIGHT*(0.2+0.6*(1-in)))); break; case orbit.LINEAR: break; } return (AXES_y0+y_pix_value); } } // class figure class figure_text extends Canvas { orbits orbit; figure_text(orbits pointer){ super(); orbit = pointer; } public void paint(Graphics g){ g.drawString("X(t+1) = "+orbit.Function[orbit.ACTIVE_FUNCTION],10,20); } } // class figure_text