/* This applet simulates an object falling with a constant gravitational acceleration. */ /* */ import java.applet.*; import java.awt.*; import java.awt.event.*; /*--------------------------------------------------*/ public class FallingObject extends Applet implements ActionListener{ BoxShoot shoot; Button startBt; TextField nDropsText; TextField gText; TextField speedUpText; TextField elevationText; int nDropsDefault = 1; double gDefault = 980.0; double speedUpDefault = 1.0; double elevationDefault= 200.0; int nDrops; int dropCount=0; double g; double speedUp; double elevation; // Add detector setup here G_Detector detector=null; /*--------------------------------------------------*/ // public void init(){ setBackground(Color.blue); setLayout(new BorderLayout()); nDrops = nDropsDefault; g = gDefault; speedUp = speedUpDefault; elevation = elevationDefault; shoot = new BoxShoot(nDrops, speedUp, g, elevation,this); add("West",shoot); Panel controls = new Panel(new BorderLayout()); controls.setBackground(Color.white); startBt = new Button("Start"); startBt.addActionListener(this); Panel bp0 = new Panel(); bp0.add(startBt); controls.add("West",bp0); Panel entriesPane = new Panel(); entriesPane.setLayout(new GridLayout(4,2)); Panel lp0 = new Panel(new FlowLayout(FlowLayout.RIGHT)); lp0.add(new Label("N-drops:")); entriesPane.add(lp0); nDropsText = new TextField(""+nDrops,4); nDropsText.addActionListener(this); Panel pt0 = new Panel(new FlowLayout(FlowLayout.LEFT)); pt0.add(nDropsText); entriesPane.add(pt0); Panel lp1 = new Panel(new FlowLayout(FlowLayout.RIGHT)); lp1.add(new Label("g(cm/s**2)=")); entriesPane.add(lp1); gText = new TextField(""+g,4); gText.addActionListener(this); Panel pt1 = new Panel(new FlowLayout(FlowLayout.LEFT)); pt1.add(gText); entriesPane.add(pt1); Panel lp2 = new Panel(new FlowLayout(FlowLayout.RIGHT)); lp2.add(new Label("FrameRate:")); entriesPane.add(lp2); speedUpText = new TextField(""+speedUp,4); speedUpText.addActionListener(this); Panel pt2 = new Panel(new FlowLayout(FlowLayout.LEFT)); pt2.add(speedUpText); entriesPane.add(pt2); Panel lp3 = new Panel(new FlowLayout(FlowLayout.RIGHT)); lp3.add(new Label("Height(cm):")); entriesPane.add(lp3); elevationText = new TextField(""+elevation,4); elevationText.addActionListener(this); Panel pt3 = new Panel(new FlowLayout(FlowLayout.LEFT)); pt3.add(elevationText); entriesPane.add(pt3); controls.add("East",entriesPane); add("South",controls); // Add the detector detector = new G_Detector( shoot.dY, shoot.dX); add("East",detector); detector.init(); } /*--------------------------------------------------*/ public void dropDone () { detector.eventDone(); dropCount--; if(dropCount > 0){ detector.eventStart(); shoot.start(); } } /*--------------------------------------------------*/ public void actionPerformed ( ActionEvent e) { Object src = e.getSource(); String str; if( src == startBt) { checkTextFields(); dropCount = nDrops; detector.eventStart(); shoot.start(); } else if(src == nDropsText){ str = nDropsText.getText(); try{ nDrops = Integer.valueOf(str).intValue(); } catch ( NumberFormatException ex){ nDrops = nDropsDefault; nDropsText.setText(""+nDropsDefault); } shoot.setnDrops(nDrops); } else if(src == gText){ str = gText.getText(); try{ g = Double.valueOf(str).doubleValue(); } catch ( NumberFormatException ex){ g = gDefault; gText.setText(""+gDefault); } shoot.setGrav(g); } else if(src == speedUpText){ str = speedUpText.getText(); try{ speedUp = Double.valueOf(str).doubleValue(); } catch ( NumberFormatException ex){ speedUp = speedUpDefault; speedUpText.setText(""+speedUpDefault); } shoot.setSpeedUp(speedUp); } else if(src == elevationText){ str = elevationText.getText(); try{ elevation = Double.valueOf(str).doubleValue(); } catch ( NumberFormatException ex){ elevation = elevationDefault; elevationText.setText(""+elevationDefault); } shoot.setElevation(elevation); } } /*--------------------------------------------------*/ void checkTextFields(){ String str = nDropsText.getText(); try{ nDrops = Integer.valueOf(str).intValue(); } catch ( NumberFormatException ex){ nDrops = nDropsDefault; nDropsText.setText(""+nDropsDefault); } shoot.setnDrops(nDrops); str = gText.getText(); try{ g = Double.valueOf(str).doubleValue(); } catch ( NumberFormatException ex){ g = gDefault; gText.setText(""+gDefault); } shoot.setGrav(g); str = speedUpText.getText(); try{ speedUp = Double.valueOf(str).doubleValue(); } catch ( NumberFormatException ex){ speedUp = speedUpDefault; speedUpText.setText(""+speedUpDefault); } shoot.setSpeedUp(speedUp); str = elevationText.getText(); try{ elevation = Double.valueOf(str).doubleValue(); } catch ( NumberFormatException ex){ elevation = elevationDefault; elevationText.setText(""+elevationDefault); } shoot.setElevation(elevation); } /*--------------------------------------------------*/ public Insets getInsets(){ return new Insets(2,2,2,2); } }