Hanhart
On 1 July 1882, the Swiss watchmaker Johann A. Hanhart opened a watch shop in Diessenhofen, in north-eastern Switzerland. In 1902, he relocated his business to Schwenningen, southern Germany. In 1924, Hanhart launched its first stopwatch, and a short time later, the product range was extended to include pocket watches and wristwatches. From 1932 onwards, following the death of his father, Wilhelm Julius Hanhart concentrated on manufacturing raw movements. In 1938, the first single-button chronograph with the "Calibre 40" entered series production, pilot’s chronographs followed in 1939.
retrieved on 16 October 2016
JavaScript
Chronograph.js
function Chronograph() { var whenStarted = 0; var whenStopped = 0; this.reset = function() { whenStarted = 0; whenStopped = 0; } this.start = function() { whenStarted = new Date(); } this.stop = function() { whenStopped = new Date(); } this.hasBeenStarted = function() { return whenStarted > 0; } this.isStopped = function() { return whenStopped >= whenStarted; } this.isRunning = function() { return whenStarted > whenStopped; } this.elapsedTimeInMs = function() { if (this.isRunning()) { return new Date() - whenStarted; } else { return whenStopped - whenStarted; } } }
Stopwatch1Movement.js
function Stopwatch1Movement() { var previouslyElapsedTimeInMs = 0; var timer = new Chronograph(); this.startStop = function() { if (!timer.hasBeenStarted()) { timer.start(); } else if (timer.isRunning()) { timer.stop(); } else { previouslyElapsedTimeInMs = this.elapsedTimeInMs(); timer.start(); } } this.reset = function() { previouslyElapsedTimeInMs = 0; timer.reset(); } this.isRunning = function() { return timer.isRunning(); } this.elapsedTimeInMs = function() { return previouslyElapsedTimeInMs + timer.elapsedTimeInMs(); } }
Stopwatch1.js
function Stopwatch1(movement) { if (!(movement instanceof Stopwatch1Movement)) { movement = new Stopwatch1Movement(); } this.pressCrownButton = function() { movement.startStop(); } this.elapsedTimeInMs = function() { return movement.elapsedTimeInMs(); } this.isRunning = function() { return movement.isRunning(); } }
Stopwatch1Gui.js
function Stopwatch1Gui(stopwatch1) { var that = this; if (!(stopwatch1 instanceof Stopwatch1)) { stopwatch1 = new Stopwatch1(); } this.stopwatch = stopwatch1; (function connectCrownButtion() { function listener(e) { console.log("GUI's crown button pressed."); //console.log("Event:"); console.log(e); that.pressCrownButton.call(that); } that.crownButton.addEventListener("click", listener); })(); } Stopwatch1Gui.prototype = { displayTime: function() { console.log("Displaying time."); this.timeValue.innerHTML = this.stopwatch.elapsedTimeInMs(); }, periodicallyRefreshDisplay: function() { var that = this; function repeat() { function doIt() { that.displayTime.call(that); repeat(); } if (that.stopwatch.isRunning() && that.gui.window != null) { setTimeout(doIt, that.refreshRateInMs); } } repeat(); }, pressCrownButton: function() { this.stopwatch.pressCrownButton(); if (this.stopwatch.isRunning()) { this.periodicallyRefreshDisplay.call(this); } }, };
Stopwatch1GuiA.js
function Stopwatch1GuiA(stopwatch1) { // Configuration values var GUI_WIDTH = "400"; var GUI_HEIGHT = "150"; var CROWN_BUTTON_NAME = "Start/Stop"; var UNITS_LABEL = "ms"; var INITIAL_TIME = "0"; var INITIAL_LABEL = "Elapsed time:"; // Stopwatch container this.gui = window.open ("", "", "width="+GUI_WIDTH+",height="+GUI_HEIGHT); // Alias for this.gui.document var doc = this.gui.document; // Crown button doc.write("<center><button id='crownButton'>"); doc.write(CROWN_BUTTON_NAME); doc.write("</button></center>"); // Time display doc.write("<br><center><h1>"); doc.write("<span id='timeLable'>"); doc.write(INITIAL_LABEL+"</span>"); doc.write(" <span id='timeValue'>"); doc.write(INITIAL_TIME+"</span>"); doc.write(" <span id='timeUnits'>"); doc.write(UNITS_LABEL+"</span>"); doc.write("</h1></center>"); // DOM elements this.crownButton = doc.getElementById("crownButton"); this.timeLabel = doc.getElementById("timeLabel"); this.timeValue = doc.getElementById("timeValue"); this.timeUnits = doc.getElementById("timeUnits"); // Constructor chaining Stopwatch1Gui.call(this, stopwatch1); } // Prototype chaining Stopwatch1GuiA.prototype = Object.create(Stopwatch1Gui.prototype); // Additional shared properties Stopwatch1GuiA.prototype.refreshRateInMs = 25;
Stopwatch.html
<script src="Chronograph.js"></script> <script src="Stopwatch1Movement.js"></script> <script src="Stopwatch1.js"></script> <script src="Stopwatch1Gui.js"></script> <script src="Stopwatch1GuiA.js"></script> <button onclick="new Stopwatch1GuiA;">New Stopwatch</button>
Java
./com/blogspot/finnegantakes/Chronograph.js
package com.blogspot.finnegantakes; public class Chronograph { private long whenStarted = 0; private long whenStopped = 0; public void reset() { whenStarted = 0; whenStopped = 0; } public void start() { whenStarted = System.currentTimeMillis(); } public void stop() { whenStopped = System.currentTimeMillis(); } public boolean hasBeenStarted() { return whenStarted > 0; } public boolean isStopped() { return whenStopped >= whenStarted; } public boolean isRunning() { return whenStarted > whenStopped; } public long elapsedTimeInMs() { if (isRunning()) { return System.currentTimeMillis() - whenStarted; } else { return whenStopped - whenStarted; } } }
./com/blogspot/finnegantakes/Stopwatch1Movement.java
package com.blogspot.finnegantakes; import com.blogspot.finnegantakes.Chronograph; public class Stopwatch1Movement { private long previouslyElapsedTimeInMs = 0; private Chronograph timer; public Stopwatch1Movement() { this.timer = new Chronograph(); } public void startStop() { if (!timer.hasBeenStarted()) { timer.start(); } else if (timer.isRunning()) { timer.stop(); } else { previouslyElapsedTimeInMs = elapsedTimeInMs(); timer.start(); } } public void reset() { previouslyElapsedTimeInMs = 0; timer.reset(); } public boolean isRunning() { return timer.isRunning(); } public long elapsedTimeInMs() { return previouslyElapsedTimeInMs + timer.elapsedTimeInMs(); } }
./com/blogspot/finnegantakes/Stopwatch1.java
package com.blogspot.finnegantakes; import com.blogspot.finnegantakes.Stopwatch1Movement; public class Stopwatch1 { private Stopwatch1Movement movement; public Stopwatch1() { this(new Stopwatch1Movement()); } public Stopwatch1(Stopwatch1Movement movement) { this.movement = movement; } public void pressCrownButton() { movement.startStop(); } public long elapsedTimeInMs() { return movement.elapsedTimeInMs(); } public boolean isRunning() { return movement.isRunning(); } }
./com/blogspot/finnegantakes/Stopwatch1Gui.java
package com.blogspot.finnegantakes; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.Timer; import com.blogspot.finnegantakes.Stopwatch1; public abstract class Stopwatch1Gui { protected JLabel timeLabel = new JLabel(); protected JLabel timeValue = new JLabel(); protected JLabel timeUnits = new JLabel(); protected JButton crownButton = new JButton(); protected JFrame gui = new JFrame(); private Stopwatch1 stopwatch; Stopwatch1Gui(Stopwatch1 stopwatch1) { stopwatch = stopwatch1; connectCrownButton(); periodicallyRefreshDisplay(); } protected void displayTime() { this.timeValue.setText(Long.toString(stopwatch.elapsedTimeInMs())); } protected void periodicallyRefreshDisplay() { final int PERIOD_MS = 100; ActionListener listener = new ActionListener() { public void actionPerformed(ActionEvent evt) { displayTime(); } }; new javax.swing.Timer(PERIOD_MS, listener).start(); } protected void connectCrownButton() { crownButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stopwatch.pressCrownButton(); } }); } public void pressCrownButton() { stopwatch.pressCrownButton(); } }
./com/blogspot/finnegantakes/Stopwatch1GuiA.java
package com.blogspot.finnegantakes; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.FlowLayout; import com.blogspot.finnegantakes.Stopwatch1Gui; public class Stopwatch1GuiA extends Stopwatch1Gui { private final int GUI_WIDTH = 325; private final int GUI_HEIGHT = 75; private final String CROWN_BUTTON_NAME = "Start/Stop"; private final String UNITS_LABEL = "ms"; private final String INITIAL_TIME = "0"; private final String INITIAL_LABEL = "Elapsed time: "; public Stopwatch1GuiA() { this(new Stopwatch1()); } public Stopwatch1GuiA(Stopwatch1 stopwatch1) { super(stopwatch1); crownButton.setText(CROWN_BUTTON_NAME); timeLabel.setText(INITIAL_LABEL); timeValue.setText(INITIAL_TIME); timeUnits.setText(UNITS_LABEL); gui.getContentPane().setLayout(new FlowLayout()); gui.getContentPane().add(crownButton); gui.getContentPane().add(timeLabel); gui.getContentPane().add(timeValue); gui.getContentPane().add(timeUnits); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(GUI_WIDTH, GUI_HEIGHT); gui.setVisible(true); } }
./Stopwatch.java
import com.blogspot.finnegantakes.Stopwatch1GuiA; public class Stopwatch { public static void main(String[] args) { new Stopwatch1GuiA(); } }