How do I place the for every 1 second?

Asked

Viewed 317 times

-5

I managed to put the effect of clicking on the button but still put a click every second because it is going too fast

package gênius;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class tela {

    //nome do jogador e pontos
    int pontos = 0 ;
    String usuario = "";

    //varieaveis de jogadas
    String jjogador = "";
    String maquina ="";
    int aux ;

    Random numeroale = new Random();

    // janela de exibição
    public void janela(){
        JFrame  ftela = new JFrame();
        ftela.setTitle("Gênius");
        ftela.setSize(400, 600);
        ftela.setBackground(Color.white);
        ftela.setLocationRelativeTo(null);
        ftela.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ftela.setVisible(true);
        ftela.setLayout(null);

        //boões 
        JButton b1 = new JButton("0");
        b1.setBounds(120, 220, 80, 80);
        b1.setVisible(true);
        b1.setBackground(Color.BLUE);

        JButton b2 = new JButton("1");
        b2.setBounds(200, 220, 80, 80);
        b2.setVisible(true);
        b2.setBackground(Color.red);

        JButton b3 = new JButton("2");
        b3.setBounds(120, 300, 80, 80);
        b3.setVisible(true);
        b3.setBackground(Color.GREEN);

        JButton b4 = new JButton("3");
        b4.setBounds(200, 300, 80, 80);
        b4.setVisible(true);
        b4.setBackground(Color.YELLOW);

        // exibir pontos
        JLabel mPontos = new JLabel("pontos :"+maquina);
        mPontos.setHorizontalAlignment(SwingConstants.CENTER);
        mPontos.setBounds(70, 150, 80,80);

        //botão de inicar o jogo
        JButton inicio = new JButton("INICIAR");
        inicio.setBounds(90, 10, 80, 30);
        inicio.setVisible(true);


        JPanel p1 = new JPanel();
        p1.setVisible(true);
        ftela.add(mPontos);
        ftela.add(b1);
        ftela.add(b2);
        ftela.add(b3);
        ftela.add(b4);
        ftela.add(inicio);

        //tempo de pause para efeito de botão clicado
        Timer tempo = new Timer();
        TimerTask tarefa = new TimerTask() {

            @Override
            public void run() {
                b1.setBackground(Color.BLUE);
            }


        }   ; 
        Timer tempo2 = new Timer();
        TimerTask tarefa2 = new TimerTask() {

            @Override
            public void run() {
                b2.setBackground(Color.red);
            }


        }   ; 
        Timer tempo3 = new Timer();
        TimerTask tarefa3 = new TimerTask() {

            @Override
            public void run() {
                b3.setBackground(Color.green);
            }


        }   ; 
        Timer tempo4 = new Timer();
        TimerTask tarefa4 = new TimerTask() {

            @Override
            public void run() {
                b4.setBackground(Color.yellow);
            }


        }   ; 

        //fechamento de pausa do botão

        //gerar numeros aleatorios

        //  maquina += num.nextInt(4);


        // acão do botão inicio
        inicio.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                maquina += numeroale.nextInt(4);
                maquina += numeroale.nextInt(4);
                System.out.println(maquina+"\n"+maquina.length()+"\n"+maquina.substring(0,1)+"\n"+maquina.substring(1,2));
                //vez da mqauina
                for (int i = 0; i < maquina.length(); i++) {

                    System.out.println("valor atual"+maquina.substring(i,1));

                    if((maquina.substring(i,i+1)).equals("0")){
                        System.out.println("1 pressed");
                        play("b1");
                        b1.setBackground(Color.white);
                        tempo.scheduleAtFixedRate(tarefa, 1000, 1000);

                    }
                    else if((maquina.substring(i,i+1)).equals("1")){
                        System.out.println("2 pressed");
                        play("b2");
                        b2.setBackground(Color.white);
                        tempo2.scheduleAtFixedRate(tarefa2, 1000, 1000);                

                    }
                    else if((maquina.substring(i,i+1)).equals("2")){
                        System.out.println("3 pressed");
                        play("b3");
                        b3.setBackground(Color.white);
                        tempo3.scheduleAtFixedRate(tarefa3, 1000, 1000);


                    } else{
                        System.out.println("4 pressed");
                        play("b4");
                        b4.setBackground(Color.white);
                        tempo4.scheduleAtFixedRate(tarefa4, 1000, 1000);

                    }

                }   

                //vez do jogador 

                //encerra o jogo
            }
        }); 


        //ações dos botôes
        b1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b1");
                jjogador +=0;

            }
        });
        b2.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b2");
                jjogador += " "+1;


            }
        });
        b3.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b3");
                jjogador += " "+2;

            }
        });
        b4.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                play("b4");
                jjogador += " "+3;

            }
        });

    }
    //  som no botão 
    public void play(String audio){
        URL url = getClass().getResource(audio+".wav");
        AudioClip  audioc = Applet.newAudioClip(url);
        audioc.play();
    }

    public void jlabe (){
        JLabel mPontos = new JLabel("pontos ");
        mPontos.setText("pontos :"+pontos);
        mPontos.setHorizontalAlignment(SwingConstants.CENTER);
        mPontos.setBounds(70, 200, 40, 40);
    }

    public void contagem(){
        try {Thread.sleep(1000);} catch (InterruptedException e) {}
    }

}

1 answer

1

    @Override
    public void actionPerformed(ActionEvent e) {
            b3.setBackground(Color.white);
            Thread.sleep(2000); //Aqui você coloca o tempo em milissegundo.  
            b3.setBackground(Color.blue);   
        }
    });
  • Again saying, this does not work in the swing, because the Thread in question that will be locked, is the EDT, and it only updates the GUI after finishing all execution on top of the window, so your code will lock the window for 2 seconds and the button will not change the click, as the OP wants.

  • which I could then ?

  • I had an idea, if it hangs like I make it lock with the button turning white, like some command inside it that on the latch it turns white

  • I managed to do using timertask

Browser other questions tagged

You are not signed in. Login or sign up in order to post.