Advertisement
LucasSousa

Lucas's Timer Finalized v1

Dec 24th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.89 KB | None | 0 0
  1. //>>>>>>>>>>>>>>>>CLASSE 1
  2.  
  3. package crono3;
  4.  
  5. import javax.swing.*;
  6.  
  7. public class Cronometragem extends Thread {
  8.  
  9.     private JLabel label;
  10.     boolean threadON = false;
  11.     boolean threadOFF = true;
  12.  
  13.     boolean threadON2;
  14.     boolean threadOFF2;
  15.  
  16.     int centésimo = 0, segundo = 0, minuto = 0;
  17.  
  18.     public Cronometragem(JLabel stringTempo) {
  19.  
  20.         this.label = stringTempo;
  21.     }
  22.  
  23.     @Override
  24.     public void run() {
  25.  
  26.         while (true) {
  27.  
  28.             try {
  29.  
  30.                 Thread.sleep((long) 9.6787);
  31.  
  32.                 threadON = true;
  33.  
  34.                 threadOFF = false;
  35.  
  36.                 if (CRONO3.isContador()) {
  37.                     centésimo++;
  38.  
  39.                     if (centésimo == 100) {
  40.                         centésimo = 0;
  41.                         segundo++;
  42.                     }
  43.  
  44.                     if (segundo == 60) {
  45.                         segundo = 0;
  46.                         minuto++;
  47.                     }
  48.  
  49.                     String timerMinuto = "";
  50.                     String timerSegundo = "";
  51.                     String timerCentésimo = "";
  52.  
  53.                     if (minuto < 10) {
  54.                         timerMinuto = "0" + minuto;
  55.                     } else {
  56.                         timerMinuto = String.valueOf(minuto);
  57.                     }
  58.  
  59.                     if (segundo < 10) {
  60.                         timerSegundo = "0" + segundo;
  61.                     } else {
  62.                         timerSegundo = String.valueOf(segundo);
  63.                     }
  64.  
  65.                     if (centésimo < 10) {
  66.                         timerCentésimo = "0" + centésimo;
  67.                     } else {
  68.                         timerCentésimo = String.valueOf(centésimo);
  69.                     }
  70.  
  71.                     this.label.setText(timerMinuto + ":" + timerSegundo + "." + timerCentésimo);
  72.                     this.label.revalidate();
  73.  
  74.                 }
  75.             } catch (InterruptedException e) {
  76.  
  77.             }
  78.  
  79.         }
  80.  
  81.     }
  82.  
  83. }
  84.  
  85. ===============================================================================================================
  86. ===============================================================================================================
  87.  
  88. //>>>>>>>>>>>>>>>>>>>CLASSE 2
  89. package crono3;
  90.  
  91. import java.util.*; //Random
  92.  
  93. public class Scrambler2x2 {
  94.  
  95.     public static String embaralhar2x2() {
  96.  
  97.         String[] moves1 = {"F", "U", "R"};
  98.         String[] moves2 = {" ", "' "};
  99.         Random r = new Random();
  100.         String s = "";
  101.         String f = "";
  102.         String retorno = "";
  103.  
  104.         for (int i = 0; i < 8; i++) {
  105.  
  106.             do {
  107.                 f = moves1[r.nextInt(3)];
  108.  
  109.             } while (f == s);
  110.             s = f;
  111.  
  112.             String u = moves2[r.nextInt(2)];
  113.             retorno += s + u;
  114.         }
  115.  
  116.         return retorno;
  117.     }
  118.  
  119. }
  120.  
  121. ===============================================================================================================
  122. ===============================================================================================================
  123.  
  124. //>>>>>>>>>>>>>CLASSE 3
  125.  
  126. package crono3;
  127.  
  128. import java.awt.*;
  129. import java.awt.event.*;
  130. import javax.swing.*;
  131.  
  132. public class CRONO3 extends JFrame {
  133.  
  134.     static JLabel tempoCorrendo;
  135.     static JLabel tempoCorrendoInsp;
  136.  
  137.     static boolean contador = true;
  138.     boolean prontoParaPause = false;
  139.     boolean botãoSemUtilidadePorEnquanto = false;
  140.  
  141.     JLabel label1;
  142.     JButton botão1, botão2, botão3;
  143.  
  144.     Scrambler3x3 scramble1 = new Scrambler3x3();
  145.  
  146.     public CRONO3() {
  147.  
  148.         setTitle("Timer");
  149.         setSize(441, 217);
  150.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  151.         setLayout(new FlowLayout(FlowLayout.CENTER));
  152.  
  153.         label1 = new JLabel();
  154.         label1.setText("Scramble: " + String.valueOf(scramble1.embaralhar3x3()));
  155.         add(label1);
  156.  
  157.         tempoCorrendo = new JLabel();
  158.         tempoCorrendo.setText("00:00.00");
  159.         tempoCorrendo.setFont(new Font("Times New Roman", Font.BOLD, 100));
  160.         add(tempoCorrendo);
  161.  
  162.         Cronometragem running = new Cronometragem(tempoCorrendo);
  163.         InspTime runningInsp = new InspTime(tempoCorrendo);
  164.  
  165.         botão1 = new JButton("Start/Stop");
  166.  
  167.         botão1.addActionListener(new ActionListener() {
  168.             @Override
  169.             public void actionPerformed(ActionEvent evt) {
  170.                
  171.                 if (running.threadCronoON == false) {
  172.                    
  173.                     running.start();
  174.                 }
  175.  
  176.                 if (running.threadCronoON == true) {
  177.                     iniciarActionPerformed(evt);
  178.                 }
  179.  
  180.                 if (running.threadCronoOFF == false) {
  181.                     pausarActionPerformed(evt);
  182.  
  183.                     label1.setText("Scramble: " + String.valueOf(scramble1.embaralhar3x3()));
  184.                     botão1.setVisible(false);
  185.                     botão1.setEnabled(false);
  186.  
  187.                     botão2.setEnabled(true);
  188.                     botão2.setVisible(true);
  189.  
  190.                     botão3.setVisible(false);
  191.                     botão3.setEnabled(false);
  192.                 }
  193.             }
  194.         });
  195.  
  196.         botão2 = new JButton("Start/Stop");
  197.         botão2.setVisible(false);
  198.         botão2.setEnabled(false);
  199.  
  200.         botão2.addActionListener(new ActionListener() {
  201.             @Override
  202.             public void actionPerformed(ActionEvent evt) {
  203.  
  204.                 if (running.threadCronoON == true) {
  205.  
  206.                     if (running.centésimo > 0 || running.minuto > 0 || running.segundo > 0) {
  207.  
  208.                         running.centésimo = 0;
  209.                         running.minuto = 0;
  210.                         running.segundo = 0;
  211.                     }
  212.  
  213.                     iniciarActionPerformed(evt);
  214.  
  215.                     botão1.setVisible(false);
  216.                     botão1.setEnabled(false);
  217.  
  218.                     botão2.setVisible(false);
  219.                     botão2.setEnabled(false);
  220.  
  221.                     botão3.setEnabled(true);
  222.                     botão3.setVisible(true);
  223.  
  224.                     prontoParaPause = true;
  225.  
  226.                 }
  227.  
  228.             }
  229.         });
  230.  
  231.         botão3 = new JButton("Start/Stop");
  232.         botão3.setEnabled(false);
  233.         botão3.setVisible(false);
  234.  
  235.         botão3.addActionListener(new ActionListener() {
  236.             @Override
  237.             public void actionPerformed(ActionEvent evt) {
  238.  
  239.                 if (prontoParaPause == true) {
  240.  
  241.                     pausarActionPerformed(evt);
  242.                     label1.setText("Scramble: " + String.valueOf(scramble1.embaralhar3x3()));
  243.  
  244.                     botão1.setEnabled(true);
  245.                     botão1.setVisible(true);
  246.  
  247.                     botão2.setVisible(false);
  248.                     botão2.setEnabled(false);
  249.  
  250.                     botão3.setVisible(false);
  251.                     botão3.setEnabled(false);
  252.  
  253.                     botãoSemUtilidadePorEnquanto = true;
  254.  
  255.                     if (botãoSemUtilidadePorEnquanto == true) {
  256.  
  257.                         botão1.setVisible(false);
  258.                         botão1.setEnabled(false);
  259.  
  260.                         botão2.setEnabled(true);
  261.                         botão2.setVisible(true);
  262.  
  263.                         botão3.setVisible(false);
  264.                         botão3.setEnabled(false);
  265.  
  266.                     }
  267.  
  268.                 }
  269.  
  270.             }
  271.         });
  272.  
  273.         add(botão1);
  274.         reagirSpace(botão1);
  275.  
  276.         add(botão2);
  277.         reagirSpace(botão2);
  278.  
  279.         add(botão3);
  280.         reagirSpace(botão3);
  281.     }
  282.  
  283.     public static void reagirSpace(JButton botão) {
  284.         botão.registerKeyboardAction(botão.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)), KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), JComponent.WHEN_IN_FOCUSED_WINDOW);
  285.  
  286.         botão.registerKeyboardAction(botão.getActionForKeyStroke(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)), KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true), JComponent.WHEN_IN_FOCUSED_WINDOW);
  287.  
  288.     }
  289.  
  290.     void pausarActionPerformed(ActionEvent evt) {
  291.         contador = false;
  292.  
  293.     }
  294.  
  295.     public static boolean isContador() {
  296.         return contador;
  297.     }
  298.  
  299.     private void iniciarActionPerformed(ActionEvent evt) {
  300.  
  301.         tempoCorrendo.revalidate();
  302.         contador = true;
  303.  
  304.     }
  305.  
  306.     public static void main(String[] args) {
  307.  
  308.         CRONO3 tela = new CRONO3();
  309.  
  310.         tela.setVisible(true);
  311.         tela.setLocationRelativeTo(null);
  312.  
  313.     }
  314.  
  315. }
  316.  
  317. ========================================================================================================
  318. ========================================================================================================
  319.  
  320. //>>>>>>>>>>>>>>>>>>>>>CLASSE 4 (precisa de alterações na classe principal ara ser implementada)
  321.  
  322. package crono3;
  323.  
  324. import javax.swing.*;
  325.  
  326. public class InspTime extends Thread {
  327.  
  328.     private JLabel labelInsp;
  329.  
  330.     boolean threadInspON = false;
  331.     boolean threadInspOFF = true;
  332.     boolean ganhouDNF = false;
  333.  
  334.     int segInsp = 3; //apenas 3 pq é só pra testar..
  335.  
  336.     public InspTime(JLabel stringInsp) {
  337.  
  338.         this.labelInsp = stringInsp;
  339.     }
  340.  
  341.     public void run() {
  342.  
  343.         while (true) {
  344.  
  345.             try {
  346.                 Thread.sleep(1000);
  347.  
  348.                 threadInspON = true;
  349.                 threadInspOFF = false;
  350.  
  351.                 if (CRONO3.isContadorInsp()) {
  352.                     segInsp--;
  353.  
  354.                     this.labelInsp.setText(String.valueOf(segInsp));
  355.                     this.labelInsp.revalidate();
  356.  
  357.                     if (segInsp == 0) {
  358.  
  359.                         this.labelInsp.setText("DNF");
  360.                         ganhouDNF = true;
  361.  
  362.                         stop();
  363.                     }
  364.  
  365.                 }
  366.  
  367.             } catch (InterruptedException e) {
  368.  
  369.             }
  370.  
  371.         }
  372.  
  373.     }
  374.  
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement