/* * fgun.java - revised 13 Oct Apr 05 - width 451, height 271 * @author jackord@kw.igs.net * a version of fhomer.java modified to deal with the variation of density * with elevation in the atmosphere * finds the initial velocity required to give 40 cm diameter iron sphere * a horizontal range of 40 km if there is * (a) no atmosphere * (b) a constant density atmosphere * (c) a variable density atmosphere */ import java.applet.Applet; import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class fgun extends Applet implements ActionListener { int ntry, show, dens; int kk=0; int rr=0; int first=0; // Declarations double gr, vi, vo, phi, dphi, k, t, tt, h; String b1s="Range"; Button b1=new Button(b1s); String b2s="Motion"; Button b2=new Button(b2s); Image aim, bim; Graphics agr, bgr; public void init() { setLayout(new FlowLayout(FlowLayout.LEFT)); add(b1); add(b2); b1.addActionListener(this); b2.addActionListener(this); setBackground(new Color(211, 211, 211)); // Light Gray Background } double hit(Graphics g) { // Feynman Algorithm double x, y, q, vx, vy, dvx, dvy, dt, u, ux, uy, cc, xx, yy, vxx, vyy; int x1, y1, x2, y2, xx1, yy1, xx2, yy2, r; dt=.1; t=0; x=0; y=0; q=phi*Math.PI/180; vx=vi*Math.cos(q); vy=vi*Math.sin(q); x1=0; y1=250; xx1=x1; yy1=y1; r=6; if (kk==1) { // Find Range if (dens==0) { // No Air Resistance vy=vy-gr*dt/2; do { t=t+dt; x=x+vx*dt; y=y+vy*dt; x2=(int)(x/100); y2=(int)(250-y/100); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; vy=vy-gr*dt; } while (y>0); } if (dens>0) { // Sea-Level or dvx=-k*vx*vi*dt; vx=vx+dvx/2; // variable density dvy=-(gr+k*vy*vi)*dt; vy=vy+dvy/2; // of air... do { t=t+dt; x=x+vx*dt; y=y+vy*dt; if (show==1) { x2=(int)(x/100); y2=(int)(250-y/100); g.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; } ux=vx+dvx/2; uy=vy+dvy/2; u=Math.sqrt(ux*ux+uy*uy); if (dens==1) { // ...choose which cc=1; } else { cc=Math.exp(-y/h); } dvx=-cc*k*ux*u*dt; vx=vx+dvx; // Newton's Law dvy=-(gr+cc*k*uy*u)*dt; vy=vy+dvy; } while (y>0); } } if (kk==2) { // Plot Motion xx=0; yy=0; vxx=vo*Math.cos(Math.PI/4); vyy=vxx; tt=2*vyy/gr; dvx=-k*vx*vi*dt; vx=vx+dvx/2; dvy=-(gr+k*vy*vi)*dt; vy=vy+dvy/2; do { t=t+dt; x=x+vx*dt; y=y+vy*dt; // With air if (y<0) { x=x-vx*y/vy; y=0; } xx=vxx*t; yy=vyy*t-gr*t*t/2; // Without air x2=(int)(x/100); y2=(int)(250-y/100); xx2=(int)(xx/100); yy2=(int)(250-yy/100); bgr.drawLine(x1, y1, x2, y2); x1=x2; y1=y2; if (yy>=0) { bgr.drawLine(xx1, yy1, xx2, yy2); xx1=xx2; yy1=yy2; } agr.drawImage(bim, 0, 0, null); agr.fillArc(x2-r, y2-r, 2*r, 2*r, 0, 360); // Show... agr.fillArc(xx1-r, yy1-r, 2*r, 2*r, 0, 360); // ...cannonballs g.drawImage(aim, 0, 0, null); cc=Math.exp(-y/h); // Newton's Law ux=vx+dvx/2; uy=vy+dvy/2; u=Math.sqrt(ux*ux+uy*uy); dvx=-cc*k*ux*u*dt; vx=vx+dvx; dvy=-(gr+cc*k*uy*u)*dt; vy=vy+dvy; } while (y>0); } return x; } double ang(Graphics g) { // Find Optimum Angle double dph, x, xo; dph=dphi; x=hit(g); do { phi=phi+dph; xo=x; x=hit(g); if (x.02); phi=phi+2*dph; ntry=ntry+1; if (ntry<5) { show=1; x=hit(g); show=0; } return xo; } public void paint(Graphics g) { // Main Program if (first==0) { aim=createImage(451, 271); // Animation buffers agr=aim.getGraphics(); bim=createImage(451, 271); bgr=bim.getGraphics(); first=1; } agr.setColor(Color.red); bgr.setColor(Color.black); for (int i=0; i<=450; i=i+50) { bgr.drawLine(i, 0, i, 250); } for (int i=0; i<=250; i=i+50) { bgr.drawLine(0, i, 450, i); } bgr.setColor(Color.blue); g.drawImage(bim, 0, 0, null); g.setColor(Color.black); g.setFont(new Font("TimesRoman", Font.PLAIN, 14)); double m, r, va, vb, dvi, range; r=.2; m=4*Math.PI/3*7870*Math.pow(r, 3); gr=9.8; k=.87*r*r/m; range=40000; h=100000/9.8/1.21; vo=Math.sqrt(gr*range); if (kk==1) { // Find the Range dens=0; vi=vo; phi=45; // No Air Resistance g.setColor(Color.red); hit(g); g.drawString((int)vi+" m/s at 45 deg", 20, 265); g.setColor(Color.yellow); for (int j=1; j<=2; j=j+1) { // Air Resistance ntry=0; show=0; dens=j; dvi=vo; vi=0; phi=45; dphi=2; do { // Bracket the Range vi=vi+dvi; } while (ang(g)range) { vb=vi; } else { va=vi; } } while (vb-va>.1); if (j==1) { g.setColor(Color.magenta); } else { g.setColor(Color.blue); } vi=(va+vb)/2; show=1; hit(g); g.drawString((int)vi+" m/s at "+(int)(10*phi+.5)/10.+" deg", 150*j, 265); g.setColor(Color.green); } kk=0; rr=1; } if (kk==2) { // Plot the Motion hit(g); g.setColor(Color.blue); g.drawString("T = "+(int)tt+" sec without air", 50, 265); g.drawString("T = "+(int)t+" sec with air", 250, 265); kk=0; } } public void actionPerformed(ActionEvent e) { // Buttons String tst; tst=e.getActionCommand(); if (b1s.equals(tst)) { // Find the Range kk=1; repaint(); } if ((b2s.equals(tst))&&(rr==1)) { // Plot the Motion kk=2; repaint(); } } }