import java.awt.*; import javax.swing.*; import java.awt.event.*; /** * Class Boat - type a current's speed and direction, * and the boat's speed and direction to control a boat * See http://collections.ic.gc.ca/allaboutmaps/aaMaps_M4_bearing_Z.htm * * @author Chris Thiel * @version 1.1 */ public class SwimSim extends JApplet implements ActionListener { // instance variables - replace the example below with your own JTextField wSpeed; JTextField wDirection; JTextField speed; JTextField direction; JButton resetButton; JButton startButton; JLabel wsLabel,wdLabel,sLabel, dLabel; Container c; JPanel p; DrawingSurface ds; Timer timer; Image boat; /** * Called by the browser or applet viewer to inform this JApplet that it * has been loaded into the system. It is always called before the first * time that the start method is called. */ public void init() { // this is a workaround for a security conflict with some browsers // including some versions of Netscape & Internet Explorer which do // not allow access to the AWT system event queue which JApplets do // on startup to check access. May not be necessary with your browser. JRootPane rootPane = this.getRootPane(); rootPane.putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE); // provide any initialisation necessary for your JApplet boat = getImage(getCodeBase(), "Swimmer.gif"); ds = new DrawingSurface(boat); wsLabel= new JLabel("Speed of Current:",JLabel.RIGHT); wdLabel= new JLabel("Compass Direction:",JLabel.RIGHT); sLabel= new JLabel("Knots:",JLabel.RIGHT); dLabel= new JLabel("Direction:",JLabel.RIGHT); wSpeed = new JTextField("2",8); wDirection = new JTextField("90",8); speed = new JTextField("5", 8); direction = new JTextField("0", 8); ds.setXPos(150); ds.setYPos(150); /** * Here I'll make an Annonymous Inner Class for the * ActionListener of the scrollButton which * converts the scrollButton into a "start" or "stop" button, * and switches the timer on and off */ timer = new Timer(60, this); resetButton = new JButton("Reset"); resetButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { if (timer.isRunning()){ timer.stop(); startButton.setText("Start"); } wSpeed.setText("2"); wDirection.setText("90"); speed.setText("5"); direction.setText("0"); direction.selectAll(); ds.setXPos(150); ds.setYPos(150); ds.repaint(); } }); startButton = new JButton("Start"); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ev) { if (timer.isRunning()){ timer.stop(); startButton.setText("Start"); }else{ try{ double wp =Double.parseDouble(wSpeed.getText()); double wd =Double.parseDouble(wDirection.getText()); double sp =Double.parseDouble(speed.getText()); double dr =Double.parseDouble(direction.getText()); ds.setWaterSpeed(wp); ds.setWaterDirection(wd); ds.setSpeed(sp); ds.setDirection(dr); }catch (NumberFormatException e){ } direction.selectAll(); timer.start(); startButton.setText("Stop"); } } }); c = getContentPane(); c.setLayout(new FlowLayout()); p = new JPanel(); p.setLayout( new GridLayout(2,6)); p.add(wsLabel); p.add(wSpeed); p.add(sLabel); p.add(speed); p.add(startButton); p.add(wdLabel); p.add(wDirection); // p.add(Box.createHorizontalGlue()); p.add(dLabel); p.add(direction); p.add(resetButton); c.add(p); c.add(ds); } /** * Called by the browser or applet viewer to inform this JApplet that it * is being reclaimed and that it should destroy any resources that it * has allocated. The stop method will always be called before destroy. */ public void actionPerformed(ActionEvent ev) { Object source = ev.getSource(); if(source == timer) { ds.sailAway(); repaint(); } } }