package pacecalc; import java.awt.*; import java.awt.event.*; import java.applet.*; import java.lang.*; /** * Title: pacecalc * Description: Pace Calculator to be used to calculate pace based on a number * of miles entered and a time entered in the format hh:mm:ss * Copyright: Copyright (c) 2001 * Company: * @author David Bischoff * @version 1.0 */ public class pacecalc extends Applet { boolean isStandalone = false; BorderLayout borderLayout1 = new BorderLayout(); Panel panel1 = new Panel(); Panel panel2 = new Panel(); Panel panel3 = new Panel(); Label lblMiles = new Label(); TextField txtMiles = new TextField(); Label lblTime = new Label(); TextField txtTime = new TextField(); Label lblPace = new Label(); TextField txtPace = new TextField(); /**Get a parameter value*/ public String getParameter(String key, String def) { return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); } /**Construct the applet*/ public pacecalc() { } /**Initialize the applet*/ public void init() { try { jbInit(); } catch(Exception e) { e.printStackTrace(); } } /**Component initialization*/ private void jbInit() throws Exception { this.setLayout(borderLayout1); lblMiles.setFont(new java.awt.Font("Dialog", 1, 20)); lblMiles.setText("Miles:"); txtMiles.setColumns(15); txtMiles.setSelectionEnd(20); txtMiles.addFocusListener(new java.awt.event.FocusAdapter() { public void focusGained(FocusEvent e) { txtMiles_focusGained(e); } }); lblTime.setFont(new java.awt.Font("Dialog", 1, 20)); lblTime.setText("Time:"); txtTime.setColumns(15); txtTime.addFocusListener(new java.awt.event.FocusAdapter() { public void focusLost(FocusEvent e) { txtTime_focusLost(e); } public void focusGained(FocusEvent e) { txtTime_focusGained(e); } }); lblPace.setFont(new java.awt.Font("Dialog", 1, 20)); lblPace.setText("Pace:"); txtPace.setColumns(15); panel1.setBackground(Color.white); panel2.setBackground(Color.white); panel3.setBackground(Color.white); this.add(panel1, BorderLayout.NORTH); panel1.add(lblMiles, null); this.add(panel2, BorderLayout.CENTER); this.add(panel3, BorderLayout.SOUTH); panel1.add(txtMiles, null); panel2.add(lblTime, null); panel2.add(txtTime, null); panel3.add(lblPace, null); panel3.add(txtPace, null); } /**Start the applet*/ public void start() { } /**Stop the applet*/ public void stop() { } /**Destroy the applet*/ public void destroy() { } /**Get Applet information*/ public String getAppletInfo() { return "Applet Information"; } /**Get parameter info*/ public String[][] getParameterInfo() { return null; } /**Main method*/ public static void main(String[] args) { pacecalc applet = new pacecalc(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("Applet Frame"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(400,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); } /* Method: txtTime_focusLost Description: When the txtTime field loses focus, call the method to calculate the pace and put the value in txtPace text box. */ void txtTime_focusLost(FocusEvent e) { try { txtPace.setText(GetPace(txtTime.getText(), txtMiles.getText())); } catch (Throwable err) { txtPace.setText("err"); } } /* Method: GetPace Description: Given the time and the miles, compute the pace. */ public String GetPace(String sTime, String sMiles) { double fMinutes, fMiles; fMinutes = GetMinutes(sTime); fMiles = Double.valueOf(sMiles.trim()).doubleValue(); //get the miles as a double fMinutes = fMinutes / fMiles; //minutes / miles = pace return MinutesToString(fMinutes); //convert minutes to string representation } /* Method GetMinutes Description: This function converts a string of time (hh:mm:ss) to a double minutes value. */ public double GetMinutes(String sTime) { String sRestOfTime, sHours, sMinutes, sSeconds; int nSecColon, nMinColon, nHrColon; double fSeconds, fMinutes, fHours; sHours = ""; sMinutes = ""; sSeconds = ""; //reverse the string so when we find the colons we know where the data goes sTime = StrReverse(sTime); nSecColon = sTime.indexOf(':'); //find the first colon if (nSecColon > 0) { //we found the colon, these are seconds sSeconds = sTime.substring(0,nSecColon); sSeconds = StrReverse(sSeconds); sRestOfTime = sTime.substring(nSecColon+1,sTime.length()); //check this nMinColon = sRestOfTime.indexOf(':'); //find the next colon for the minutes if (nMinColon > 0) { sMinutes = sRestOfTime.substring(0,nMinColon); sMinutes = StrReverse(sMinutes); sRestOfTime = sTime.substring(nMinColon+1,sTime.length()); nHrColon = sRestOfTime.indexOf(':'); //find the next colon for the hours if (nHrColon > 0) { sHours = sRestOfTime.substring(nHrColon+1,sRestOfTime.length()); sHours = StrReverse(sHours); } else { sHours = sRestOfTime; sHours = StrReverse(sHours); } } else { sMinutes = sRestOfTime; sMinutes = StrReverse(sMinutes); } } else { sSeconds = sTime; sSeconds = StrReverse(sSeconds); } //convert to numbers here and do our calculations if (sSeconds != "") fSeconds = Double.valueOf(sSeconds.trim()).doubleValue(); else fSeconds = 0.0; if (sMinutes != "") fMinutes = Double.valueOf(sMinutes.trim()).doubleValue(); else fMinutes = 0.0; if (sHours != "") fHours = Double.valueOf(sHours.trim()).doubleValue(); else fHours = 0.0; fSeconds = fSeconds / 60.0; fHours = fHours * 60.0; return fHours + fMinutes + fSeconds; } /* Method: MinutesToString Description: Given a double minutes value, this function converts the minutes to a string representation of the time (hh:mm:ss). */ public String MinutesToString(double fMinutes) { int nMinutes, nSeconds, nHours; double fSeconds, fHours; String sMinutes, sSeconds; fHours = fMinutes / 60.0; //total time is in minutes, get the hours from that nHours = (int)fHours; //convert hours to integer fMinutes = fHours - nHours; //get minutes by subtracting int hours from double hours fMinutes = fMinutes * 60.0; //multiply minutes by 60 to get the real number of minutes nMinutes = (int)fMinutes; //convert minutes to int fSeconds = fMinutes - nMinutes; //get the seconds in the same way as minutes fSeconds = fSeconds * 60.0; nSeconds = (int)fSeconds; //convert to integer //we want to have leading 0 on minutes and seconds if (nMinutes < 10) sMinutes = "0" + nMinutes; else sMinutes = Integer.toString(nMinutes); if (nSeconds < 10) sSeconds = "0" + nSeconds; else sSeconds = Integer.toString(nSeconds); if (nHours > 0) return nHours + ":" + sMinutes + ":" + sSeconds; else return nMinutes + ":" + sSeconds; } /* Method: StrReverse Description: Reverse the string passed in. */ public static String StrReverse(String source) { int i, len = source.length(); StringBuffer dest = new StringBuffer(len); for (i = (len - 1); i >= 0; i--) { dest.append(source.charAt(i)); } return dest.toString(); } /* Method: txtMiles_focusGained Description: When the txtMiles text box gains focus, select the text in the box so the user doesn't need to delete the old text. */ void txtMiles_focusGained(FocusEvent e) { txtMiles.setSelectionStart(0); txtMiles.setSelectionEnd(txtMiles.getText().length()); } /* Method: txtTime_focusGained Description: When the txtTime text box gains focus, select the text in the box so the user doesn't need to delete the old text. */ void txtTime_focusGained(FocusEvent e) { txtTime.setSelectionStart(0); txtTime.setSelectionEnd(txtTime.getText().length()); } }