Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //>>>>>>>>>>>>>>>>CLASSE 1
- package crono3;
- import javax.swing.*;
- public class Cronometragem extends Thread {
- private JLabel label;
- boolean threadON = false;
- boolean threadOFF = true;
- boolean threadON2;
- boolean threadOFF2;
- int centésimo = 0, segundo = 0, minuto = 0;
- public Cronometragem(JLabel stringTempo) {
- this.label = stringTempo;
- }
- @Override
- public void run() {
- while (true) {
- try {
- Thread.sleep((long) 9.6787);
- threadON = true;
- threadOFF = false;
- if (CRONO3.isContador()) {
- centésimo++;
- if (centésimo == 100) {
- centésimo = 0;
- segundo++;
- }
- if (segundo == 60) {
- segundo = 0;
- minuto++;
- }
- String timerMinuto = "";
- String timerSegundo = "";
- String timerCentésimo = "";
- if (minuto < 10) {
- timerMinuto = "0" + minuto;
- } else {
- timerMinuto = String.valueOf(minuto);
- }
- if (segundo < 10) {
- timerSegundo = "0" + segundo;
- } else {
- timerSegundo = String.valueOf(segundo);
- }
- if (centésimo < 10) {
- timerCentésimo = "0" + centésimo;
- } else {
- timerCentésimo = String.valueOf(centésimo);
- }
- this.label.setText(timerMinuto + ":" + timerSegundo + "." + timerCentésimo);
- this.label.revalidate();
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
- ===============================================================================================================
- ===============================================================================================================
- //>>>>>>>>>>>>>>>>>>>CLASSE 2
- package crono3;
- import java.util.*; //Random
- public class Scrambler2x2 {
- public static String embaralhar2x2() {
- String[] moves1 = {"F", "U", "R"};
- String[] moves2 = {" ", "' "};
- Random r = new Random();
- String s = "";
- String f = "";
- String retorno = "";
- for (int i = 0; i < 8; i++) {
- do {
- f = moves1[r.nextInt(3)];
- } while (f == s);
- s = f;
- String u = moves2[r.nextInt(2)];
- retorno += s + u;
- }
- return retorno;
- }
- }
- ===============================================================================================================
- ===============================================================================================================
- //>>>>>>>>>>>>>CLASSE 3
- package crono3;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class CRONO3 extends JFrame {
- static JLabel tempoCorrendo;
- static JLabel tempoCorrendoInsp;
- static boolean contador = true;
- boolean prontoParaPause = false;
- boolean botãoSemUtilidadePorEnquanto = false;
- JLabel label1;
- JButton botão1, botão2, botão3;
- Scrambler3x3 scramble1 = new Scrambler3x3();
- public CRONO3() {
- setTitle("Timer");
- setSize(441, 217);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setLayout(new FlowLayout(FlowLayout.CENTER));
- label1 = new JLabel();
- label1.setText("Scramble: " + String.valueOf(scramble1.embaralhar3x3()));
- add(label1);
- tempoCorrendo = new JLabel();
- tempoCorrendo.setText("00:00.00");
- tempoCorrendo.setFont(new Font("Times New Roman", Font.BOLD, 100));
- add(tempoCorrendo);
- Cronometragem running = new Cronometragem(tempoCorrendo);
- InspTime runningInsp = new InspTime(tempoCorrendo);
- botão1 = new JButton("Start/Stop");
- botão1.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- if (running.threadCronoON == false) {
- running.start();
- }
- if (running.threadCronoON == true) {
- iniciarActionPerformed(evt);
- }
- if (running.threadCronoOFF == false) {
- pausarActionPerformed(evt);
- label1.setText("Scramble: " + String.valueOf(scramble1.embaralhar3x3()));
- botão1.setVisible(false);
- botão1.setEnabled(false);
- botão2.setEnabled(true);
- botão2.setVisible(true);
- botão3.setVisible(false);
- botão3.setEnabled(false);
- }
- }
- });
- botão2 = new JButton("Start/Stop");
- botão2.setVisible(false);
- botão2.setEnabled(false);
- botão2.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- if (running.threadCronoON == true) {
- if (running.centésimo > 0 || running.minuto > 0 || running.segundo > 0) {
- running.centésimo = 0;
- running.minuto = 0;
- running.segundo = 0;
- }
- iniciarActionPerformed(evt);
- botão1.setVisible(false);
- botão1.setEnabled(false);
- botão2.setVisible(false);
- botão2.setEnabled(false);
- botão3.setEnabled(true);
- botão3.setVisible(true);
- prontoParaPause = true;
- }
- }
- });
- botão3 = new JButton("Start/Stop");
- botão3.setEnabled(false);
- botão3.setVisible(false);
- botão3.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent evt) {
- if (prontoParaPause == true) {
- pausarActionPerformed(evt);
- label1.setText("Scramble: " + String.valueOf(scramble1.embaralhar3x3()));
- botão1.setEnabled(true);
- botão1.setVisible(true);
- botão2.setVisible(false);
- botão2.setEnabled(false);
- botão3.setVisible(false);
- botão3.setEnabled(false);
- botãoSemUtilidadePorEnquanto = true;
- if (botãoSemUtilidadePorEnquanto == true) {
- botão1.setVisible(false);
- botão1.setEnabled(false);
- botão2.setEnabled(true);
- botão2.setVisible(true);
- botão3.setVisible(false);
- botão3.setEnabled(false);
- }
- }
- }
- });
- add(botão1);
- reagirSpace(botão1);
- add(botão2);
- reagirSpace(botão2);
- add(botão3);
- reagirSpace(botão3);
- }
- public static void reagirSpace(JButton botão) {
- 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);
- 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);
- }
- void pausarActionPerformed(ActionEvent evt) {
- contador = false;
- }
- public static boolean isContador() {
- return contador;
- }
- private void iniciarActionPerformed(ActionEvent evt) {
- tempoCorrendo.revalidate();
- contador = true;
- }
- public static void main(String[] args) {
- CRONO3 tela = new CRONO3();
- tela.setVisible(true);
- tela.setLocationRelativeTo(null);
- }
- }
- ========================================================================================================
- ========================================================================================================
- //>>>>>>>>>>>>>>>>>>>>>CLASSE 4 (precisa de alterações na classe principal ara ser implementada)
- package crono3;
- import javax.swing.*;
- public class InspTime extends Thread {
- private JLabel labelInsp;
- boolean threadInspON = false;
- boolean threadInspOFF = true;
- boolean ganhouDNF = false;
- int segInsp = 3; //apenas 3 pq é só pra testar..
- public InspTime(JLabel stringInsp) {
- this.labelInsp = stringInsp;
- }
- public void run() {
- while (true) {
- try {
- Thread.sleep(1000);
- threadInspON = true;
- threadInspOFF = false;
- if (CRONO3.isContadorInsp()) {
- segInsp--;
- this.labelInsp.setText(String.valueOf(segInsp));
- this.labelInsp.revalidate();
- if (segInsp == 0) {
- this.labelInsp.setText("DNF");
- ganhouDNF = true;
- stop();
- }
- }
- } catch (InterruptedException e) {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement