Advertisement
Metziop

Untitled

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