// Rocket_flight.java import java.awt.*; import java.awt.event.*; import java.io.*; import javax.swing.*; public class Rocket_flight extends Frame { double Ft[] = new double[20]; // thrust force in newtons double s = 0.0; // altitude in meters double v = 0.0; // velocity in meters per second double a = 0.0; // acceleration in meters per second squared double m = 0.0; // mass in kilograms double m_body = 0.0340; // mass of body in kilograms double m_engine = 0.0242; // initial engine mass double t = 0.0; // time in seconds double dt = 0.1; // delta time step in seconds double Fd = 0.0; // drag force in newtons double Fg = 0.0; // fource due to gravity in newtons double F = 0.0; // total force in newtons double gr = 9.80665; // acceleration due to gravity double Rho = 1.293; // kilograms per cubic meter double Cd_body = 0.45; // coefficient of drag for nose cone and body double A_body = 0.000506; // cross section area of body in square meter double Cd_fins = 0.01; // coefficient of drag for fins double A_fins = 0.00496; // surface area of fins in square meters double sum = 0.0; // sum of thrust for checking int step = 0; // step number for thrust, max=19 int width = 800; int height = 700; int x, y, b; ImageIcon imageRocket = new ImageIcon(" "); // set below ImageIcon imageThrust = new ImageIcon(" "); // set below Dimension dimActive; public Rocket_flight() { System.out.println("Rocket_flight.java launching"); setTitle("Rocket_flight"); setSize(width,height); setBackground(Color.white); setForeground(Color.black); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); this.addMouseListener (new mousePressHandler()); Ft[0] = 0.0; // actually unused, initial condition Ft[1] = 6.0; Ft[2] = 14.1; Ft[3] = 5.01; for(int i=4; i<19; i++) Ft[i] = 4.326; Ft[19] = 0.0; imageRocket = new ImageIcon("rocket.jpg"); dimActive = new Dimension(imageRocket.getIconWidth(), imageRocket.getIconHeight()); System.out.println("rocket.jpg dimension = "+dimActive); imageThrust = new ImageIcon("thrust.jpg"); dimActive = new Dimension(imageThrust.getIconWidth(), imageThrust.getIconHeight()); System.out.println("thrust.jpg dimension = "+dimActive); requestFocus(); repaint(); } double drag(double Cd, double Rho, double Area, double velocity) { return Cd*Rho*Area*velocity*velocity/2.0; } class mousePressHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { x = e.getX(); y = e.getY(); b = e.getButton(); System.exit(0); requestFocus(); repaint(); } } public void paint(Graphics g) { int ti=0; // draw a boundary setForeground(Color.black); g.drawRect(20, 40, width-40, height-60); g.drawString("Estes Alpha III", 200, 70); g.drawString("meter", 30, 50); g.drawString("time seconds", 100, 50); // imageThrust.paintIcon( this, g, 120, 380); // =0.0) { // System.out.println("t="+t+", s="+s+", v="+v+", a="+a+ // ", F="+F+", m="+m); t = t + dt; ti = (int)(t*10.0+0.0000001); t = (double)ti/10.0; // remove trailing if(step<19) step++; Fg = m * gr; m = m - Ft[step]*0.0001648; Fd = drag(Cd_body, Rho, A_body, v) + drag(Cd_fins, Rho, A_fins, v); F = Ft[step] - Fg - Fd; a = F/m; v = v + a * dt; s = s + v * dt; if(v>=0.0) { setForeground(Color.red); // g.fillOval(70, height-(int)(1.68*s)-25 , 6, 6); g.drawLine(70, height-(int)(1.68*s)-25 ,80, height-(int)(1.68*s)-25); g.drawString(" "+t+" ", 90, height-(int)(1.68*s)-25); } try { // physicsComputation(); Thread.sleep(100); // 100 millisec 0.1 sec } catch(InterruptedException e) { System.out.println("interruptedException"+e); } } setForeground(Color.black); g.drawString(" "+t+" ", 200, 50); g.drawString("height "+s+" ", 300, 50); } // end paint public static void main (String[] args) { new Rocket_flight(); } } // end Rocket_flight.java