1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| public class Chrono extends JPanel{
private final static DateTimeFormatter HHMMSS_FORMAT = DateTimeFormatter.ofPattern("HH:mm:ss");
public final static Function<Duration,String> FORMAT_HH_MM_SS = duration-> HHMMSS_FORMAT.format(LocalTime.ofNanoOfDay(duration.toNanos()));
public final static Function<Duration,String> FORMAT_SEC = duration-> String.valueOf(duration.getSeconds());
private JLabel jlabel;
private Timer timer;
private String DEFAULT_DISPLAY =" ";
private Instant startTime;
private Instant endTime;
private Function<Duration, String> format;
public Chrono() {
this(FORMAT_HH_MM_SS);
}
public Chrono(Function<Duration,String> format) {
this.format = Objects.requireNonNull(format);
jlabel = new JLabel(DEFAULT_DISPLAY , JLabel.CENTER);
add(jlabel);
}
public void setFormat(Function<Duration,String> format) {
this.format = Objects.requireNonNull(format);
repaint();
}
public Function<Duration, String> getFormat() {
return format;
}
public void start() {
if ( timer==null ) {
timer = new Timer(1000, e-> updateTime());
startTime = Instant.now();
endTime = Instant.now();
timer.start();
updateTime();
}
}
public void stop() {
if ( timer!=null ) {
endTime = Instant.now();
timer.stop();
timer=null;
}
}
public Duration getDuration() {
if ( startTime==null ) {
Instant now=Instant.now();
return Duration.between(now, now);
}
return Duration.between(startTime, endTime);
}
private void updateTime() {
endTime = Instant.now();
final Duration duration = Duration.between(startTime, endTime);
jlabel.setText(format.apply(duration));
revalidate();
repaint();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Démo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Chrono[] chronos = new Chrono[]{
new Chrono(),
new Chrono(Chrono.FORMAT_SEC),
new Chrono(duration-> {
final long s = duration.getSeconds();
return String.format("%02d:%02d", s / 60, s % 60);
}),
new Chrono(duration-> String.format("%d sec", duration.getSeconds()))
};
JPanel chronoPanel = new JPanel(new GridLayout(0,1));
for(Chrono chrono : chronos) {
chronoPanel.add(chrono);
}
frame.add( chronoPanel );
JPanel buttonPanel = new JPanel();
JButton buttonStart = new JButton("Start");
JButton buttonStop = new JButton("Stop");
buttonStart.addActionListener(e-> start(chronos, buttonStart, buttonStop));
buttonStop.addActionListener(e-> stop(chronos, buttonStart, buttonStop));
buttonPanel.add( buttonStart );
buttonPanel.add( buttonStop );
frame.add(buttonPanel, BorderLayout.SOUTH);
frame.addWindowListener(new WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent e) {
stop(chronos);
};
});
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
buttonStart.setEnabled(false);
start(chronos);
}
private static void start(Chrono[] chronos) {
for(Chrono chrono : chronos) chrono.start();
}
private static void stop(Chrono[] chronos) {
for(Chrono chrono : chronos) chrono.stop();
}
private static void start(Chrono[] chronos, JButton buttonStart,
JButton buttonStop) {
start(chronos);
buttonStart.setEnabled(false);
buttonStop.setEnabled(true);
}
private static void stop(Chrono[] chronos, JButton buttonStart,
JButton buttonStop) {
stop(chronos);
buttonStart.setEnabled(true);
buttonStop.setEnabled(false);
}
} |