Metziop

Untitled

May 13th, 2021 (edited)
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.77 KB | None | 0 0
  1.  
  2. import java.util.Observable;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. public class cronometro extends Observable implements Runnable {
  7.  
  8.     private int horas, minutos, segundos;
  9.  
  10.     public cronometro(int horas, int minutos, int segundos) {
  11.         this.horas = horas;
  12.         this.minutos = minutos;
  13.         this.segundos = segundos;
  14.  
  15.     }
  16.  
  17.     @Override
  18.     public void run() {
  19.         String tiempo;
  20.         while (true) {
  21.             try {
  22.                 tiempo = "";
  23.                 if (horas < 10) {
  24.                     tiempo += "0" + horas;
  25.                 } else {
  26.                     tiempo += horas;
  27.                 }
  28.                 tiempo += ":";
  29.                 if (minutos < 10) {
  30.                     tiempo += "0" + minutos;
  31.                 } else {
  32.                     tiempo += minutos;
  33.                 }
  34.                 tiempo += ":";
  35.                 if (segundos < 10) {
  36.                     tiempo += "0" + segundos;
  37.                 } else {
  38.                     tiempo += segundos;
  39.                 }
  40.  
  41.                 this.setChanged();
  42.                 this.notifyObservers(tiempo);
  43.                 this.clearChanged();
  44.                 Thread.sleep(1000);
  45.                 segundos++;
  46.                 if (segundos == 60) {
  47.                     minutos++;
  48.                     segundos = 0;
  49.                     if (minutos == 60) {
  50.                         horas++;
  51.                         minutos = 0;
  52.                     }
  53.                     if (horas == 24) {
  54.                         horas = 0;
  55.                     }
  56.                 }
  57.             } catch (InterruptedException ex) {
  58.                 Logger.getLogger(cronometro.class.getName()).log(Level.SEVERE, null, ex);
  59.             }
  60.         }
  61.     }
  62.  
  63. }
  64.  
Add Comment
Please, Sign In to add comment