// SimApplet.java // This class provides a nice graphical user interface for the chaotic // dynamics simulations. import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; public class SimApplet extends Applet implements Runnable { public void init() { int i,j; left_panel=new Panel(); left_panel.setLayout(new GridLayout(8,1)); system_choice=new Choice(); system_choice.addItem("Harmonic Oscillator"); system_choice.addItem("Coupled Oscillator"); system_choice.addItem("Triple Oscillator"); left_panel.add(system_choice); method_choice=new Choice(); method_choice.addItem("Euler"); method_choice.addItem("RK2"); method_choice.addItem("RK4"); method_choice.addItem("Symplectic Order 1"); method_choice.addItem("Symplectic Order 2"); method_choice.addItem("Symplectic Order 4"); left_panel.add(method_choice); clear_button=new Button("Clear Section"); left_panel.add(clear_button); restart_button=new Button("Restart System"); left_panel.add(restart_button); section_button=new Button("Draw Section"); left_panel.add(section_button); lyap_label=new Label("Lyap=0.0"); left_panel.add(lyap_label); this.setLayout(new BorderLayout()); bottom_panel=new Panel(); bottom_panel.add(new Label("m1=")); m1=new TextField("1.0"); bottom_panel.add(m1); bottom_panel.add(new Label("m2=")); m2=new TextField("1.0"); bottom_panel.add(m2); bottom_panel.add(new Label("k1=")); k1=new TextField("1.0"); bottom_panel.add(k1); bottom_panel.add(new Label("k2=")); k2=new TextField("1.0"); bottom_panel.add(k2); bottom_panel.add(new Label("dt=")); dt=new TextField("0.03"); bottom_panel.add(dt); this.add("South",bottom_panel); this.add("West",left_panel); this.layout(); pendulum=createImage(80,480-bottom_panel.size().height); section=createImage(400-left_panel.size().width,(480-bottom_panel.size().height)/2); section.getGraphics().setColor(Color.black); section.getGraphics().fillRect(0,0,section.getWidth(this),section.getHeight(this)); energy=createImage(400-left_panel.size().width,(480-bottom_panel.size().height)/2); energy_vector=new Vector(101); thread=null; // Setup the system for Euler and regular Oscillator first_op=0; second_op=0; InitSystemState(); ready_to_run=true; ball_y=new int[3]; } public void start() { if(thread==null) { thread=new Thread(this); thread.start(); } } public void stop() { if(thread!=null) { thread.stop(); thread=null; } } public void run() { int i; try { while(true) { if(!ready_to_run) return; if(hold_ball==-1) { sys.Advance(); lyap.Advance(); lyap_label.setText("Lyap="+lyap.LyapMeasure()); } DrawSection(); DrawPendulum(); DrawEnergy(); repaint(); Thread.sleep(5); } } catch(InterruptedException e) { thread.stop(); thread=null; } } private void DrawPendulum() { if(pendulum==null) return; int i,y=0,y_prime=0; int draw_scale=20,default_length=100; Graphics g=pendulum.getGraphics(); int xmax,ymax; ball_x=40+left_panel.size().width; xmax=pendulum.getWidth(this); ymax=pendulum.getHeight(this); g.setColor(Color.black); g.fillRect(0,0,xmax,ymax); g.setColor(Color.red); for(i=0; i=0)) { x=(int)(((sv0+(last_sv0-sv0)*sv0/(last_sv2-sv2))+4.0)/8.0*xmax); y=(int)(((sv1+(last_sv1-sv1)*sv1/(last_sv2-sv2))+4.0)/8.0*ymax); g.drawLine(x,y,x,y); } last_sv0=sv0; last_sv1=sv1; last_sv2=sv2; } catch(System.OutOfRangeException e) {} } } private void DrawEnergy() { if(energy==null) return; Graphics g=energy.getGraphics(); int xmax,ymax; int i,lim; double x1,y1,x2,y2; xmax=energy.getWidth(this); ymax=energy.getHeight(this); g.setColor(Color.black); g.fillRect(0,0,xmax,ymax); g.setColor(Color.white); g.drawString("Energy",0,20); g.setColor(Color.cyan); energy_vector.addElement(new Double(rule.Energy(sys.GetStateVector()))); if(energy_vector.size()>100) { lim=energy_vector.size()/2; for(i=1; i