import java.applet.*; import java.*; import java.awt.*; import java.util.*; import java.lang.*; public class Homerun extends Applet { TrajectoryPoint[] history; Image img = null; Image imgback = null; Image back, ball; Image[] batter; boolean erase = true; int batterindex = -1; boolean newball; int xball, yball; Graphics gi = null; Graphics g2; BB bb; boolean okToHit; Choice alt; Scrollbar vel, ang; String[] cities = {"New York", "St. Louis","Milwaukee","Kansas City","Atlanta","Denver","Pike's Peak"}; int[] elevations = {55, 470, 593, 800, 1050, 5280, 14109}; Button hit, clearButt; double initialAltitude, initialVelocity, initialAngle; Font fhelp, distFont; Label altLab, velLab, angLab; final double M2FT = 3.28084; final double MPS2MPH = 2.23694; int distance = -1; boolean showHelp = true; AudioClip batSound, fanSound; public void init() { GetMediaFile gif = new GetMediaFile(this); batSound = gif.getAudioClip("woodbat.au"); fanSound = gif.getAudioClip("fans.au"); back = gif.getImage("ballpark.gif"); ball = gif.getImage("ball.gif"); batter = new Image[3]; batter[0] = gif.getImage("batter0.gif"); batter[1] = gif.getImage("batter1.gif"); batter[2] = gif.getImage("batter2.gif"); batterindex = 0; newball = false; okToHit = true; bb = new BB(); distFont = new Font("sansserif",Font.BOLD,18); fhelp = new Font("sans",Font.PLAIN,14); setLayout(null); Color bc = new Color(1, 153, 1); setBackground(bc); initialAltitude = 1609.; initialAngle = 40.; initialVelocity = 40.; hit = new Button("Hit"); clearButt = new Button("Clear"); alt = new Choice(); alt.setForeground(Color.black); String cityparam = getParameter("cities"); String eleparam = getParameter("elevations"); if (cityparam != null && eleparam != null) { StringTokenizer stc = new StringTokenizer(cityparam,","); StringTokenizer ste = new StringTokenizer(eleparam,","); int n = stc.countTokens(); cities = new String[n]; elevations = new int[n]; for (int i=0; i 623) { // 1.8 * 345. cheer = true; fanSound.play(); } paint(this.getGraphics()); try { Thread.sleep(60); } catch (Exception eb) {;} } distance = (int)Math.round(history[bb.npts-1].position[0]*M2FT); batterindex = 0; paint(this.getGraphics()); return true; } if (e.target == clearButt) { if (img != null) { img.flush(); imgback.flush(); } img = null; imgback = null; distance = -1; paint(this.getGraphics()); okToHit = true; hit.enable(true); return true; } return false; } public void update(Graphics g) { paint(g); } public void paint(Graphics g) { if (img == null) { img = createImage(back.getWidth(this), back.getHeight(this)); gi = img.getGraphics(); gi.drawImage(back,0,0,this); imgback = createImage(back.getWidth(this), back.getHeight(this)); } gi = img.getGraphics(); if (newball) { gi.drawImage(ball,xball,yball,this); } newball = false; g2 = imgback.getGraphics(); g2.drawImage(img,0,0,this); if (batterindex >= 0) { int xoff = 30; if (batterindex == 2) xoff = 18; g2.drawImage(batter[batterindex],xoff,285,this); if (distance > 0) { g2.setFont(distFont); g2.setColor(Color.black); g2.drawString(distance+" feet",600, 455); g2.setColor(Color.white); g2.drawString(distance+" feet",601, 456); } } if (showHelp) { g2.setColor(Color.white); g2.setFont(fhelp); g2.drawString("Pick a city (elevation)",20,490); g2.drawString("Set speed of hit ball",225,490); g2.drawString("Set angle leaving bat",430,490); } g.drawImage(imgback,0,0,this); g2.dispose(); gi.dispose(); g.dispose(); } /** * @param t temperature (K) * @param td dewpoint (K) * */ public double makeRhoZero(double t, double td) { return (.348388 * 1013.2 / virtualTemperature(t, td, 1013.2)); } /** Virtual Temperature * */ public double virtualTemperature(double t, double td, double p) { return ( t * (1.0 + .000609*mixingRatio(td, p)) ); } /** saturation vapor pressure over water. t in kelvin. * */ public double satVapPres(double t) { double coef[]={6.1104546,0.4442351,1.4302099e-2, 2.6454708e-4, 3.0357098e-6, 2.0972268e-8, 6.0487594e-11,-1.469687e-13}; // sat vap pressures every 5C from -50 to -200 double escold[] = { 0.648554685769663908E-01, 0.378319512256073479E-01, 0.222444934288790197E-01, 0.131828928424683120E-01, 0.787402077141244848E-02, 0.473973049488473318E-02, 0.287512035504357928E-02, 0.175743037675810294E-02, 0.108241739518850975E-02, 0.671708939185605941E-03, 0.419964702632039404E-03, 0.264524363863469876E-03, 0.167847963736813220E-03, 0.107285397631620379E-03, 0.690742634496135612E-04, 0.447940489768084267E-04, 0.292570419563937303E-04, 0.192452912634994161E-04, 0.127491372410747951E-04, 0.850507010275505138E-05, 0.571340025334971129E-05, 0.386465029673876238E-05, 0.263210971965005286E-05, 0.180491072930570428E-05, 0.124607850555816049E-05, 0.866070571346870824E-06, 0.605982217668895538E-06, 0.426821197943242768E-06, 0.302616508514379476E-06, 0.215963854234913987E-06, 0.155128954578336869E-06}; double temp = t - 273.16; double retval; if (temp > -50.) { retval = ( coef[0] + temp*(coef[1] + temp*(coef[2] + temp*(coef[3] + temp*(coef[4] + temp*(coef[5] + temp*(coef[6] + temp*coef[7])))))) ); } else { double tt = (-temp - 50.)/5.; int inx = (int) tt; if (inx < escold.length) { retval = escold[inx] + (tt % 1.)*(escold[inx+1]-escold[inx]); } else { retval = 1e-7; } } return retval; } /** mixing ratio * */ public double mixingRatio(double t, double p) { double e = satVapPres(t); return ( 621.97*e/(p - e) ); } }