/* * fudp.java - updated 7 Apr 05 - width 421, height 301 * @author jackord@kw.igs.net * updated for consistency with Liberty Basic version with eps option */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class fudp extends Applet implements ActionListener { int kk=0; // Declarations double gr, vt; String bs="Plot"; Button b=new Button(bs); public void init() { setBackground(new Color(211, 211, 211)); // Light Gray Background setLayout(new FlowLayout(FlowLayout.LEFT)); add(b); b.addActionListener(this); } double acc(double u) { // Newton's Law return -(1+u*Math.abs(u)/(vt*vt))*gr; } public void paint(Graphics g) { int x1, y1, x2, y2; double dt, vret, h, ym, t, y, v, dv; g.setColor(Color.black); g.drawRect(0, 0, 420, 300); if (kk==1) { gr=9.8; vt=30; dt=.01; h=0; // Feynman Algorithm t=0; y=0; v=vt; // ..no air resistance g.setColor(Color.red); x1=0; y1=300; v=v-gr*dt/2; do { t=t+dt; y=y+v*dt; v=v-gr*dt; x2=(int)(60*t); y2=(int)(6*(50-y)); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } while (y>0); t=0; y=0; v=vt; // ..air resistance g.setColor(Color.blue); x1=0; y1=300; dv=acc(v)*dt; v=v+dv/2; do { t=t+dt; y=y+v*dt; x2=(int)(60*t); y2=(int)(6*(50-y)); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; dv=acc(v+dv/2)*dt; v=v+dv; if (((v-dv)>0) && (v<=0)) { h=y; } } while (y>0); v=v-(.5+y/v/dt)*dv; vret=vt/Math.sqrt(2); // Analytic V return ym=vt*vt*Math.log(2)/2/gr; // Analytic Y max g.drawString("Initial speed = "+(float)vt, 60, 200); g.drawString("Return speed = "+(float)(-v),60, 215); g.drawString("Analytic ratio = "+(float)(-v/vret), 60, 230); g.drawString("Maximum height = "+(float)h, 60, 260); g.drawString("Analytic ratio = "+(float)(h/ym), 60, 275); } kk=0; } public void actionPerformed(ActionEvent e) { String tst; tst=e.getActionCommand(); if (bs.equals(tst)) { kk=1; } repaint(); } }